不完整地表达散点图不允许对分类标签进行排序

问题描述

使用边际绘图时,设置x轴的类别顺序似乎会使px.scatter图形崩溃

import pandas as pd
import plotly.express as px # plotly 4.9.0

df = pd.DataFrame({'x':['c','a','b'],'y':[1,2,1]})
fig = px.scatter(df,x='x',y='y',marginal_y='Box')
fig.update_xaxes(categoryorder='category ascending') # Line x
fig.show(renderer='browser')
# Only works,if line x is removed,or when parameter marginal_y='Box' is removed

这是错误还是我错过了什么?

解决方法

尝试更新布局

import pandas as pd
import plotly.express as px # plotly 4.9.0

df = pd.DataFrame({'x':['c','a','b'],'y':[1,2,1]})
fig = px.scatter(df,x='x',y='y',marginal_y='box')
fig.update_layout(xaxis=dict(type='category',categoryorder='category ascending'))
fig.show()