亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly 合并兩個動畫條形圖

Plotly 合并兩個動畫條形圖

www說 2023-10-11 16:23:49
我想創建一個分組條形圖,顯示每組的中位數和平均值。我還使用動畫幀來顯示每個中位數和均值如何根據 ubi 數量而變化。我想對這些條形圖進行分組,以便它顯示每個組的中位數和平均值及其變化方式。這是我的代碼,但我不知道如何對它們進行分組。import pandas as pdimport plotly.express as pxsummary_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()
查看完整描述

2 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

我不確定這是否是您所要求的


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()

https://img1.sycdn.imooc.com/65265bda0001022206520736.jpg

查看完整回答
反對 回復 2023-10-11
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

這是一個不修改數據框的解決方案。但到目前為止,我還沒有找到一個好的方法來顯示沒有圖例的變量名稱(中位數、平均值)。我還修改了顏色以顯示差異,但如果您愿意,可以將顏色更改為相同。


代碼是:


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()

查看完整回答
反對 回復 2023-10-11
  • 2 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號