我一直在尝试使用plotly库创建子图。我已经完成了大部分工作,但想知道如何添加以下内容:
到目前为止,这是我的代码:
import numpy as np
import plotly
import plotly.offline as py
import plotly.plotly as pyp
import plotly.graph_objs as go
import plotly.figure_factory as ff
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)
np.random.seed(42)
data = np.random.binomial(10, 0.3, 10000)
x, y = np.sort(data), np.arange(1, len(data)+1) / len(data)
idx = np.abs(y-0.5).argmin()
z = x[idx]
hist0 = go.Histogram(x = data,name='Histogram')
sc1 = go.Scatter(x=x,y=y,mode = 'markers',name = 'Empirical CDF')
sc2 = go.Scatter(x=x,y=[0.5]*len(x),mode = 'lines',name = 'y=0.5 (central tendency)')
sc3 = go.Scatter(x=[z]*1000,y=np.linspace(0,1,1000),mode = 'lines',name = 'x={:.2f}'.format(z))
fig = plotly.tools.make_subplots(rows=1, cols=2, shared_xaxes=True,
subplot_titles=('Histogram','Empirical CDF'))
fig.append_trace(hist0, 1, 1)
fig.append_trace(sc1, 1, 2)
fig.append_trace(sc2, 1, 2)
fig.append_trace(sc3, 1, 2)
fig['layout'].update(height=400, width=800, title='')
iplot(fig)
答案 0 :(得分:2)
假设您使用的是plotly
版本4:
fig.update_xaxes(row=1, col=2, zeroline=False)
删除垂直的x = 0线fig.update_xaxes(row=1, col=1, title="title")
标记轴(用yaxes
代替xaxes
,并为row
/ col
设置适当的标题如果您使用的是版本3:
fig.layout.xaxis<n>.zeroline=False
(适当设置n
,在这种情况下可能是2
,从任何x轴上删除零线fig.layout.<dim>axis<n>.title="title"
设置任何轴的标题(适当设置dim
和n
,例如yaxis1
等)