ced*_*eds 5 python pandas plotly
我需要在 Plotly 中绘制两个直方图,其中每个直方图在均值所在的位置绘制一条线,并带有一个显示平均值的标签。我的代码目前绘制了两个直方图,但是我不知道如何使用标签添加平均线。任何的想法?
import numpy as np
import random
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode() # run at the start of every ipython notebook
a = np.random.normal(1500, 100, 1000)
b = np.random.normal(1500, 150, 1000)
trace1 = go.Histogram(
x=a,
opacity=0.75,
histnorm='probability',
name='> 180 t/h'
)
trace2 = go.Histogram(
x=b,
opacity=0.75,
histnorm='probability',
name='< 160 t/h',
yaxis='y2'
)
data = [trace1, trace2]
layout = go.Layout(
title='title',
barmode='overlay',
xaxis=dict(
title=''
),
yaxis=dict(
title='Normalized Frequency < 160 t/h'
),
yaxis2=dict(
title='Normalized Frequency > 180 t/h',
anchor='free',
overlaying='y',
side='right',
position=1
)
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Run Code Online (Sandbox Code Playgroud)
经过几个小时的摆弄,我想我得到了一些大致有效的东西:
a = np.random.normal(1200, 100, 1000)
b = np.random.normal(1500, 150, 1000)
df = pd.DataFrame(np.transpose([a,b]), columns=['a','b'])
a = df.a
b = df.b
trace1 = go.Histogram(
x=df.a,
opacity=0.75,
histnorm='probability',
name='> 180 t/h'
)
trace2 = go.Histogram(
x=df.b,
opacity=0.75,
histnorm='probability',
name='< 160 t/h',
yaxis='y2'
)
# Create traces
data = [trace1, trace2]
layout = go.Layout(
title='item',
barmode='overlay',
xaxis=dict(
title=''
),
yaxis=dict(
title='Normalized Frequency < 160 t/h'
),
yaxis2=dict(
title='Normalized Frequency > 180 t/h',
anchor='free',
overlaying='y',
side='right',
position=1
),
# Mean lines
shapes= [{'line': {'color': '#0099FF', 'dash': 'solid', 'width': 1},
'type': 'line',
'x0': df.a.mean(),
'x1': df.a.mean(),
'xref': 'x',
'y0': -0.1,
'y1': 1,
'yref': 'paper'},
{'line': {'color': '#FDAB5A', 'dash': 'solid', 'width': 1},
'type': 'line',
'x0': df.b.mean(),
'x1': df.b.mean(),
'xref': 'x',
'y0': -0.1,
'y1': 1,
'yref': 'paper'}],
# Annotations
annotations=[
dict(
x=df.a.mean(),
y=1,
xref='x',
yref='paper',
text="Mean a = {:,.0f}".format(df.a.mean()),
showarrow=True,
arrowhead=7,
ax=1,
ay=1,
axref='paper',
ayref='paper'
),
dict(
x=df.b.mean(),
y=0.95,
xref='x',
yref='paper',
text="Mean b = {:,.0f}".format(df.b.mean()),
showarrow=True,
arrowhead=7,
ax=1,
ay=1,
axref='paper',
ayref='paper'
)
]
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
Run Code Online (Sandbox Code Playgroud)
最初我尝试使用cufflinks. 这工作正常:
import cufflinks as cf
df.iplot(kind='histogram', histnorm='probability', barmode='overlay',
vline=[dict(x=df.a.mean(),color='#5283AD'), dict(x=df.b.mean(),color='#FDAB5A')])
Run Code Online (Sandbox Code Playgroud)
但是,如果您还尝试添加注释,则会删除 vlines。
最后,我使用 asFigure 返回带有 vlines 但没有布局的 plotdict。然后我提取了形状位以手动创建上面的解决方案。
# Return a dict
plotdict = df.iplot(kind='histogram', histnorm='probability', barmode='overlay',
vline=[dict(x=df.a.mean(),color='#5283AD'), dict(x=df.b.mean(),color='#FDAB5A')],
asFigure=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10418 次 |
| 最近记录: |