独立大小的桶用于多方面的直方图绘制

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

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()

Current output:
独立大小的桶用于多方面的直方图绘制

答案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大小分别为2200,因此分布完全相同:

独立大小的桶用于多方面的直方图绘制

如果需要进行微调,请使用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.

huangapple
  • 本文由 发表于 2023年6月19日 18:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505784.html
匿名

发表评论

匿名网友

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

确定