[英]Using pandas under python, how can I plot dates using matplotlib?
I have a datframe bankdat with two columns, one with the date in str format, one with floats. 我有一个带有两列的datframe bankdat,一列是str格式的日期,另一列是浮点数。 I want to use pandas and matplotlib to generate a plot with the x-axis being the dates and the y-axis the floats. 我想用pandas和matplotlib生成一个图,x轴是日期,y轴是浮点数。 I converted the strings to dates using 我用字符串将字符串转换为日期
bankdat['Buchungstag'] = pd.to_datetime(bankdat['Buchungstag'])
When I now try to plot my dataframe using 当我现在尝试使用时绘制我的数据帧
bankdat.plot()
plt.show()
I get numbers instead of dates on my x-axis: 我的x轴上有数字而不是日期:
What am I dointg wrong? 我做错了什么? Thanks for the help PS: I'm new to coding and stackoverflow and would be very grateful if you pointed out things i got wrong 感谢PS的帮助:我是编码和stackoverflow的新手,如果你指出我做错了,我将非常感激
Edit: Thanks for your suggestions. 编辑:谢谢你的建议。 Unfortunatly, using set index yields this plot: fig2 . 不幸的是,使用set index得出这个图: fig2 。 Am i using different distributions than suggested? 我使用的分布比建议的不同吗?
Edit2. EDIT2。 I am getting the dataframe from a csv file using 我正在使用csv文件获取数据帧
ausgaben = pd.read_csv('asd.csv', encoding = 'ISO-8859-1',sep=';', skiprows=6,usecols=[0,7])
after that, i am adding a first row using 之后,我正在使用添加第一行
bankdat = pd.DataFrame(np.array([[np.nan, '10000']]), columns=[ausgaben.columns[0], ausgaben.columns[1]]).append(ausgaben)
thanks for pointing out the date ordering, i didnt even get to the "thinking weather my data makes sense" stage. 谢谢你指出日期排序,我甚至没有达到“思考天气我的数据有意义”的阶段。 The dataframe looks like this: example 3 , and exported to csv gives 数据框如下所示: 示例3 ,并导出到csv给出
,Betrag (EUR),Kontostand
,4130.96,4130.96
2016-12-23,-65.0,4065.96
2016-12-20,520.0,4585.96
2016-12-19,-29.5,4556.46
2016-09-12,-1000.0,3556.46
Solved as per Dmitry Polonskiy's suggestion:" It could be the fact that you have a NAT as the first date in your dates column. Try this bankdat.Buchungstag.shift(1).plot()". 根据Dmitry Polonskiy的建议解决:“可能是你的日期栏中有第一个日期的NAT。试试这个bankdat.Buchungstag.shift(1).plot()”。 Thank you for yoyur help! 谢谢yoyur的帮助!
bankdat.set_index(['Buchungstag'], inplace = True)
bankdat.index.name = None
bankdat.Buchungstag.plot(figsize=(10,6), title = 'Title', fontsize = 16)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.