我有两个价格不同的清单。第一个是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()
Run Code Online (Sandbox Code Playgroud)
我建议您简单地为每组点定义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()
Run Code Online (Sandbox Code Playgroud)
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")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2871 次 |
最近记录: |