在matplotlib中的绘图数据之前绘制x轴上的年份

时间:2021-08-23 23:39:11

I want to pre plot xaxis so that it contains 5 years, which are 2012,2013,2014,2015 and 2016. Then say I have two sets of data, first one is two list:

我想预先绘制xaxis,使其包含5年,即2012,2013,2014,2015和2016.然后说我有两组数据,第一组是两个列表:

years1 = ['2012','2013']
scores1 = [0.2,0.3]

second one is also two lists, but has different length from the first one:

第二个也是两个列表,但与第一个列表的长度不同:

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

How can I plot these two sets of data in one plot, using matplotlib in python? The xaxis is predetermined.

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

2 个解决方案

#1


1  

You can just call scatter twice:

你可以调用两次散点图:

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

在matplotlib中的绘图数据之前绘制x轴上的年份

#2


0  

You don't have to predetermine the xaxis. It will adjust automatically to the data. If you dont want that you can of course set the limits or ticks manually.

您不必预先确定xaxis。它会自动调整数据。如果您不想要,您当然可以手动设置限制或滴答。

You may then convert your strings to integers for plotting:

然后,您可以将字符串转换为整数以进行绘图:

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

However, matplotlib is fine with plotting strings as long as they can be converted to a meaningful number or date. So the following works fine as well.

但是,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()

在matplotlib中的绘图数据之前绘制x轴上的年份

#1


1  

You can just call scatter twice:

你可以调用两次散点图:

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

在matplotlib中的绘图数据之前绘制x轴上的年份

#2


0  

You don't have to predetermine the xaxis. It will adjust automatically to the data. If you dont want that you can of course set the limits or ticks manually.

您不必预先确定xaxis。它会自动调整数据。如果您不想要,您当然可以手动设置限制或滴答。

You may then convert your strings to integers for plotting:

然后,您可以将字符串转换为整数以进行绘图:

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

However, matplotlib is fine with plotting strings as long as they can be converted to a meaningful number or date. So the following works fine as well.

但是,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()

在matplotlib中的绘图数据之前绘制x轴上的年份