<p>我不确定这是否是你想要的</p>
<pre class="lang-py prettyprint-override"><code>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()
</code></pre>
<p><a href="https://i.stack.imgur.com/yjxKV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yjxKV.png" alt="enter image description here"/></a></p>