文章目录
- 4.2 利用 go.Histogram 的直方图
- 4.2.1 基本直方图
- 4.2.2 归一化直方图
- 4.2.3 水平直方图
- 4.2.4 叠加直方图
- 4.2.5 堆叠直方图
- 4.2.6 风格直方图
- 4.2.7 直方图条形文本
- 4.2.8 累积直方图
- 4.2.9 指定聚合函数
- 4.2.10 自定义分箱
- 4.2.11 在直方图之间共享 bin
- 4.2.12 按类别顺序排序直方图
- 4.2.13 另请参阅:条形图
4.2 利用 go.Histogram 的直方图
4.2.1 基本直方图
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
x = np.random.randn(500)
print(x)
'''
[ 1.62434536e+00 -6.11756414e-01 -5.28171752e-01 -1.07296862e+00
8.65407629e-01 -2.30153870e+00 1.74481176e+00 -7.61206901e-01
3.19039096e-01 -2.49370375e-01 1.46210794e+00 -2.06014071e+00
-3.22417204e-01 -3.84054355e-01 1.13376944e+00 -1.09989127e+00
-1.72428208e-01 -8.77858418e-01 4.22137467e-02 5.82815214e-01
-1.10061918e+00 1.14472371e+00 9.01590721e-01 5.02494339e-01
9.00855949e-01 -6.83727859e-01 -1.22890226e-01 -9.35769434e-01
-2.67888080e-01 5.30355467e-01 -6.91660752e-01 -3.96753527e-01
-6.87172700e-01 -8.45205641e-01 -6.71246131e-01 -1.26645989e-02
-1.11731035e+00 2.34415698e-01 1.65980218e+00 7.42044161e-01
-1.91835552e-01 -8.87628964e-01 -7.47158294e-01 1.69245460e+00
5.08077548e-02 -6.36995647e-01 1.90915485e-01 2.10025514e+00
1.20158952e-01 6.17203110e-01 3.00170320e-01 -3.52249846e-01
-1.14251820e+00 -3.49342722e-01 -2.08894233e-01 5.86623191e-01
8.38983414e-01 9.31102081e-01 2.85587325e-01 8.85141164e-01
-7.54397941e-01 1.25286816e+00 5.12929820e-01 -2.98092835e-01
4.88518147e-01 -7.55717130e-02 1.13162939e+00 1.51981682e+00
2.18557541e+00 -1.39649634e+00 -1.44411381e+00 -5.04465863e-01
1.60037069e-01 8.76168921e-01 3.15634947e-01 -2.02220122e+00
-3.06204013e-01 8.27974643e-01 2.30094735e-01 7.62011180e-01
-2.22328143e-01 -2.00758069e-01 1.86561391e-01 4.10051647e-01
1.98299720e-01 1.19008646e-01 -6.70662286e-01 3.77563786e-01
1.21821271e-01 1.12948391e+00 1.19891788e+00 1.85156417e-01
-3.75284950e-01 -6.38730407e-01 4.23494354e-01 7.73400683e-02
-3.43853676e-01 4.35968568e-02 -6.20000844e-01 6.98032034e-01
-4.47128565e-01 1.22450770e+00 4.03491642e-01 5.93578523e-01
-1.09491185e+00 1.69382433e-01 7.40556451e-01 -9.53700602e-01
-2.66218506e-01 3.26145467e-02 -1.37311732e+00 3.15159392e-01
8.46160648e-01 -8.59515941e-01 3.50545979e-01 -1.31228341e+00
-3.86955093e-02 -1.61577235e+00 1.12141771e+00 4.08900538e-01
-2.46169559e-02 -7.75161619e-01 1.27375593e+00 1.96710175e+00
-1.85798186e+00 1.23616403e+00 1.62765075e+00 3.38011697e-01
-1.19926803e+00 8.63345318e-01 -1.80920302e-01 -6.03920628e-01
-1.23005814e+00 5.50537496e-01 7.92806866e-01 -6.23530730e-01
5.20576337e-01 -1.14434139e+00 8.01861032e-01 4.65672984e-02
-1.86569772e-01 -1.01745873e-01 8.68886157e-01 7.50411640e-01
5.29465324e-01 1.37701210e-01 7.78211279e-02 6.18380262e-01
2.32494559e-01 6.82551407e-01 -3.10116774e-01 -2.43483776e+00
1.03882460e+00 2.18697965e+00 4.41364444e-01 -1.00155233e-01
-1.36444744e-01 -1.19054188e-01 1.74094083e-02 -1.12201873e+00
-5.17094458e-01 -9.97026828e-01 2.48799161e-01 -2.96641152e-01
4.95211324e-01 -1.74703160e-01 9.86335188e-01 2.13533901e-01
2.19069973e+00 -1.89636092e+00 -6.46916688e-01 9.01486892e-01
2.52832571e+00 -2.48634778e-01 4.36689932e-02 -2.26314243e-01
1.33145711e+00 -2.87307863e-01 6.80069840e-01 -3.19801599e-01
-1.27255876e+00 3.13547720e-01 5.03184813e-01 1.29322588e+00
-1.10447026e-01 -6.17362064e-01 5.62761097e-01 2.40737092e-01
2.80665077e-01 -7.31127037e-02 1.16033857e+00 3.69492716e-01
1.90465871e+00 1.11105670e+00 6.59049796e-01 -1.62743834e+00
6.02319280e-01 4.20282204e-01 8.10951673e-01 1.04444209e+00
-4.00878192e-01 8.24005618e-01 -5.62305431e-01 1.95487808e+00
-1.33195167e+00 -1.76068856e+00 -1.65072127e+00 -8.90555584e-01
-1.11911540e+00 1.95607890e+00 -3.26499498e-01 -1.34267579e+00
1.11438298e+00 -5.86523939e-01 -1.23685338e+00 8.75838928e-01
6.23362177e-01 -4.34956683e-01 1.40754000e+00 1.29101580e-01
1.61694960e+00 5.02740882e-01 1.55880554e+00 1.09402696e-01
-1.21974440e+00 2.44936865e+00 -5.45774168e-01 -1.98837863e-01
-7.00398505e-01 -2.03394449e-01 2.42669441e-01 2.01830179e-01
6.61020288e-01 1.79215821e+00 -1.20464572e-01 -1.23312074e+00
-1.18231813e+00 -6.65754518e-01 -1.67419581e+00 8.25029824e-01
-4.98213564e-01 -3.10984978e-01 -1.89148284e-03 -1.39662042e+00
-8.61316361e-01 6.74711526e-01 6.18539131e-01 -4.43171931e-01
1.81053491e+00 -1.30572692e+00 -3.44987210e-01 -2.30839743e-01
-2.79308500e+00 1.93752881e+00 3.66332015e-01 -1.04458938e+00
2.05117344e+00 5.85662000e-01 4.29526140e-01 -6.06998398e-01
1.06222724e-01 -1.52568032e+00 7.95026094e-01 -3.74438319e-01
1.34048197e-01 1.20205486e+00 2.84748111e-01 2.62467445e-01
2.76499305e-01 -7.33271604e-01 8.36004719e-01 1.54335911e+00
7.58805660e-01 8.84908814e-01 -8.77281519e-01 -8.67787223e-01
-1.44087602e+00 1.23225307e+00 -2.54179868e-01 1.39984394e+00
-7.81911683e-01 -4.37508983e-01 9.54250872e-02 9.21450069e-01
6.07501958e-02 2.11124755e-01 1.65275673e-02 1.77187720e-01
-1.11647002e+00 8.09271010e-02 -1.86578994e-01 -5.68244809e-02
4.92336556e-01 -6.80678141e-01 -8.45080274e-02 -2.97361883e-01
4.17302005e-01 7.84770651e-01 -9.55425262e-01 5.85910431e-01
2.06578332e+00 -1.47115693e+00 -8.30171895e-01 -8.80577600e-01
-2.79097722e-01 1.62284909e+00 1.33526763e-02 -6.94693595e-01
6.21803504e-01 -5.99804531e-01 1.12341216e+00 3.05267040e-01
1.38877940e+00 -6.61344243e-01 3.03085711e+00 8.24584625e-01
6.54580153e-01 -5.11884476e-02 -7.25597119e-01 -8.67768678e-01
-1.35977326e-01 -7.97269785e-01 2.82675712e-01 -8.26097432e-01
6.21082701e-01 9.56121704e-01 -7.05840507e-01 1.19268607e+00
-2.37941936e-01 1.15528789e+00 4.38166347e-01 1.12232832e+00
-9.97019796e-01 -1.06793987e-01 1.45142926e+00 -6.18036848e-01
-2.03720123e+00 -1.94258918e+00 -2.50644065e+00 -2.11416392e+00
-4.11639163e-01 1.27852808e+00 -4.42229280e-01 3.23527354e-01
-1.09991490e-01 8.54894544e-03 -1.68198840e-01 -1.74180344e-01
4.61164100e-01 -1.17598267e+00 1.01012718e+00 9.20017933e-01
-1.95057341e-01 8.05393424e-01 -7.01344426e-01 -5.37223024e-01
1.56263850e-01 -1.90221025e-01 -4.48738033e-01 -6.72448039e-01
-5.57494722e-01 9.39168744e-01 -1.94332341e+00 3.52494364e-01
-2.36436952e-01 7.27813500e-01 5.15073614e-01 -2.78253447e+00
5.84646610e-01 3.24274243e-01 2.18628366e-02 -4.68673816e-01
8.53281222e-01 -4.13029310e-01 1.83471763e+00 5.64382855e-01
2.13782807e+00 -7.85533997e-01 -1.75592564e+00 7.14789597e-01
8.52704062e-01 3.53600971e-02 -1.53879325e+00 -4.47895185e-01
6.17985534e-01 -1.84176326e-01 -1.15985185e-01 -1.75458969e-01
-9.33914656e-01 -5.33020326e-01 -1.42655542e+00 1.76795995e+00
-4.75372875e-01 4.77610182e-01 -1.02188594e+00 7.94528240e-01
-1.87316098e+00 9.20615118e-01 -3.53679249e-02 2.11060505e+00
-1.30653407e+00 7.63804802e-02 3.67231814e-01 1.23289919e+00
-4.22856961e-01 8.64644065e-02 -2.14246673e+00 -8.30168864e-01
4.51615951e-01 1.10417433e+00 -2.81736269e-01 2.05635552e+00
1.76024923e+00 -6.06524918e-02 -2.41350300e+00 -1.77756638e+00
-7.77858827e-01 1.11584111e+00 3.10272288e-01 -2.09424782e+00
-2.28765829e-01 1.61336137e+00 -3.74804687e-01 -7.49969617e-01
2.05462410e+00 5.34095368e-02 -4.79157099e-01 3.50167159e-01
1.71647264e-02 -4.29142278e-01 1.20845633e+00 1.11570180e+00
8.40861558e-01 -1.02887218e-01 1.14690038e+00 -4.97025792e-02
4.66643267e-01 1.03368687e+00 8.08844360e-01 1.78975468e+00
4.51284016e-01 -1.68405999e+00 -1.16017010e+00 1.35010682e+00
-3.31283170e-01 3.86539145e-01 -8.51455657e-01 1.00088142e+00
-3.84832249e-01 1.45810824e+00 -5.32234021e-01 1.11813340e+00
6.74396105e-01 -7.22391905e-01 1.09899633e+00 -9.01634490e-01
-8.22467189e-01 7.21711292e-01 -6.25342001e-01 -5.93843067e-01
-3.43900709e-01 -1.00016919e+00 1.04499441e+00 6.08514698e-01
-6.93286967e-02 -1.08392067e-01 4.50155513e-01 1.76533510e+00
8.70969803e-01 -5.08457134e-01 7.77419205e-01 -1.18771172e-01
-1.98998184e-01 1.86647138e+00 -4.18937898e-01 -4.79184915e-01
-1.95210529e+00 -1.40232915e+00 4.51122939e-01 -6.94920901e-01
5.15413802e-01 -1.11487105e+00 -7.67309826e-01 6.74570707e-01
1.46089238e+00 5.92472801e-01 1.19783084e+00 1.70459417e+00
1.04008915e+00 -9.18440038e-01 -1.05344713e-01 6.30195671e-01
-4.14846901e-01 4.51946037e-01 -1.57915629e+00 -8.28627979e-01
5.28879746e-01 -2.23708651e+00 -1.10771250e+00 -1.77183179e-02]
'''
fig = go.Figure(data=[go.Histogram(x=x)])
fig.show()
4.2.2 归一化直方图
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(x=x, histnorm='probability')])
fig.show()
4.2.3 水平直方图
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(y=x)])
fig.show()
4.2.4 叠加直方图
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
x0 = np.random.randn(500)
x1 = np.random.randn(500) + 1
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))
# 叠加两个直方图
fig.update_layout(barmode='overlay')
# 减少不透明度以查看两个直方图
fig.update_traces(opacity=0.75)
fig.show()
4.2.5 堆叠直方图
import plotly.graph_objects as go
import numpy as np
x0 = np.random.randn(2000)
x1 = np.random.randn(2000) + 1
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))
# 这两个柱状图是在另一个柱状图之上绘制的
fig.update_layout(barmode='stack')
fig.show()
4.2.6 风格直方图
import plotly.graph_objects as go
import numpy as np
x0 = np.random.randn(500)
x1 = np.random.randn(500) + 1
fig = go.Figure()
fig.add_trace(go.Histogram(
x=x0,
histnorm='percent',
name='control', # 图例和悬停标签中使用的名称
xbins=dict( # 用于直方图的箱子
start=-4.0,
end=3.0,
size=0.5
),
marker_color='#EB89B5',
opacity=0.75
))
fig.add_trace(go.Histogram(
x=x1,
histnorm='percent',
name='experimental',
xbins=dict(
start=-3.0,
end=4,
size=0.5
),
marker_color='#330C73',
opacity=0.75
))
fig.update_layout(
title_text='抽样结果',
xaxis_title_text='数值', # xaxis label
yaxis_title_text='计数', # yaxis label
bargap=0.2, # 相邻位置坐标的钢筋之间的间隙
bargroupgap=0.1 # 相同位置坐标的钢筋之间的间隙
)
fig.show()
4.2.7 直方图条形文本
texttemplate您可以使用该参数将文本添加到直方图条。在此示例中,我们将 x 轴值添加为格式后的文本%{variable}。我们还使用 调整文本的大小textfont_size。
import plotly.graph_objects as go
numbers = ["5", "10", "3", "10", "5", "8", "5", "5"]
fig = go.Figure()
fig.add_trace(go.Histogram(x=numbers, name="count", texttemplate="%{x}", textfont_size=20))
fig.show()
4.2.8 累积直方图
import plotly.graph_objects as go
import numpy as np
x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(x=x, cumulative_enabled=True)])
fig.show()
4.2.9 指定聚合函数
import plotly.graph_objects as go
x = ["Apples","Apples","Apples","Oranges", "Bananas"]
y = ["5","10","3","10","5"]
fig = go.Figure()
fig.add_trace(go.Histogram(histfunc="count", y=y, x=x, name="count"))
fig.add_trace(go.Histogram(histfunc="sum", y=y, x=x, name="sum"))
fig.show()
4.2.10 自定义分箱
对于沿 x 轴的自定义分箱,请使用属性nbinsx. nbinsx请注意,autobin 算法将选择一个“不错”的圆形 bin 大小,这可能会导致 bin总数略少。xbins或者,您可以为with设置确切的值autobinx = False。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02',
'1972-01-31', '1970-02-13', '1971-04-19']
fig = make_subplots(rows=3, cols=2)
trace0 = go.Histogram(x=x, nbinsx=4)
trace1 = go.Histogram(x=x, nbinsx = 8)
trace2 = go.Histogram(x=x, nbinsx=10)
trace3 = go.Histogram(x=x,
xbins=dict(
start='1969-11-15',
end='1972-03-31',
size='M18'), # M18代表18个月
autobinx=False
)
trace4 = go.Histogram(x=x,
xbins=dict(
start='1969-11-15',
end='1972-03-31',
size='M4'), # 4个月箱大小
autobinx=False
)
trace5 = go.Histogram(x=x,
xbins=dict(
start='1969-11-15',
end='1972-03-31',
size= 'M2'), # 2 months
autobinx = False
)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 2, 2)
fig.append_trace(trace4, 3, 1)
fig.append_trace(trace5, 3, 2)
fig.show()
4.2.11 在直方图之间共享 bin
在此示例中,两个直方图都使用bingroup属性具有兼容的 bin 设置。请注意,同一子图上和具有相同barmode(“堆栈”、“相对”、“组”)的轨迹被强制进入相同的bingroup,但是具有barmode = "overlay"和不同轴(相同轴类型)的轨迹可以具有兼容的 bin 设置. Histogram 和histogram2d trace 可以共享相同的bingroup.
import plotly.graph_objects as go
import numpy as np
fig = go.Figure(go.Histogram(
x=np.random.randint(7, size=100),
bingroup=1))
fig.add_trace(go.Histogram(
x=np.random.randint(7, size=20),
bingroup=1))
fig.update_layout(
barmode="overlay",
bargap=0.1)
fig.show()
4.2.12 按类别顺序排序直方图
直方图条也可以使用x 轴的categoryorder属性基于分类值的排序逻辑进行排序。使用对直方图条进行排序categoryorder也适用于同一 x 轴上的多条迹线。在以下示例中,直方图条是根据总数值排序的。
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day").update_xaxes(categoryorder='total ascending')
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day", color="smoker").update_xaxes(categoryorder='total descending')
fig.show()
4.2.13 另请参阅:条形图
如果要在每个直方图中显示有关各个项目的信息,请创建一个带有悬停信息的堆叠条形图,如下所示。px.histogram请注意,这在技术上不是直方图图表类型,但通过比较 和 的输出,它将具有如下所示的类似效果px.bar。有关详细信息,请参阅条形图教程。
import plotly.express as px
df = px.data.tips()
fig1 = px.bar(df, x='day', y='tip', height=300,
title='堆叠条形图-将鼠标悬停在单个项目上')
fig2 = px.histogram(df, x='day', y='tip', histfunc='sum', height=300,
title='直方图')
fig1.show()
fig2.show()