用两组绘制pandas数据帧

时间:2017-06-14 15:28:06

标签: python pandas matplotlib tableau

我正在使用Pandas和matplotlib尝试从tableau复制此图表:

Tableau Graph

到目前为止,我有这段代码:

BYTE *Duplication::scaleBuffer(unsigned char *data, int width, int height)
{
    Gdiplus::Bitmap bitmap(width, height, 4 * width, PixelFormat32bppARGB, data);
    INT n_width = mWidthResolution;
    INT n_height = mHeightResolution;
    double ratio = ((double)width) / ((double)height);
    if (width > height) {
        n_height = (double)n_width / ratio;
    }
    else {
        n_width = n_height * ratio;
    }
    Gdiplus::Bitmap newBitmap(n_width, n_height, bitmap.GetPixelFormat());
    Gdiplus::Graphics graphics(&newBitmap);
    graphics.SetInterpolationMode(Gdiplus::InterpolationModeBilinear);
    graphics.DrawImage(&bitmap, 0, 0, n_width, n_height);

    Gdiplus::Rect rect(0, 0, newBitmap.GetWidth(), newBitmap.GetHeight());
    Gdiplus::BitmapData bitmapData;
    BYTE *buffer = nullptr;
    if (Gdiplus::Ok == newBitmap.LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bitmapData)) {
        int len = bitmapData.Height * std::abs(bitmapData.Stride);
        buffer = new BYTE[len];
        RtlZeroMemory(buffer, len);
        memcpy(buffer, bitmapData.Scan0, len);
        newBitmap.UnlockBits(&bitmapData);
    }

    return buffer;
}

生成此图表:

enter image description here

它正确地对数据进行分组,但是可以将其分组,类似于Tableau显示的方式吗?

1 个答案:

答案 0 :(得分:3)

您可以使用相应的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()

enter image description here