matplotlib 绘制两个长度不同的列表

bqjvbblv  于 2023-05-18  发布在  其他
关注(0)|答案(5)|浏览(116)

我有两张价格不同的单子。第一份名单是2008-2018年,第二份是2010-2018年。在X轴上是2008年到2018年,而第二个列表从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()
31moq8wy

31moq8wy1#

要告诉matplotlib你希望点在x轴上的什么位置结束,你必须显式地提供x值。x轴值的大小必须与y值的大小相对应,但是在独立数据集之间不需要有任何关系,正如您已经看到的。

Geb_x = range(2008, 2018)

...

ax.plot(Geb_x, Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(Geb_x[2:], Geb_a30, label='Prices 2010-2018', color = 'red')
b0zn9rqh

b0zn9rqh2#

您应该创建一个包含您的年份的新列表。然后,您可以指定在x轴上的位置,您想绘制的odoing年[10:18],例如

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]

years = list(range(2008,2018))

fig, ax = plt.subplots()
ax.plot(years[0:len(Geb_b30)],Geb_b30, label='Prices 2008-2018', 
color='blue')
ax.plot(years[2:],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轴

t98cgbkg

t98cgbkg3#

有很多方法可以实现这一点。一个优雅的方法是使用Pandas。这样,您就可以自动获得正确标记和对齐的x刻度。

from matplotlib import pyplot as plt
import pandas as pd

geb_b30_x = pd.date_range(start="20080101", end="20180101", freq="A")
geb_b30_y = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
geb_b30 = pd.Series(data=geb_b30_y, index=geb_b30_x)

geb_a30_x = pd.date_range(start="20100101", end="20180101", freq="A")
geb_a30_y = [12, 10, 13, 14, 12, 13, 18, 16]
geb_a30 = pd.Series(data=geb_a30_y, index=geb_a30_x)

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

lg40wkob4#

我建议您简单地定义x值(即年份列表),并将它们传递到ax.plot()的参数中,如下所示:

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

2fjabf4q5#

IIUC,用None/NaN来填补你缺失的年份:

import pandas as pd

years = list(range(2008, 2018))
Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
Geb_a30 = [None, None, 12, 10, 13, 14, 12, 13, 18, 16]
df = pd.DataFrame({"years":years, "b30": Geb_b30, "a30": Geb_a30})

df.plot(x="years")

相关问题