Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两个人物与熊猫合并_Python_Pandas_Matplotlib - Fatal编程技术网

Python 将两个人物与熊猫合并

Python 将两个人物与熊猫合并,python,pandas,matplotlib,Python,Pandas,Matplotlib,我已将数据帧df绘制为堆叠条形图: dft['Date'] = old['DATE'].dt.to_period('M') df = dft.groupby(by=['Date', 'Size'])['Spend'].sum() stacked = df.unstack('Size').plot(kind='bar', stacked=True) 这个堆积条形图每个月都有一个条形图 我有另一个带有季度数据的数据帧cir(每3个月一次)。我需要这个数据作为一个涵盖所有3个月的条形图,背景是主

我已将数据帧df绘制为堆叠条形图:

dft['Date'] = old['DATE'].dt.to_period('M')
df = dft.groupby(by=['Date', 'Size'])['Spend'].sum()  
stacked = df.unstack('Size').plot(kind='bar', stacked=True)
这个堆积条形图每个月都有一个条形图

我有另一个带有季度数据的数据帧cir(每3个月一次)。我需要这个数据作为一个涵盖所有3个月的条形图,背景是主堆叠条形图。以下代码单独绘制季度数据

cir['date'] = ['3-15','4-15', '1-16', '2-16', '3-16', '4-16', '1-17', '2- 
17', '3-17', '4-17']
cir['value'] = [78.4, 13.5, 19.8, 19, 78.4, 123.2, 82.6, 86.4, 83.5, 12.2.4]
cir.plot.bar()

我想叠加这两个图(它们有不同的y轴)。我尝试过使用子代码块功能,但实际上没有取得任何进展。

这是比实际代码更多的伪代码,因为您没有提供太多信息或您编写的代码。不过,这可以为您提供一些关于如何将
pandas.DataFrame.plot
matplotlib.pyplot.bar
与不同y轴组合的提示

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

width = 1
fig, ax1 = plt.subplots()
value_x = np.arange(cir['value'])
ax1.bar(x, cir['value'], width = 3*width)
ax2 = ax1.twinx()
df.unstack('Size').plot(kind='bar', stacked=True, ax=ax2, secondary_y=True, width=width)

plt.show()
请注意,诀窍是使用
ax2=ax1.twinx()
并在不同的轴上绘制序列


关于
宽度
,我不确定这是否可行,因为我不知道你的
x轴是什么。但这只是给你一个如何做的想法。

你能展示你当前的情节吗?谢谢,这应该是非常有用的信息。注意,我现在添加了一些关于轴的更多信息。要添加错误,只需在ax1.bar中使用
yerr=the_error
(x,cir['value'],width=3*width,yerr=the_error)
。您可以使用
alpha=0.5
ax1.bar(x,cir['value'],width=3*width,alpha=0.5)
。如果有帮助的话,请考虑接受答案,如果你有新的问题,请创建新的帖子: