按数据总数缩放的直方图
Plotly Histogram scaled by the total number of data
我想比较两个数据集的值,但是我的一个数据集包含的数据比另一个多很多,我想根据数据的大小对直方图进行归一化。我在下面留下一个代码示例。
import numpy as np
import pandas as pd
import plotly.graph_objects as go
df_rand_A = pd.DataFrame(np.random.randn(200, 1), columns=['value'])
df_rand_A['kind'] = 'A'
df_rand_B = pd.DataFrame(np.random.randn(2000, 1), columns=['value'])
df_rand_B['kind'] = 'B'
df_rand = pd.concat([df_rand_A, df_rand_B])
fig = go.Figure()
df_grouped = df_rand.groupby('kind')
for kind, group in df_grouped:
fig.add_trace(go.Histogram(x=group.value, name=kind, showlegend=True, bingroup=1))
fig.update_traces(opacity=0.75)
fig.update_layout(barmode="overlay", bargap=0, margin=dict(l=20, r=20, t=20, b=20), font=dict(size=13), width=800, height=500,legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right",x=1))
fig.show()
使用 plotly distplot 绘制分布图 - https://plotly.com/python/distplot/
或者,使用 np.histogram
计算直方图分布,并将条形的值除以直方图中值的总和以获得归一化直方图。
import plotly.graph_objects as go
import numpy as np
import plotly.io as pio
pio.renderers.default='browser'
data1 = np.random.normal(0, 1, 100000)
data2 = np.random.normal(0, 1, 1000000)
hist1 = np.histogram(data1)
hist2 = np.histogram(data2)
fig = go.Figure()
fig.add_trace(go.Bar(x=hist1[1], y = hist1[0]/sum(hist1[0])))
fig.add_trace(go.Bar(x=hist2[1], y = hist2[0]/sum(hist2[0])))
fig.show()
结果:
我想比较两个数据集的值,但是我的一个数据集包含的数据比另一个多很多,我想根据数据的大小对直方图进行归一化。我在下面留下一个代码示例。
import numpy as np
import pandas as pd
import plotly.graph_objects as go
df_rand_A = pd.DataFrame(np.random.randn(200, 1), columns=['value'])
df_rand_A['kind'] = 'A'
df_rand_B = pd.DataFrame(np.random.randn(2000, 1), columns=['value'])
df_rand_B['kind'] = 'B'
df_rand = pd.concat([df_rand_A, df_rand_B])
fig = go.Figure()
df_grouped = df_rand.groupby('kind')
for kind, group in df_grouped:
fig.add_trace(go.Histogram(x=group.value, name=kind, showlegend=True, bingroup=1))
fig.update_traces(opacity=0.75)
fig.update_layout(barmode="overlay", bargap=0, margin=dict(l=20, r=20, t=20, b=20), font=dict(size=13), width=800, height=500,legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right",x=1))
fig.show()
使用 plotly distplot 绘制分布图 - https://plotly.com/python/distplot/
或者,使用 np.histogram
计算直方图分布,并将条形的值除以直方图中值的总和以获得归一化直方图。
import plotly.graph_objects as go
import numpy as np
import plotly.io as pio
pio.renderers.default='browser'
data1 = np.random.normal(0, 1, 100000)
data2 = np.random.normal(0, 1, 1000000)
hist1 = np.histogram(data1)
hist2 = np.histogram(data2)
fig = go.Figure()
fig.add_trace(go.Bar(x=hist1[1], y = hist1[0]/sum(hist1[0])))
fig.add_trace(go.Bar(x=hist2[1], y = hist2[0]/sum(hist2[0])))
fig.show()
结果: