我正在尝试在 plotly 中更改堆叠的 100% 条形图的颜色。
我有以下数据框和代码:
import pandas as pd
import plotly
import plotly.express as px
from plotly.offline import plot
df = pd.DataFrame([["A","Jan","20%"],["A","Fev","10%"],"Mar","15%"],"Apr",["B",["C","25%"],"30%"],["D","40%"],"50%"],"45%"],"50%"]],columns=["type","month","percentage"])
colors = plotly.colors.qualitative.Prism
fig=px.bar(df,x='month',y='percentage',color=df['type'],barmode ='stack',text = df['percentage'])
fig.update_traces(textfont_size=12,marker_color = colors)
fig.update_layout(title = {'text': "Title",'x':0.5,'xanchor': 'center'},title_font_size=30,legend=dict(yanchor="bottom",y=0.0,xanchor="right",x=1.2),legend_font_size=16,xaxis_title = 'Months',yaxis_title ='%',xaxis_tickangle=-45,width = 1000,height = 600)
fig.show()
更改整个条形的颜色,而不是更改每个条形部分的颜色。
我想要的是把原来的蓝、红、绿、紫改成:
(这就是我在 marker_color = colors
中没有 fig.update_traces
时得到的结果,尝试根据需要更改颜色。)
我该怎么做才能正确?