Python 在matplotlib中打印数据之前在xaxis上打印年份

Python 在matplotlib中打印数据之前在xaxis上打印年份,python,matplotlib,plot,Python,Matplotlib,Plot,我想预先规划xaxis,使其包含5年,即2012201320142015年和2016年。然后假设我有两组数据,第一组是两个列表: years1 = ['2012','2013'] scores1 = [0.2,0.3] 第二个列表也是两个列表,但长度与第一个不同: years2 = ['2013','2014','2015'] scores2 = [0.5,-0.4,0.8] 如何使用python中的matplotlib在一个绘图中绘制这两组数据?xaxis是预先确定的。您可以调用scatt

我想预先规划xaxis,使其包含5年,即2012201320142015年和2016年。然后假设我有两组数据,第一组是两个列表:

years1 = ['2012','2013']
scores1 = [0.2,0.3]
第二个列表也是两个列表,但长度与第一个不同:

years2 = ['2013','2014','2015']
scores2 = [0.5,-0.4,0.8]

如何使用python中的matplotlib在一个绘图中绘制这两组数据?xaxis是预先确定的。

您可以调用scatter两次:

import matplotlib.pyplot as plt

years1_string = ['2012','2013']
years2_string = ['2013','2014','2015']

years1 = [int(i) for i in years1_string]
scores1 = [0.2,0.3]

years2 = [int(i) for i in years2_string]
scores2 = [0.5,-0.4,0.8]

fig, ax = plt.subplots(1)
ax.set_xlim(2012,2016)
ax.set_xticks([2012,2013,2014,2015,2016])
ax.scatter(years1, scores1, c='r', edgecolor=None, label = 'One')
ax.scatter(years2, scores2, c='b', edgecolor=None, label = 'Two')
ax.legend()
fig.show()

您只需调用scatter两次:

import matplotlib.pyplot as plt

years1_string = ['2012','2013']
years2_string = ['2013','2014','2015']

years1 = [int(i) for i in years1_string]
scores1 = [0.2,0.3]

years2 = [int(i) for i in years2_string]
scores2 = [0.5,-0.4,0.8]

fig, ax = plt.subplots(1)
ax.set_xlim(2012,2016)
ax.set_xticks([2012,2013,2014,2015,2016])
ax.scatter(years1, scores1, c='r', edgecolor=None, label = 'One')
ax.scatter(years2, scores2, c='b', edgecolor=None, label = 'Two')
ax.legend()
fig.show()

您不必预先确定X轴。它将根据数据自动调整。如果您不想这样做,当然可以手动设置限制或刻度

然后可以将字符串转换为整数进行打印:

import matplotlib.pyplot as plt

years1 = ['2012','2013']
scores1 = [0.2,0.3]
years2 = ['2013','2014','2015']
scores2 = [0.5,-0.4,0.8]

#convert string list to integer list
y1 = list(map(int, years1))
y2 = list(map(int, years2))

plt.plot(y1, scores1, marker="o")
plt.plot(y2, scores2, marker="o")
plt.xticks(y1+y2)

plt. show()
但是,matplotlib可以处理打印字符串,只要它们可以转换为有意义的数字或日期。所以下面的方法也很好

import matplotlib.pyplot as plt

years1 = ['2012','2013']
scores1 = [0.2,0.3]
years2 = ['2013','2014','2015']
scores2 = [0.5,-0.4,0.8]

plt.plot(years1, scores1, marker="o")
plt.plot(years2, scores2, marker="o")
plt.xticks(range(2012,2016))

plt. show()

您不必预先确定X轴。它将根据数据自动调整。如果您不想这样做,当然可以手动设置限制或刻度

然后可以将字符串转换为整数进行打印:

import matplotlib.pyplot as plt

years1 = ['2012','2013']
scores1 = [0.2,0.3]
years2 = ['2013','2014','2015']
scores2 = [0.5,-0.4,0.8]

#convert string list to integer list
y1 = list(map(int, years1))
y2 = list(map(int, years2))

plt.plot(y1, scores1, marker="o")
plt.plot(y2, scores2, marker="o")
plt.xticks(y1+y2)

plt. show()
但是,matplotlib可以处理打印字符串,只要它们可以转换为有意义的数字或日期。所以下面的方法也很好

import matplotlib.pyplot as plt

years1 = ['2012','2013']
scores1 = [0.2,0.3]
years2 = ['2013','2014','2015']
scores2 = [0.5,-0.4,0.8]

plt.plot(years1, scores1, marker="o")
plt.plot(years2, scores2, marker="o")
plt.xticks(range(2012,2016))

plt. show()

嗨:谢谢你的快速回答!然而,我刚刚发现年份列表中的元素实际上不是整数,而是字符串。我怎么还能用你的代码做类似的事情呢?我想我现在不能使用xlim。我刚刚编辑了这个问题。谢谢嗨:谢谢你的快速回答!然而,我刚刚发现年份列表中的元素实际上不是整数,而是字符串。我怎么还能用你的代码做类似的事情呢?我想我现在不能使用xlim。我刚刚编辑了这个问题。谢谢嗨:我用了你的第一种方法。但是,x轴不显示“20122015”,而是显示“0,4”。还有什么我可以修的吗?谢谢这两种方法给出了答案中所示的相同曲线图。除了向您展示一个有效的示例之外,我还能为您提供什么帮助?你在复制代码时出错了吗?嗨:我用了你的第一种方法。但是,x轴不显示“20122015”,而是显示“0,4”。还有什么我可以修的吗?谢谢这两种方法给出了答案中所示的相同曲线图。除了向您展示一个有效的示例之外,我还能为您提供什么帮助?你在复制代码时出错了吗?