Python绘制两个长度不同的列表 [英] Python plot two lists with different length
本文介绍了Python绘制两个长度不同的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个价格不同的清单.第一个列表是 2008-2018 年,第二个是 2010-2018 年.在 2008 年到 2018 年在 X 轴上并且第二个列表从 2010 年开始的情况下,我如何绘制它们?
I have two lists with different prices. The first list is for the years 2008-2018 and the second for the years 2010-2018. How I can plot them under the condition that the years 2008 to 2018 are on the X-axis and the second list starts in 2010?
我以以下代码为例:
from matplotlib import pyplot as plt
Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
Geb_a30 = [12, 10, 13, 14, 12, 13, 18, 16]
fig, ax = plt.subplots()
ax.plot(Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(Geb_a30, label='Prices 2010-2018', color = 'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()
推荐答案
我建议您简单地为每组点定义x值(即年份列表),并将它们传递给 ax的参数.plot()
,如下所示:
I suggest you to simply define the x values (i.e. the list of years) for each set of points, and to pass them in parameters of ax.plot()
, as follows:
from matplotlib import pyplot as plt
Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
years_b30 = range(2008,2018)
Geb_a30 = [12, 10, 13, 14, 12, 13, 18, 16]
years_a30 = range(2010,2018)
fig, ax = plt.subplots()
ax.plot(years_b30, Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(years_a30, Geb_a30, label='Prices 2010-2018', color = 'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()
这篇关于Python绘制两个长度不同的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文