首页 > 解决方案 > 如何删除 x 轴处的垂直线并将标签添加到绘图子图中?

问题描述

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

  1. 删除右侧散点图 x=0 处的垂直线。
  2. 将 xlabel 和 ylabels 添加到左右图形。
    • 直方图
    • 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)

输出: 在此处输入图像描述

问题

标签: pythonpandasmatplotlibplotly

解决方案


假设您使用的是plotly版本 4:

  • 您可以删除垂直 x=0 线fig.update_xaxes(row=1, col=2, zeroline=False)
  • fig.update_xaxes(row=1, col=1, title="title")您可以使用( 替换并yaxes为/xaxes设置适当的标题来标记轴rowcol

子图的文档链接

如果您使用的是版本 3:

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

推荐阅读