无法使用pandas plot()函数组合条形图和折线图 [英] Cannot combine bar and line plot using pandas plot() function
本文介绍了无法使用pandas plot()函数组合条形图和折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用plot()绘制熊猫数据框的一列作为线图:
I am plotting one column of a pandas dataframe as line plot, using plot() :
df.iloc[:,1].plot()
并获得所需的结果:
现在我想使用条形图绘制同一数据框的另一列
Now I want to plot another column of the same dataframe as bar chart using
ax=df.iloc[:,3].plot(kind='bar',width=1)
结果:
最后我想将两者结合起来
And finally I want to combine both by
spy_price_data.iloc[:,1].plot(ax=ax)
不会产生任何情节.
为什么条形图的x-ticks与折线图的x-ticks如此不同?如何将两个图合并成一个图?
Why are the x-ticks of the bar plot so different to the x-ticks of the line plot? How can I combine both plots in one plot?
推荐答案
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
一些数据
df = pd.DataFrame(np.random.randn(5,2))
print (df)
0 1
0 0.008177 -0.121644
1 0.643535 -0.070786
2 -0.104024 0.872997
3 -0.033835 0.067264
4 -0.576762 0.571293
然后创建一个轴对象(ax).请注意,我们将斧头传递给了两个图
then we create an axes object (ax). Notice that we pass ax to both plots
_, ax = plt.subplots()
df[0].plot(ax=ax)
df[1].plot(kind='bar', ax=ax)
这篇关于无法使用pandas plot()函数组合条形图和折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文