我用plotly.express制作了这些附加的组条:

我的代码是这样的:
import pandas as pd
df=pd.DataFrame([['C-1','C-11','111'],['C-1','C-12','121'],['C-1','C-13','31'],['C-1','C-14','120'],['C-1','C-15','100'],
['C-2','C-21','150'],['C-2','C-22','168'],['C-2','C-23','173'],['C-2','C-24','111'],['C-2','C-25','121'],
['C-3','C-31','123'],['C-3','C-32','100'],['C-3','C-33','111'],['C-3','C-34','111'],['C-3','C-35','163'],
['C-4','C-41','174'],['C-4','C-42','174'],['C-4','C-43','173'],['C-4','C-44','135'],['C-4','C-45','102'],
['C-5','C-51','118'],['C-5','C-52','122'],['C-5','C-53','113'],['C-5','C-54','178'],['C-5','C-55','142']
],columns=['Case group','Case number','Average revenue'])
import plotly.express as px
fig = px.bar(df,
x='Case number',y='Average revenue',
color='Case group', barmode='group',text="Average revenue",
height=500,width=700,
color_discrete_sequence=[px.colors.qualitative.Dark2[0],px.colors.qualitative.Set2[1],px.colors.qualitative.Pastel[0],px.colors.qualitative.Set2[5],px.colors.qualitative.Set2[4],])
)
fig.update_traces(textposition='outside')
fig.show()我的问题是,如何使y轴标签正常上升,使所有组共享相同的y轴标度?在我的理想思想中,C-55应该低于C-54。此外,每个栏上的注释太小,无法读取。我尝试用fig.update.layout()更改字体大小。但没起作用。
如果我做错了什么,请告诉我。
我非常感谢你能提供的任何帮助。
发布于 2022-10-20 11:01:50
您只需要将列类型转换为float,如下所示:
df["Average revenue"] = df["Average revenue"].astype(float)全文代码:
import pandas as pd
import plotly.express as px
df=pd.DataFrame([['C-1','C-11','111'],['C-1','C-12','121'],['C-1','C-13','31'],['C-1','C-14','120'],['C-1','C-15','100'],
['C-2','C-21','150'],['C-2','C-22','168'],['C-2','C-23','173'],['C-2','C-24','111'],['C-2','C-25','121'],
['C-3','C-31','123'],['C-3','C-32','100'],['C-3','C-33','111'],['C-3','C-34','111'],['C-3','C-35','163'],
['C-4','C-41','174'],['C-4','C-42','174'],['C-4','C-43','173'],['C-4','C-44','135'],['C-4','C-45','102'],
['C-5','C-51','118'],['C-5','C-52','122'],['C-5','C-53','113'],['C-5','C-54','178'],['C-5','C-55','142']
],columns=['Case group','Case number','Average revenue'])
df["Average revenue"] = df["Average revenue"].astype(float)
fig = px.bar(df,
x="Case number",y='Average revenue',
color='Case group', barmode='group',text="Average revenue",
height=500,width=700,
color_discrete_sequence=[px.colors.qualitative.Dark2[0],px.colors.qualitative.Set2[1],px.colors.qualitative.Pastel[0],px.colors.qualitative.Set2[5],px.colors.qualitative.Set2[4],])
fig.update_traces(textposition='outside', width=0.8)
fig.show()输出

https://stackoverflow.com/questions/74138589
复制相似问题