我一直在试图找出如何绘制这些数据,但无法找出我的错误:
Month Year Sales
January 2020 43
feburary 2020 23
March 2020 13
April 2020 11
May 2020 7
June 2020 2
July 2020 1
August 2020 2
September 2020 22
October 2020 11
November 2020 6
December 2020 3
January 2019 3
feburary 2019 11
March 2019 65
April 2019 22
May 2019 33
June 2019 88
July 2019 44
August 2019 12
September 2019 32
October 2019 54
November 2019 76
December 2019 23
January 2018 12
feburary 2018 32
March 2018 234
April 2018 2432
May 2018 432
June 2018 324
July 2018 12
August 2018 324
September 2018 89
October 2018 6
November 2018 46
December 2018 765
我尝试了以下
y = df["sales"]
x = df["Month"]
plt.plot(x,y)
plt.show()
下面给出了以下图表(确切的值随此处发布的数据值的变化而有所不同):
我该如何纠正它,以使我的地块在每次12月都中断,并为另一年画一条新线?
答案 0 :(得分:2)
如果您的熊猫DataFrame如下所示:
year month sales
0 2020 January 43.0
1 2020 feburary 23.0
2 2020 March 13.0
3 2020 April 11.0
4 2020 May 7.0
5 2020 June 2.0
6 2020 July 1.0
7 2020 August 2.0
8 2020 September 22.0
9 2020 October 11.0
10 2020 November 6.0
11 2020 December 3.0
12 2019 January 3.0
13 2019 feburary 11.0
14 2019 March 65.0
15 2019 April 22.0
16 2019 May 33.0
17 2019 June 88.0
18 2019 July 44.0
19 2019 August 12.0
20 2019 September 32.0
21 2019 October 54.0
22 2019 November 76.0
23 2019 December 23.0
24 2018 January 12.0
25 2018 feburary 32.0
26 2018 March 234.0
27 2018 April 2432.0
28 2018 May 432.0
29 2018 June 324.0
30 2018 July 12.0
31 2018 August 324.0
32 2018 September 89.0
33 2018 October 6.0
34 2018 November 46.0
35 2018 December 765.0
我们可以使用df.groupby('year')
来生成您想要的解析类型:
fig, ax = plt.subplots()
ax.set_xticklabels(df['month'].unique(), rotation=90)
for name, group in df.groupby('year'):
ax.plot(group['month'], group['sales'], label=name)
ax.legend()
plt.tight_layout()
plt.show()
答案 1 :(得分:1)
只需将图添加到同一图形即可:
from matplotlib import pyplot as plt
import pandas as pd
data = pd.read_csv('year_data.csv')
for year in data['Year'].unique():
plt.plot(data[data['Year']==year]['Month'], data[data['Year']==year]['Sales'])
plt.xticks(rotation=90)
plt.show()
答案 2 :(得分:0)
尝试将列转换为DataTime数据类型:
$column-gap
您现在应该可以根据自己的日期进行绘图了。
答案 3 :(得分:0)
您当前的绘图正在汇总同一图表中的所有年份,因此当您到达年底时,它实际上会“循环”。
您可以在12个月的时间段内分割数据框,然后将其实际绘制在同一matplotlib子图上,作为不同的行:
import pandas as pd
import matplotlib.pyplot as plt
######################
## DATA PREPARATION ##
######################
sales20 = df.loc[df['Year'] == 2020, 'Sales']
sales19 = df.loc[df['Year'] == 2019, 'Sales']
##############
## PLOTTING ##
##############
# Create a new figure
fig = plt.figure()
# Add a subplot to the figure
ax = fig.add_subplot()
# Add to the subplot two line plots labeled accordingly
ax.plot(df["Month"], sales20, label='2020')
ax.plot(df["Month"], sales19, label='2019')
# Add handy legend
ax.legend(loc='best')
# Finger crossed and show the graph
plt.show()
我必须承认这是一个快速而肮脏的解决方案...
一种更优雅的方法是将数据帧转换为使用datetime
索引进行索引,请查看Pandas docs on the matter