如何删除x轴上的垂直线并为可绘制的子图添加标签?

时间:2019-08-01 15:48:40

标签: python pandas matplotlib plotly

我一直在尝试使用plotly库创建子图。我已经完成了大部分工作,但想知道如何添加以下内容:

  1. 删除右侧散点图上x = 0处的垂直线。
  2. 在左右图形上添加xlabel和ylabel。
    • 直方图
    • xlabel =值,ylabel =计数
    • 散点图
    • xlabel =值,ylabe =经验CDF

到目前为止,这是我的代码:


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)

输出: enter image description here

问题

  • 如何在右侧图中删除垂直x = 0线
  • 如何给这两个子图赋予xlabel和ylabel?

1 个答案:

答案 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设置适当的标题

subplotsaxes的文档链接

如果您使用的是版本3:

  • 您可以使用fig.layout.xaxis<n>.zeroline=False(适当设置n,在这种情况下可能是2,从任何x轴上删除零线
  • 您可以使用fig.layout.<dim>axis<n>.title="title"设置任何轴的标题(适当设置dimn,例如yaxis1等)