<p>下面是一个不修改数据帧的解决方案。
然而,到目前为止,我还没有找到一个好的方法来显示变量名(中位数,平均值),而不显示图例。
我还修改了颜色以显示差异,但是如果您愿意,可以将颜色更改为相同的颜色</p>
<p>守则是:</p>
<pre><code>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()
</code></pre>
<p><a href="https://i.stack.imgur.com/Z1cFn.png" rel="nofollow noreferrer">enter image description here</a></p>