gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:19 25 4
gpt4 key购买 nike

我需要使用条形图和折线图比较 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com