仅在多面条形图中使用Plotly显示每个分面的现有x轴数值。

huangapple go评论55阅读模式
英文:

Display only existing x-axis values for each facet in a multi-faceted bar plot using plotly

问题

对于每个子图我只想显示数据中存在的x刻度
英文:

For the following multifacet plot



df = pd.DataFrame({
    'row': [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
    'col': [0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1 ],
    'x_value': [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4], 
    'count':  [1,7,4,0,0,3,1,3,1,9,2,2,0,0,3,4]


})

df = df.query('count != 0 ')

fig = px.bar(df, x='x_value', y='count', facet_col='col', facet_row='row', template='simple_white')
fig.for_each_xaxis(lambda xaxis: xaxis.update(showticklabels=True, title_font = dict(size =20), type = 'category'))


fig.show()

for each subplot, i want to show ONLY the x-ticks present in the data.

The circled ticks should not be showing:
仅在多面条形图中使用Plotly显示每个分面的现有x轴数值。

答案1

得分: 2

要获得您要查找的内容,您需要自定义每个x轴(如果需要,还包括y轴),以包含matches=None。这将停止尝试在子图的每个部分之间匹配列。因此,请将以下行替换为:

fig.for_each_xaxis(lambda xaxis: xaxis.update(showticklabels=True, matches=None, title_font=dict(size=20), type='category'))

然后您将获得下面的图表。希望这是您要查找的内容。

英文:

To get what you are looking for, you need to customize each x-axis (and y-axis, if required) to include matches=None. This will stop trying to match the columns across each of the subplots. So, replace the line...

fig.for_each_xaxis(lambda xaxis: xaxis.update(showticklabels=True, title_font = dict(size =20), type = 'category'))

with

fig.for_each_xaxis(lambda xaxis: xaxis.update(showticklabels=True, matches=None, title_font = dict(size =20), type = 'category'))

and you will get below plot. Hope this is what you are looking for...

仅在多面条形图中使用Plotly显示每个分面的现有x轴数值。

huangapple
  • 本文由 发表于 2023年6月12日 16:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定