简体   繁体   English

如何在带有子图的 plotly 图中设置辅助 x 轴及其范围?

[英]How to set secondary x-axis and its range in plotly graph with subfigures?

does anyone know how to set a secondary x-axis and also its range in plotly?有谁知道如何设置辅助 x 轴及其在 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.第 1 行设置了一个辅助 x 轴,而第 2 行为其分配了一条迹线。 I'm assuming fig.data[3] is the correct trace but you can check that for yourself.我假设fig.data[3]是正确的跟踪,但您可以自己检查。 Line 3, unsuprisingly, sets the range of the secondary x-axis.不出所料,第 3 行设置了辅助 x 轴的范围。

Plot 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何获得 Plotly 图的 x 轴的范围? - How do you get the range of the x-axis of a Plotly graph? 如何为 Plotly 条形图设置 static x 轴值和图例集? - How to set a static set of x-axis values and legend for a Plotly Bar Graph? 如何在 Plotly Python 中设置多索引 x 轴的范围? - How to set the range of multi-index x-axis in Plotly Python? 如何使用 plotly express 在 x 轴上用时间绘制一些事件? - How to graph some events with time on x-axis with plotly express? 绘图图上的辅助/平行X轴(python) - Secondary / Parallel X-Axis on Plotly charts (python) 如何设置海洋点图的x轴范围? - How to set the range of x-axis for a seaborn pointplot? 有没有办法用 plotly 或 python 创建条形图可视化,其中 y 轴是 24 小时范围,x 轴是所有工作日? - Is there a way to create a bar graph visualization with plotly or python where the y-axis is 24hour range and x-axis is all the weekdays? 绘制折线图以遵循 X 轴上的有序时间序列 - Plotly line graph to follow ordered time series on X-axis 绘图图未正确显示 x 轴值 - Plotly graph does not show x-axis values correctly 如何在Python API中使用plotly在x轴范围中间位置绘制垂直线? - How to plot a vertical line at the x-axis range median position using plotly in Python API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM