以绘图方式合并两个动画条形图

2024-05-12 23:16:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个分组条形图,显示每组的中位数和平均值。我还使用了一个动画帧来显示每个中位数和平均值是如何根据ubi量变化的

我想对这些条形图进行分组,以便显示每组的中值和平均值,以及它们是如何变化的

这是我的代码,但我被困在如何分组他们

import pandas as pd
import plotly.express as px

summary_med = pd.read_csv('https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means')

fig = px.bar(summary_med, x = 'race', y='med_resources_per_person',
         animation_frame='monthly_ubi', range_y=[0,25_000],
         labels={
                 "med_resources_per_person": "Median resources per person",
                 "race": "Race",
                 "monthly_ubi": "Monthly UBI",
                 "native": "Native",
                 "non_native": "Non-native"
             },
            title="Tax funded UBI and median resources per person", 
         color='race', 
         text='med_resources_per_person',
         height=900, width=800,
         color_discrete_map={'native': '#5886a5',
                             'non_native': '#5886a5'})

fig.update_traces(texttemplate='$%{text}')
fig.update_layout(showlegend=False, yaxis_tickprefix='$')
fig.show()

Tags: importasfigmedperson平均值pdresources
2条回答

下面是一个不修改数据帧的解决方案。 然而,到目前为止,我还没有找到一个好的方法来显示变量名(中位数,平均值),而不显示图例。 我还修改了颜色以显示差异,但是如果您愿意,可以将颜色更改为相同的颜色

守则是:

import pandas as pd 
import plotly.express as px


summary_med = pd.read_csv('https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means')

fig = px.bar(summary_med, x="race", y=["med_resources_per_person","mean_resources_per_person"],
             # color='race', 
             animation_frame='monthly_ubi', 
             range_y=[0,50_000],
             barmode='group', # make it side by side 
             # height=400
              labels={
                 "med_resources_per_person": "Median",
                 "mean_resources_per_person":"Mean",
                 "race": "Race",
                 "monthly_ubi": "Monthly UBI",
                 "native": "Native",
                 "non_native": "Non-native"
             },
            title="Tax funded UBI and median resources per person", 

            height=900, width=800,
            color_discrete_map={'med_resources_per_person': '#5886a5','mean_resources_per_person': '#58a577'}

            ) 
fig.update_traces(texttemplate='%{y}') 
fig.update_layout(showlegend=True, yaxis_tickprefix='$') 
fig.show()

enter image description here

我不确定这是否是你想要的

import pandas as pd
import plotly.express as px

url = 'https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means'
df = pd.read_csv(url)

# wide to long
df = pd.melt(df,
        id_vars=["monthly_ubi", "race"],
        value_vars=['med_resources_per_person',
                    'mean_resources_per_person'],
        var_name="resource",
             
        value_name="y")


# Format text
diz_resource = {"med_resources_per_person": "Median",
                "mean_resources_per_person": "Mean"}
diz_race = {"native": "Native",
            "non_native": "Non-native"}

df["resource"] = df["resource"].map(diz_resource)
df["race"] =  df["race"].map(diz_race)

# Range max
range_max = df["y"].max() * 1.2

# Plot
fig = px.bar(df, 
       x='race',
       y="y",
       color="resource",
       barmode='group',
       animation_frame='monthly_ubi',
       text='y',
       height=900, width=800,
       labels={"race": "Race",
               "monthly_ubi": "Monthly UBI",
               "y": "Resource Per Person"
             },
       title="Tax funded UBI and median resources per person",
       range_y=[0, range_max]
    )
fig.update_traces(texttemplate='$ %{text}')
fig.update_layout(title_x=0.5,
#                   showlegend=False,
                  yaxis_tickprefix='$')
fig.show()

enter image description here

相关问题 更多 >