问题描述
我想创建
有没有一种方法可以使用熊猫内置的绘图功能?
Is there a way to do this using pandas inbuilt plotting functionality?
我已经尝试过了:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('train.csv')
df_grouped = df.groupby(['Survived','Sex','Pclass'])['Survived'].count()
df_grouped.unstack().plot(kind='bar',stacked=True, colormap='Blues', grid=True, figsize=(13,5));
这不是我想要的.无论如何,有没有使用熊猫图制作第一个图?预先感谢
Which is not what I want. Is there anyway to produce the first plot using pandas plotting? Thanks in advance
推荐答案
生成的条形图不会像第一个图形那样彼此相邻,但是在此之外,pandas允许您执行以下操作:
The resulting bars will not neighbour each other as in your first figure, but outside of that, pandas lets you do what you want as follows:
df_g = df.groupby(['Pclass', 'Sex'])['Survived'].agg([np.mean, lambda x: 1-np.mean(x)])
df_g.columns = ['Survived', 'Died']
df_g.plot.bar(stacked=True)
在这里,斑块的水平分组由于堆叠的要求而变得复杂.例如,如果我们只关心生存"的价值,那么熊猫可以开箱即用.
Here, the horizontal grouping of patches is complicated by the requirement of stacking. If, for instance, we only cared about the value of "Survived", pandas could take care of it out-of-the-box.
df.groupby(['Pclass', 'Sex'])['Survived'].mean().unstack().plot.bar()
如果临时解决方案足以对地块进行后处理,那么这样做也不会太复杂:
If an ad hoc solution suffices for post-processing the plot, doing so is also not terribly complicated:
import numpy as np
from matplotlib import ticker
df_g = df.groupby(['Pclass', 'Sex'])['Survived'].agg([np.mean, lambda x: 1-np.mean(x)])
df_g.columns = ['Survived', 'Died']
ax = df_g.plot.bar(stacked=True)
# Move back every second patch
for i in range(6):
new_x = ax.patches[i].get_x() - (i%2)/2
ax.patches[i].set_x(new_x)
ax.patches[i+6].set_x(new_x)
# Update tick locations correspondingly
minor_tick_locs = [x.get_x()+1/4 for x in ax.patches[:6]]
major_tick_locs = np.array([x.get_x()+1/4 for x in ax.patches[:6]]).reshape(3, 2).mean(axis=1)
ax.set_xticks(minor_tick_locs, minor=True)
ax.set_xticks(major_tick_locs)
# Use indices from dataframe as tick labels
minor_tick_labels = df_g.index.levels[1][df_g.index.labels[1]].values
major_tick_labels = df_g.index.levels[0].values
ax.xaxis.set_ticklabels(minor_tick_labels, minor=True)
ax.xaxis.set_ticklabels(major_tick_labels)
# Remove ticks and organize tick labels to avoid overlap
ax.tick_params(axis='x', which='both', bottom='off')
ax.tick_params(axis='x', which='minor', rotation=45)
ax.tick_params(axis='x', which='major', pad=35, rotation=0)
这篇关于 pandas 并排堆积的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!