Python 如何在同一时间尺度上绘制具有不同点数的两个数据集?

Python 如何在同一时间尺度上绘制具有不同点数的两个数据集?,python,matplotlib,Python,Matplotlib,这是我的密码 import matplotlib.pyplot as plt years = [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020] ic = [200, 270, 386, 520, 720, 1180, 2110, 4764, 5834, 6832, 7972, 8933, 10897, 12635, 21443] pe = [38035, 492

这是我的密码

import matplotlib.pyplot as plt

years = [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020]
ic = [200, 270, 386, 520, 720, 1180, 2110, 4764, 5834, 6832, 7972, 8933, 10897, 12635, 21443] 
pe = [38035, 49261, 55709, 67246, 73549]

fig = plt.figure()

plt.plot(years, ic,  lw=2, marker='o')
#plt.plot(years, pe,  lw=2, marker='s')
plt.xlabel('Years')
plt.ylabel('Installed Capacity MWe')
plt.grid()
plt.tight_layout()
plt.show() 
图像是我想要的第一个数据集

我从1995年到2015年才开始使用能源。这是我的体育课


如何将此数据集添加到同一绘图上?

当然,要为x和y绘图的值的数量必须相同,并且它们之间需要一一对应。因此,您需要一个新的
years
数据集进行绘图

import matplotlib.pyplot as plt

years1 = [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020]
ic = [200, 270, 386, 520, 720, 1180, 2110, 4764, 5834, 6832, 7972, 8933, 10897, 12635, 21443] 

years2 = [1995, 2000, 2005, 2010, 2015]
pe = [38035, 49261, 55709, 67246, 73549]

fig = plt.figure()

plt.plot(years1, ic,  lw=2, marker='o')
plt.plot(years2, pe,  lw=2, marker='s')
plt.xlabel('Years')
plt.ylabel('Installed Capacity MWe')
plt.grid()
plt.tight_layout()
plt.show()