简体   繁体   English

使用matplotlib在x轴上绘制带有时间数据的条形图

[英]using matplotlib to plot a bar char with time data in x-axis

I have a DataFrame with 2 columns, one is a series of time date in string format, and the other is some value. 我有一个带有2列的DataFrame,一个是字符串格式的一系列时间日期,另一个是一些值。 I'd like to plot this dataframe into a bar chart 我想将此数据框绘制成条形图

Here is my code: 这是我的代码:

import matplotlib.pyplot as plt
import pandas

df = pandas.DataFrame({'a':['090000','093000','100000','103000'],'b':[2,3,4,5]})
df['a']=pandas.to_datetime(df['a'],format='%H%M%S')

fig = plt.figure()

ax1=fig.add_subplot(111)

ax1.bar(df['a'],df['b'])

but I found this error: 但是我发现了这个错误:

TypeError: float() argument must be a string or a number, not 'Timestamp'

how can I solve this problem?? 我怎么解决这个问题?? thanks 谢谢

I had a similar issue on python 2.7 when upgrading matplotlib from version 1.4.3 to 1.5.1 将matplotlib从1.4.3版本升级到1.5.1时,在python 2.7上我遇到了类似的问题

Going back the error disappeared. 回去错误消失了。

I had the same issue (matplotlib 1.5.1). 我遇到了同样的问题(matplotlib 1.5.1)。 In my case I could resolve it by setting date variable as an index, and then plotting bar chart against data frame index. 就我而言,我可以通过将日期变量设置为索引来解决它,然后针对数据框索引绘制条形图。 Ie in the code above, before plotting, one would need to add 即在上面的代码中,在绘制之前,需要添加

df.set_index('a', inplace=True)

and then plot with 然后用

ax1.bar(df.index, df['b'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM