紫衣仙女
这里有两种解决方案(堆叠和非堆叠)。根据您的问题,我们将:Head_Count在左 y 轴和UTL_R右 y 轴上绘图。report_date 将是我们的 x 轴shift 将代表我们图形的色调。堆叠版本使用pandas默认绘图功能,非堆叠版本使用seaborn.编辑根据您的要求,我添加了一个 100% 堆叠图。虽然这与您在评论中所问的不完全相同,但您询问的图形类型可能会在阅读时造成一些混乱(是基于堆栈的上线或堆栈宽度的值)。另一种解决方案可能是使用 100% 堆叠图。堆叠import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdfg = 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()堆叠 100%import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdfg = df.set_index(['report_date', 'shift']).sort_index(level=[0,1])# Create `Head_Count_Pct` columnfor 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()未堆叠import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdfg = 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()编辑#2这是您第二次请求的图形(堆叠,但堆栈 n+1 不在堆栈 n 结束的地方开始)。它稍微涉及更多,因为我们必须做多件事: - 我们需要手动将颜色分配给我们shift的 df - 一旦我们分配了颜色,我们将遍历每个日期范围和 1)排序或Head_Count值降序(所以当我们绘制图形时,我们最大的麻袋在后面),以及 2)绘制数据并将颜色分配给每个 stacj - 然后我们可以创建第二个 y 轴并绘制我们的UTL_R值 - 然后我们需要分配正确的颜色到我们的传奇标签import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdef 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 shiftdf['color'] = df['shift'].apply(assignColor)fig, ax = plt.subplots(figsize=(15,6))# plot our Head_Count valuesfor 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 valuesax2 = 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 legendleg = 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 = 0for leg_lab in leg.texts: leg.legendHandles[i].set_color(legend_maping[leg_lab.get_text()]) i += 1
慕无忌1623718
这个怎么样?tm_daily_df['UTL_R'] = tm_daily_df['UTL_R'].str.replace('%', '').astype('float') / 100pivoted = tm_daily_df.pivot_table(values=['Head_Count', 'UTL_R'], index='report_date', columns='shift')pivoted# Head_Count UTL_R# shift 1 2 3 A 1 2 3 A# report_date# 3/17/19 11 27 18 72 0.10 0.13 0.25 0.25# 3/18/19 23 16 12 71 1.00 0.25 0.50 0.10# 3/19/19 28 23 14 76 0.10 0.50 0.33 0.20# 3/20/19 29 29 10 59 0.25 0.25 0.50 0.33# 3/21/19 17 29 30 65 0.20 0.14 0.17 0.10# 3/22/19 12 10 17 54 0.14 1.00 0.14 0.20# 3/23/19 16 11 13 66 0.10 1.00 0.10 0.14fig, ax = plt.subplots()pivoted['Head_Count'].plot.bar(ax=ax)pivoted['UTL_R'].plot.line(ax=ax, legend=False, secondary_y=True, marker='D')ax.legend(loc='upper left', title='shift')