如何使用 python(Matplotlib) 两个列表进行绘图,其中一个列表包含年份,第二个包含负值和正值
How to plot using python(Matplotlib) two list in which one list contain year and second contain Negative and positive values
我有两个列表 -
years = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
profits = ['362', '622', '-409', '-92', '-148', '-130', '-128', '98', '-74', '35', '-419']
如何绘制此列表,我应该在此处使用哪个图表?
我试过这段代码,但输出不正确 -
from matplotlib import pyplot as plt
# Figure Size
fig = plt.figure(figsize =(10, 7))
# Horizontal Bar Plot
plt.bar(years, profits)
# Show Plot
plt.show()
我得到错误的输出:
当前您有一个字符串列表。您应该使用
将其转换为浮点数列表
years = list(map(float, years))
profits = list(map(float, profits))
如果您想要一个整数列表,只需将 float
更改为 int
。
我有两个列表 -
years = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
profits = ['362', '622', '-409', '-92', '-148', '-130', '-128', '98', '-74', '35', '-419']
如何绘制此列表,我应该在此处使用哪个图表?
我试过这段代码,但输出不正确 -
from matplotlib import pyplot as plt
# Figure Size
fig = plt.figure(figsize =(10, 7))
# Horizontal Bar Plot
plt.bar(years, profits)
# Show Plot
plt.show()
我得到错误的输出:
当前您有一个字符串列表。您应该使用
将其转换为浮点数列表years = list(map(float, years))
profits = list(map(float, profits))
如果您想要一个整数列表,只需将 float
更改为 int
。