How to set secondary x-axis and its range in plotly graph with subfigures?(如何在带有子图的绘图图中设置辅助 x 轴及其范围?)
问题描述
有谁知道如何设置辅助 x 轴及其范围?
我试图在这里显示一个垂直直方图,但它目前仍然太小
import pandas as pd将 numpy 导入为 np导入 plotly.graph_objects从 plotly.subplots 导入 make_subplots从 plotly.graph_objs.layout 导入 YAxis,XAxis,Marginx1 = np.linspace(0, 4, 41)y1 = (x1-2)**3+2y2 = x1*0+2x1_sample = np.random.normal(2,0.3,5000)y1_sample = (x1_sample-2)**3+2数据 = np.column_stack((x1_sample, y1_sample))df_hist = pd.DataFrame(数据 = 数据,列 = ['x1_sample','y1_sample'])fig = make_subplots(rows=1, cols=2, specs = [[{"secondary_y": True}, {"secondary_y": True}]], Horizontal_spacing=0.2)# 子图 1## 线fig.update_yaxes(范围 = [0, 4], dtick = 1, secondary_y=False)fig.add_trace(go.Scatter(x = x1, y = y1, mode='lines'),行 = 1,列 = 1)fig.add_trace(go.Scatter(x = x1, y = y2, mode = 'lines'),行 = 1,列 = 1)## 直方图fig.update_yaxes(范围 = [0, 0.5], dtick = 0.1, secondary_y=True)fig.add_trace(go.Histogram(x = df_hist['x1_sample'], histnorm='概率', nbinsx=40),secondary_y=真,行 = 1,列 = 1)fig.add_trace(go.Histogram(y = df_hist['y1_sample'], histnorm='概率', nbinsy=40),行 = 1,列 = 1)
要在示例中获得所需的内容,只需在设置中添加以下行:
fig.update_layout(xaxis2= {'anchor': 'y', 'overlaying': 'x', 'side': 'top'})fig.data[3].update(xaxis='x2')fig.update_layout(xaxis2_range=[-0,0.6])
第 1 行设置了一个辅助 x 轴,而第 2 行为其分配了一条轨迹.我假设 fig.data[3]
是正确的跟踪,但您可以自己检查.不出所料,第 3 行设置了辅助 x 轴的范围.
情节
完整代码:
将 numpy 导入为 np将熊猫导入为 pd导入 plotly.graph_objects从 plotly.subplots 导入 make_subplots从 plotly.graph_objs.layout 导入 YAxis,XAxis,Marginx1 = np.linspace(0, 4, 41)y1 = (x1-2)**3+2y2 = x1*0+2x1_sample = np.random.normal(2,0.3,5000)y1_sample = (x1_sample-2)**3+2数据 = np.column_stack((x1_sample, y1_sample))df_hist = pd.DataFrame(数据 = 数据,列 = ['x1_sample','y1_sample'])fig = make_subplots(rows=1, cols=2, specs = [[{"secondary_y": True}, {"secondary_y": True}]],水平间距=0.2,shared_xaxes = False)# 子图 1## 线fig.update_yaxes(范围 = [0, 4], dtick = 1, secondary_y=False)fig.add_trace(go.Scatter(x = x1, y = y1, mode='lines'),行 = 1,列 = 1)fig.add_trace(go.Scatter(x = x1, y = y2, mode = 'lines'),行 = 1,列 = 1)## 直方图fig.update_yaxes(范围 = [0, 0.5], dtick = 0.1, secondary_y=True)fig.add_trace(go.Histogram(x = df_hist['x1_sample'], histnorm='概率', nbinsx=40),secondary_y=真,行 = 1,列 = 1)fig.add_trace(go.Histogram(y = df_hist['y1_sample'], histnorm='概率', nbinsy=40),行 = 1,列 = 1)fig.update_layout(xaxis2= {'anchor': 'y', 'overlaying': 'x', 'side': 'top'})fig.data[3].update(xaxis='x2')fig.update_layout(xaxis2_range=[-0,0.6])图.show()
does anyone know how to set a secondary x-axis and also its range in plotly?
I am trying to show a vertical histogram here but it is currently still too small
Vertical histogram
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.graph_objs.layout import YAxis,XAxis,Margin
x1 = np.linspace(0, 4, 41)
y1 = (x1-2)**3+2
y2 = x1*0+2
x1_sample = np.random.normal(2,0.3,5000)
y1_sample = (x1_sample-2)**3+2
data = np.column_stack((x1_sample, y1_sample))
df_hist = pd.DataFrame(data = data, columns = ['x1_sample', 'y1_sample'])
fig = make_subplots(rows=1, cols=2, specs = [[{"secondary_y": True}, {"secondary_y": True}]], horizontal_spacing=0.2)
# Subplot 1
## Line
fig.update_yaxes(range = [0, 4], dtick = 1, secondary_y=False)
fig.add_trace(
go.Scatter(x = x1 , y = y1, mode='lines'),
row = 1, col = 1
)
fig.add_trace(
go.Scatter(x = x1, y = y2, mode = 'lines'),
row = 1, col = 1
)
## Histogram
fig.update_yaxes(range = [0, 0.5], dtick = 0.1, secondary_y=True)
fig.add_trace(
go.Histogram(x = df_hist['x1_sample'], histnorm='probability', nbinsx=40),
secondary_y=True,
row = 1, col = 1
)
fig.add_trace(
go.Histogram(y = df_hist['y1_sample'], histnorm='probability', nbinsy=40),
row = 1, col = 1
)
To get what you need in your example, just add the following lines to your setup:
fig.update_layout(xaxis2= {'anchor': 'y', 'overlaying': 'x', 'side': 'top'})
fig.data[3].update(xaxis='x2')
fig.update_layout(xaxis2_range=[-0,0.6])
Line 1 sets up a secondary x-axis, while line 2 assigns a trace to it. I'm assuming fig.data[3]
is the correct trace but you can check that for yourself. Line 3, unsuprisingly, sets the range of the secondary x-axis.
Plot
Complete code:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.graph_objs.layout import YAxis,XAxis,Margin
x1 = np.linspace(0, 4, 41)
y1 = (x1-2)**3+2
y2 = x1*0+2
x1_sample = np.random.normal(2,0.3,5000)
y1_sample = (x1_sample-2)**3+2
data = np.column_stack((x1_sample, y1_sample))
df_hist = pd.DataFrame(data = data, columns = ['x1_sample', 'y1_sample'])
fig = make_subplots(rows=1, cols=2, specs = [[{"secondary_y": True}, {"secondary_y": True}]],
horizontal_spacing=0.2,
shared_xaxes = False)
# Subplot 1
## Line
fig.update_yaxes(range = [0, 4], dtick = 1, secondary_y=False)
fig.add_trace(
go.Scatter(x = x1 , y = y1, mode='lines'),
row = 1, col = 1
)
fig.add_trace(
go.Scatter(x = x1, y = y2, mode = 'lines'),
row = 1, col = 1
)
## Histogram
fig.update_yaxes(range = [0, 0.5], dtick = 0.1, secondary_y=True)
fig.add_trace(
go.Histogram(x = df_hist['x1_sample'], histnorm='probability', nbinsx=40),
secondary_y=True,
row = 1, col = 1
)
fig.add_trace(
go.Histogram(y = df_hist['y1_sample'], histnorm='probability', nbinsy=40),
row = 1, col = 1
)
fig.update_layout(xaxis2= {'anchor': 'y', 'overlaying': 'x', 'side': 'top'})
fig.data[3].update(xaxis='x2')
fig.update_layout(xaxis2_range=[-0,0.6])
fig.show()
这篇关于如何在带有子图的绘图图中设置辅助 x 轴及其范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在带有子图的绘图图中设置辅助 x 轴及其范围?
基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01