如何使用python(Matplotlib)绘制两个列表,其中一个列表包含年份,第二个列表包含负值和正值

2024-04-20 02:41:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两份名单-

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()

我得到的输出错误:

My Wrong Output Graph Image


Tags: 代码fromimport列表plotmatplotlibas图表
1条回答
网友
1楼 · 发布于 2024-04-20 02:41:30

当前您有一个字符串列表。您应该使用

years = list(map(float, years))
profits = list(map(float, profits))

如果您想要一个int列表,只需将float更改为int

相关问题 更多 >