简体   繁体   English

用两组绘制pandas数据帧

[英]Plotting pandas dataframe with two groups

I'm using Pandas and matplotlib to try to replicate this graph from tableau: 我正在使用Pandas和matplotlib尝试从tableau复制此图:

Tableau图

So far, I have this code: 到目前为止,我有这个代码:

group = df.groupby(["Region","Rep"]).sum()
total_price = group["Total Price"].groupby(level=0, group_keys=False)
total_price.nlargest(5).plot(kind="bar")

Which produces this graph: 这产生了这个图:

在此输入图像描述

It correctly groups the data, but is it possible to get it grouped similar to how Tableau shows it? 它正确地对数据进行分组,但是可以将其分组,类似于Tableau显示的方式吗?

You can create some lines and labels using the respective matplotlib methods ( ax.text and ax.axhline ). 您可以使用相应的matplotlib方法( ax.textax.axhline )创建一些线条和标签。

import pandas as pd
import numpy as np; np.random.seed(5)
import matplotlib.pyplot as plt

a = ["West"]*25+ ["Central"]*10+ ["East"]*10
b = ["Mattz","McDon","Jeffs","Warf","Utter"]*5 + ["Susanne","Lokomop"]*5 + ["Richie","Florence"]*5
c = np.random.randint(5,55, size=len(a))
df=pd.DataFrame({"Region":a, "Rep":b, "Total Price":c})


group = df.groupby(["Region","Rep"]).sum()
total_price = group["Total Price"].groupby(level=0, group_keys=False)

gtp = total_price.nlargest(5)
ax = gtp.plot(kind="bar")

#draw lines and titles
count = gtp.groupby("Region").count()
cum = np.cumsum(count)
for i in range(len(count)):
    title = count.index.values[i]
    ax.axvline(cum[i]-.5, lw=0.8, color="k")
    ax.text(cum[i]-(count[i]+1)/2., 1.02, title, ha="center",
            transform=ax.get_xaxis_transform())

# shorten xticklabels
ax.set_xticklabels([l.get_text().split(", ")[1][:-1] for l in ax.get_xticklabels()])

plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM