Python 使用列值之一作为Xaxis打印栏
如何使用熊猫绘图栏,将日期值作为Xaxis 像这样:Python 使用列值之一作为Xaxis打印栏,python,pandas,plot,Python,Pandas,Plot,如何使用熊猫绘图栏,将日期值作为Xaxis 像这样: 我认为您需要首先转换列日期,然后转换为 最后: 如果要绘制日期,请执行以下操作: df.date = pd.to_datetime(df.date).dt.year df = df.set_index('date') df.index.name= None print (df) A B C D 2010 53936248 53928376 7840 2800 2011 22
我认为您需要首先转换列
日期,然后转换为
最后:
如果要绘制日期,请执行以下操作:
df.date = pd.to_datetime(df.date).dt.year
df = df.set_index('date')
df.index.name= None
print (df)
A B C D
2010 53936248 53928376 7840 2800
2011 22936276 53928404 7840 2800
2012 523763 53928404 7840 2800
df.plot.bar(rot=0)
假设日期
已经是日期时间
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df.date = pd.to_datetime(df.date)
df = df.set_index('date')
df.index.name= None
print (df)
A B C D
2010-02-17 53936248 53928376 7840 2800
2011-02-17 22936276 53928404 7840 2800
2012-02-17 523763 53928404 7840 2800
ax = df.plot.bar(rot=45)
ticklabels = df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df.date = pd.to_datetime(df.date)
df = df.set_index('date')
df.index.name= None
print (df)
A B C D
2010-02-17 53936248 53928376 7840 2800
2011-02-17 22936276 53928404 7840 2800
2012-02-17 523763 53928404 7840 2800
ax = df.plot.bar(rot=45)
ticklabels = df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()
df.assign(year=df.date.dt.year) \
.set_index('year').drop('date', 1) \
.rename_axis([None]).plot.bar(rot=0)