英文:
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.
答案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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论