python - 如何删除 x 轴处的垂直线并将标签添加到绘图子图中?
问题描述
我一直在尝试使用 plotly 库创建子图。我已经完成了大部分工作,但想知道如何添加以下内容:
- 删除右侧散点图 x=0 处的垂直线。
- 将 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)
问题
- 如何删除右侧图中的垂直 x=0 线
- 如何给这两个子图提供 xlabel 和 ylabels?
解决方案
假设您使用的是plotly
版本 4:
- 您可以删除垂直 x=0 线
fig.update_xaxes(row=1, col=2, zeroline=False)
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
等)设置任何轴的标题
推荐阅读
- apache-kafka - 在具有单个分区的 Kafka 记录中,我可以插入的最大记录是多少?
- flutter - 颤动 SingleChildScrollView 删除滚动空白
- javascript - Java Web Socket 关闭 (getCloseCode : TOO BIG)
- android - Flutter 插件错误:无法从 C:\***\AndroidManifest.xml 读取 packageName
- raspberry-pi - 如何在树莓派启动时运行 exe 文件
- java - JPA + MySQL - 数据库生成的列值
- confluent-platform - 可以远程控制 Confluent 控制中心 Web UI 吗?
- angular - Angular 2 使用带有强制参数的服务
- sql-server - SQL Server 的 Logstash 时区异常
- sql - Postgres SQL Case Join 在第一个案例匹配时不停止