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

标签 python plotly histogram

有谁知道如何在绘图中设置辅助x轴及其范围?

我试图在这里显示垂直直方图,但它目前仍然太小

Vertical histogram

enter image description here

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
)

最佳答案

要获得示例中所需的内容,只需将以下行添加到您的设置中即可:

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 轴的范围。

绘图

enter image description here

完整代码:

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

关于python - 如何在带有子图的绘图中设置辅助x轴及其范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66196920/

相关文章:

python - 如何为 Cassandra 创建只读模型

python - 类型错误 : unsupported format string passed to Series. __format__

python - 密谋:如何反转轴?

java - 如何删除直方图中的重复字符[JAVA]

python - 如何通过 Selenium 和 Python 根据 HTML 单击引导按钮

python遍历列表

python - plotly 中的相机位置如何工作

asp.net-mvc-3 - 用于制作直方图的库javascript

python - 使用 nx. Degree_histogram 绘制图的度分布

python - 使用 tweepy 从 Twitter 获取热门话题。有更好的方法吗?