Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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_Bar Chart_Seaborn - Fatal编程技术网

Python 比较两列的条形图

Python 比较两列的条形图,python,pandas,bar-chart,seaborn,Python,Pandas,Bar Chart,Seaborn,我想画一个条形图,比较每月时间轴(12个月的发票)上两个收入变量的演变 我想使用sns.barplot,但不能使用“色调”(因为这两个变量不是子类别?)。有没有其他方法,像色调一样简单?我能“创造”一种色调吗 以下是我的一个小样本数据: (我确实将我的表转换为透视表) [In] data_pivot['Revenue-Small-Seller-in'] = data_pivot["Small-Seller"] + data_pivot["Best-Seller"] + data_pivot["M

我想画一个条形图,比较每月时间轴(12个月的发票)上两个收入变量的演变

我想使用sns.barplot,但不能使用“色调”(因为这两个变量不是子类别?)。有没有其他方法,像色调一样简单?我能“创造”一种色调吗

以下是我的一个小样本数据:

(我确实将我的表转换为透视表)

[In]

data_pivot['Revenue-Small-Seller-in'] = data_pivot["Small-Seller"] + data_pivot["Best-Seller"] + data_pivot["Medium-Seller"]
data_pivot['Revenue-Not-Small-Seller-in'] = data_pivot["Best-Seller"] + data_pivot["Medium-Seller"]
data_pivot
[Out]

InvoiceNo   Month   Year    Revenue-Small-Seller-in Revenue-Not-Small-Seller-in
 536365       12    2010             139.12                   139.12
 536366       12    2010              22.20                    11.10
 536367       12    2010             278.73                   246.93

您可以执行以下操作:

render_df = data_pivot[data_pivot.columns[-2:]]
fig, ax = plt.subplots(1,1)
render_df.plot(kind='bar', ax=ax)
ax.legend()
plt.show()
输出:

或者像您要求的那样使用sns样式

render_df = data_pivot[data_pivot.columns[-2:]].stack().reset_index()
sns.barplot('level_0', 0, hue='level_1',
            render_df)
stack()之后的
render_df
是:

+---+---------+-----------------------------+--------+
|   | level_0 |           level_1           |   0    |
+---+---------+-----------------------------+--------+
| 0 |       0 | Revenue-Small-Seller-in     | 139.12 |
| 1 |       0 | Revenue-Not-Small-Seller-in | 139.12 |
| 2 |       1 | Revenue-Small-Seller-in     | 22.20  |
| 3 |       1 | Revenue-Not-Small-Seller-in | 11.10  |
| 4 |       2 | Revenue-Small-Seller-in     | 278.73 |
| 5 |       2 | Revenue-Not-Small-Seller-in | 246.93 |
+---+---------+-----------------------------+--------+
和输出:


我没有真正使用“级别”s。另外,我有超过我显示的5列(简单起见,但也许我应该键入完整的表)。我只是添加了一张完整透视表的图片(我知道这并不容易,抱歉)@LolaVanpump它们是在双索引上重置索引的结果。请参阅编辑,希望有帮助。