英文:
Independently sized buckets for multi-faceted histogram plot
问题
在Plotly Express中:
我想要绘制多列的直方图,这些列位于不同的比例上。
我需要每个子图的桶独立于其他子图。
以下是代码部分:
import plotly.express as px
df = px.data.tips()
df['total_bill_times_100'] = df['total_bill']*100
df_plot = df.melt(value_vars=['total_bill','total_bill_times_100'])
fig = px.histogram(df_plot, x="value", facet_col='variable', template='simple_white')
fig.update_yaxes(matches=None)
fig.update_xaxes(matches=None)
fig.show()
英文:
in plotly express:
I want to plot the histograms of multiple columns, that are on different scales.
I need the buckets of each subolot to be indepent of the others.
here is the code:
import plotly.express as px
df = px.data.tips()
df['total_bill_times_100'] = df['total_bill']*100
df_plot = df.melt(value_vars = ['total_bill','total_bill_times_100' ])
fig = px.histogram(df_plot, x="value", facet_col = 'variable', template='simple_white')
fig.update_yaxes(matches=None)
fig.update_xaxes(matches=None)
fig.show()
答案1
得分: 1
使用px.histogram()
与facet_col
,分面的子图将共享相同的bingroup
> bingroup
:设置一组直方图跟踪,这些跟踪将具有兼容的bin设置[...]
...这将使它们具有相同的bin大小,即200
。
您可以将bingroup设置为None
,以便每个直方图中的bin大小都单独计算:
fig = px.histogram(df_plot, x="value", facet_col='variable', template='simple_white')
fig.update_yaxes(matches=None)
fig.update_xaxes(matches=None)
fig.update_traces(bingroup=None)
fig.show()
'total_bill'和'total_bill_times_100'的bin大小分别为2
和200
,因此分布完全相同:
如果需要进行微调,请使用plotly.graph_objects
而不是plotly.express
,请参阅Custom binning。
英文:
By using px.histogram()
with facet_col
, the facetted subplots will share the same bingroup
> bingroup
: Set a group of histogram traces which will have compatible bin
> settings [...]
... which will make them have the same bin size, that is, 200
.
You can set the bingroup to None
so that the bin size in each histogram is computed individually :
fig = px.histogram(df_plot, x="value", facet_col = 'variable', template='simple_white')
fig.update_yaxes(matches=None)
fig.update_xaxes(matches=None)
fig.update_traces(bingroup=None)
fig.show()
The bin size of 'total_bill' and 'total_bill_times_100' is now 2
and 200
respectively so the distribution is exactly the same :
If fine tuning is needed, use plotly.graph_objects
instead of plotly.express
, see Custom binning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论