简体   繁体   English

在python下使用pandas,如何使用matplotlib绘制日期?

[英]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.

相关问题 Python使用matplotlib绘制日期 - Python plot dates using matplotlib 如何在 Python 中使用 pandas 和 matplotlib 绘制条形图时删除条形之间的空间? - How can I remove space between bars while plotting bar plot using pandas and matplotlib in Python? plot 中的日期如何用不同于英语的另一种语言书写? (我正在使用 matplotlib.dates) - How can the dates in a plot be written in another language different than english? (I am using matplotlib.dates) 如何使用matplotlib在python中保存一个图? - How can I save a plot in python using matplotlib? 我如何在 Python 中使用 matplotlib plot 一条最佳拟合线? - How can I plot a line of best fit using matplotlib in Python? 如何使用Python matplotlib绘制带有4个象限的图形或图形? - How can I draw a graph or plot with 4 quadrants using Python matplotlib? 如何使用matplotlib和pandas进行绘图? - How to plot using matplotlib and pandas? 如何使用python pandas或matplotlib获得方波? - How can I get a square wave by using python pandas or matplotlib? 如何在 python 中使用 matplotlib 和 pandas 绘制 CSV 数据 - How to plot CSV data using matplotlib and pandas in python 如何在 Python 中使用 matplotlib 将第二个子 plot 放在第一个子 plot 之上? - How can I bring second sub plot on top of first sub plot using matplotlib in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM