本文介绍了Pandas - 条形图和折线图 - 日期时间轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制条形图和折线图,其中我的数据以日期时间作为索引.这是代码:
I am trying to plot a bar chart and a line overlay, where my data has datetime as an index.This is the code:
import pandas as pd
import numpy as np
dates = pd.date_range('2019-01-01', '2019-01-31', freq='B')
df = pd.DataFrame(index=dates,
columns=['a', 'b', 'c'],
data = np.random.randn(len(dates), 3))
fig, ax = plt.subplots()
df.plot.bar(ax=ax)
df.sum(axis=1).plot(ax=ax)
不幸的是,它最终只显示请求的最后一个图表.
Unfortunately, it only ends up showing the last chart requested.
我正在使用
python 3.6.8
pandas 0.24.0
matplotlib 3.0.2
问候
推荐答案
按照@ImportanceOfBeingErnest 的评论,我认为回答这个问题的最好方法是使用 use_index=False
kwarg.对于数据格式等,这是另一个问题,取决于想要实现的目标.
Following the comment by @ImportanceOfBeingErnest I think the best way to answer this is to use the use_index=False
kwarg.For the data format etc, this is another problem and depends on what one wants to achieve.
import pandas as pd
import numpy as np
dates = pd.date_range('2019-01-01', '2019-01-31', freq='B')
df = pd.DataFrame(index=dates,
columns=['a', 'b', 'c'],
data = np.random.randn(len(dates), 3))
fig, ax = plt.subplots()
df.plot.bar(ax=ax)
df.sum(axis=1).plot(ax=ax, use_index=False)
这篇关于Pandas - 条形图和折线图 - 日期时间轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!