Python 使用Matplotlib和Pandas打印日期
我正试图根据X轴上的日期绘制Y轴上的销售额。我似乎想不出一个办法来计算X轴上的日期。如果我将日期更改为浮动,我可以绘制它,但我仍然无法找到绘制日期格式的方法Python 使用Matplotlib和Pandas打印日期,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我正试图根据X轴上的日期绘制Y轴上的销售额。我似乎想不出一个办法来计算X轴上的日期。如果我将日期更改为浮动,我可以绘制它,但我仍然无法找到绘制日期格式的方法 import matplotlib.pyplot as plt import csv x = [] y = [] with open('book1.csv','r') as csvfile: plots = csv.reader(csvfile, delimiter=',') for row in plots:
import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('book1.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(float(row[0]))
y.append(float(row[1]))
plt.plot(x,y, label='Loaded from file!')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
我的csv看起来像:
1/1/2019,7980.185714
2/1/2019,9478.297619
3/1/2019,9282.166667
4/1/2019,6900.833333
5/1/2019,5563.716667
如果我将日期更改为浮点格式,我将得到以下图形:
您需要将
x
的类型更改为datetime:
import matplotlib.pyplot as plt
import csv
import datetime as dt
x = []
y = []
with open('book1.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(dt.datetime.strptime(row[0],'%m/%d/%Y').date())
y.append(float(row[1]))
plt.plot(x,y, label='Loaded from file!')
plt.xlabel('x')
plt.xticks(x, x, rotation=90)
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
非常感谢。如何将日期标签间隔更改为每日?因为现在它每4天标记一次日期。