python - 使用辅助轴线图制作分类或分组条形图

标签 python pandas matplotlib plot seaborn

我需要使用条形图和折线图来比较 4 个类次之间的不同每日数据集(分类/分组)。我到处寻找,但没有找到不包括生成新的枢轴等的可行解决方案。

我已经使用了matplotlib和seaborn,虽然我可以选择其中之一(每个类次使用不同颜色的条/线),但一旦我合并另一个,要么一个消失,要么像只有一个一样发生其他异常情节点显示。我已经查看了所有内容,并且有一些解决方案可以在两种图表类型上表示单个数据系列,但没有一个解决方案可以进入多类别或对这两种图表类型进行分组。

数据示例:

report_date wh_id   shift   Head_Count  UTL_R
3/17/19     55  A   72  25%
3/18/19     55  A   71  10%
3/19/19     55  A   76  20%
3/20/19     55  A   59  33%
3/21/19     55  A   65  10%
3/22/19     55  A   54  20%
3/23/19     55  A   66  14%
3/17/19     55  1   11  10%
3/17/19     55  2   27  13%
3/17/19     55  3   18  25%
3/18/19     55  1   23  100%
3/18/19     55  2   16  25%
3/18/19     55  3   12  50%
3/19/19     55  1   28  10%
3/19/19     55  2   23  50%
3/19/19     55  3   14  33%
3/20/19     55  1   29  25%
3/20/19     55  2   29  25%
3/20/19     55  3   10  50%
3/21/19     55  1   17  20%
3/21/19     55  2   29  14%
3/21/19     55  3   30  17%
3/22/19     55  1   12  14%
3/22/19     55  2   10  100%
3/22/19     55  3   17  14%
3/23/19     55  1   16  10%
3/23/19     55  2   11  100%
3/23/19     55  3   13  10%
tm_daily_df = pd.read_csv('fg_TM_Daily.csv')
tm_daily_df = tm_daily_df.set_index('report_date')
fig2, ax2 = plt.subplots(figsize=(12,8))
ax3 = ax2.twinx()
group_obj = tm_daily_df.groupby('shift')
g = group_obj['Head_Count'].plot(kind='bar', x='report_date',  y='Head_Count',ax=ax2,stacked=False,alpha = .2)
g = group_obj['UTL_R'].plot(kind='line',x='report_date', y='UTL_R', ax=ax3,marker='d', markersize=12)
plt.legend(tm_daily_df['shift'].unique())

这段代码已经让我得到了我所能得到的最接近的结果。请注意,即使使用 stacked = False,它们仍然是堆叠的。我将设置更改为 True,但没有任何变化。

我所需要的只是使条形图彼此相邻,并具有代表转变的相同配色方案

图表:

graph

最佳答案

这里有两种解决方案(堆叠式和非堆叠式)。根据您的问题,我们将:

  • 在左 y 轴上绘制 Head_Count,在右 y 轴上绘制 UTL_R
  • report_date 将是我们的 x 轴
  • shift 将代表图表的色调。

堆叠版本使用 pandas 默认绘图功能,非堆叠版本使用 seaborn

编辑
根据您的要求,我添加了 100% 堆积图。虽然这与您在评论中所要求的不太完全一样,但您所要求的图形类型可能会在阅读时造成一些困惑(是基于堆栈上行或堆栈宽度的值)。另一种解决方案可能是使用 100% 堆叠图。

堆叠

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dfg = df.set_index(['report_date', 'shift']).sort_index(level=[0,1])

fig, ax = plt.subplots(figsize=(12,6))

ax2  = ax.twinx()

dfg['Head_Count'].unstack().plot.bar(stacked=True, ax=ax, alpha=0.6)
dfg['UTL_R'].unstack().plot(kind='line', ax=ax2, marker='o', legend=None)

ax.set_title('My Graph')
plt.show()

enter image description here

堆叠 100%

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dfg = df.set_index(['report_date', 'shift']).sort_index(level=[0,1])

# Create `Head_Count_Pct` column
for date in dfg.index.get_level_values('report_date').unique():
    for shift in dfg.loc[date, :].index.get_level_values('shift').unique():
        dfg.loc[(date, shift), 'Head_Count_Pct'] = dfg.loc[(date, shift), 'Head_Count'].sum() / dfg.loc[(date, 'A'), 'Head_Count'].sum()

fig, ax = plt.subplots(figsize=(12,6))

ax2  = ax.twinx()
pal = sns.color_palette("Set1")

dfg[dfg.index.get_level_values('shift').isin(['1','2','3'])]['Head_Count_Pct'].unstack().plot.bar(stacked=True, ax=ax, alpha=0.5, color=pal)
dfg['UTL_R'].unstack().plot(kind='line', ax=ax2, marker='o', legend=None, color=pal)

ax.set_title('My Graph')
plt.show()

enter image description here

未堆叠

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dfg = df.set_index(['report_date', 'shift']).sort_index(level=[0,1])

fig, ax = plt.subplots(figsize=(15,6))

ax2  = ax.twinx()

sns.barplot(x=dfg.index.get_level_values('report_date'),
            y=dfg.Head_Count,
           hue=dfg.index.get_level_values('shift'), ax=ax, alpha=0.7)

sns.lineplot(x=dfg.index.get_level_values('report_date'),
            y=dfg.UTL_R,
           hue=dfg.index.get_level_values('shift'), ax=ax2, marker='o', legend=None)

ax.set_title('My Graph')
plt.show()

enter image description here


编辑#2

这是您第二次请求的图表(堆叠,但堆栈 n+1 不是从堆栈 n 结束的地方开始)。

这有点复杂,因为我们必须做很多事情: - 我们需要手动将颜色分配给 df 中的 shift - 一旦我们分配了颜色,我们将迭代每个日期范围,并 1) 按降序排序或 Head_Count 值(以便在绘制图表时最大的麻袋位于后面),以及 2)绘制数据并为每个 stacj 分配颜色 - 然后我们可以创建第二个 y 轴并绘制我们的 UTL_R 值 - 然后我们需要为图例标签分配正确的颜色

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

def assignColor(shift):
    if shift == 'A':
        return 'R'
    if shift == '1':
        return 'B'
    if shift == '2':
        return 'G'
    if shift == '3':
        return 'Y'

# map a color to a shift
df['color'] = df['shift'].apply(assignColor)

fig, ax = plt.subplots(figsize=(15,6))

# plot our Head_Count values
for date in df.report_date.unique():
    d = df[df.report_date == date].sort_values(by='Head_Count', ascending=False)
    y = d.Head_Count.values
    x = date
    color = d.color
    b = plt.bar(x,y, color=color)

# Plot our UTL_R values
ax2 = ax.twinx()    

sns.lineplot(x=df.report_date, y=df.UTL_R, hue=df['shift'], marker='o', legend=None)

# Assign the color label color to our legend
leg = ax.legend(labels=df['shift'].unique(), loc=1)

legend_maping = dict()

for shift in df['shift'].unique():
    legend_maping[shift] = df[df['shift'] == shift].color.unique()[0]

i = 0
for leg_lab in leg.texts:
    leg.legendHandles[i].set_color(legend_maping[leg_lab.get_text()])
    i += 1

enter image description here

关于python - 使用辅助轴线图制作分类或分组条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55478428/

相关文章:

python - 独立 R 脚本加载依赖项的性能

Python 诱变剂未正确保存

python - 使用 Matplotlib 3D 轴,如何同时拖动两个轴

python - 如何使用颜色图绘制 matplotlib 线图?

python - 如何在不影响主图规模的情况下绘制平面?

python - pygame.time.set_timer() 事件重置?

python - 列表中元素的更简洁赋值(Python)?

python - 我什么时候应该在 Pandas 数据帧上使用 query vs eval?

python - np.where 代码 - ValueError : "cannot set items on DatetimeIndexResampler"

python - Pandas,将自定义函数应用于按字符串索引分组的数据