Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python绘制两个长度不同的列表_Python_Matplotlib - Fatal编程技术网

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

Python绘制两个长度不同的列表,python,matplotlib,Python,Matplotlib,我有两张不同价格的单子。第一份名单是2008-2018年,第二份是2010-2018年。如果2008年至2018年在X轴上,第二个列表从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.subpl

我有两张不同价格的单子。第一份名单是2008-2018年,第二份是2010-2018年。如果2008年至2018年在X轴上,第二个列表从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轴上绘制的位置,例如,通过标记年份[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轴更新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")

要告诉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')

我建议您只需为每组点定义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()

有很多方法可以实现这一点。一种优雅的方式是使用熊猫。这样,您可以自动获得正确的标签和对齐的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()


这并不一定能产生预期的效果。虽然它在某些情况下可能会起作用,但引入虚假数据通常不是一个好主意。用Nan替换数据,我会同意您正在复制预期的结果,这很公平-更新我喜欢图像:)imgur告诉我它坏了…正在处理它。而且,你完全正确-这里的零是完全错误的。只是行动太快了。谢谢你的提醒。现在x轴更不正确了。2008年的范围是一个巨大的序列,几乎不包含截至2018年的所有感兴趣的数字。尝试运行代码。请在发布前运行代码。现在你有一个明显的语法错误。再一次,谢谢,是的,你是对的。有点仓促。现在我们都有了相同的答案:)@madpysicast当然有。输入列表中只有10种价格,所以从2008年到2017年都包括在内,这就是为什么我使用了
范围(20182018)
。我很抱歉这么无礼。我算错了。修正了我自己的代码。投票当然翻转了。你将
Geb\u b30\u x
转换为
list
的目的是什么?即使不转换到
list
或使用
np.arange
@Bazinga,您的两种解决方案也可以正常工作。修正了第一个。第二个应该很清楚。我的意思是,如果你只写
years=range(2008,2019)
,然后使用
years[2::
,它仍然有效。不需要
np.arange
@Bazinga。我懂了。修正。你能接受答案吗?对不起,我忘了接受答案。你的答案很好,所以我用这个。