Plotly:如何将轴布局添加到子图中?

Jef*_*eff 4 python subplot plotly

鉴于我有来自此链接的以下代码:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Subplots")
fig.show()
Run Code Online (Sandbox Code Playgroud)

这段代码的问题是,xaxisyaxis没有任何标签。除此之外,当前代码仅对所有图应用一个标题,但是我想对每个散点图应用不同的标题

我怎样才能做到这一点?

ves*_*and 11

此代码中的问题是,xaxis 和 yaxis 没有任何标签。

您可以通过设置图形结构的子集来编辑任何轴:

fig['layout']['xaxis']['title']='Label x-axis 1'
Run Code Online (Sandbox Code Playgroud)

除此之外,当前代码仅将一个标题应用于所有地块

根据用户 shaik moeed 提到的情节版本,您可以subplot_titles在图形定义中包含:

fig = make_subplots(rows=1, cols=2, subplot_titles=('Subplot title1',  'Subplot title2'))
Run Code Online (Sandbox Code Playgroud)

阴谋:

在此处输入图片说明

代码:

from plotly.subplots import make_subplots
import plotly.graph_objects as go


# plotly fig setup
fig = make_subplots(rows=1,
                    cols=2,
                    subplot_titles=('Subplot title1',  'Subplot title2')) 

# traces
fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

# edit axis labels
fig['layout']['xaxis']['title']='Label x-axis 1'
fig['layout']['xaxis2']['title']='Label x-axis 2'
fig['layout']['yaxis']['title']='Label y-axis 1'
fig['layout']['yaxis2']['title']='Label y-axis 2'

# plot it
fig.show()
Run Code Online (Sandbox Code Playgroud)


sha*_*eed 10

从 Plotly 4.0.0 开始,您可以将主轴标题分别添加为 x_title 和 y_title:

from plotly.subplots import make_subplots
fig = make_subplots(rows=2,
                    cols=2,
                    x_title='Your master x-title',
                    y_title='Your master y-title',
                    subplot_titles=('Subplot title1',  'Subplot title2', 
                                    'Subplot title3', 'Subplot title4'))
Run Code Online (Sandbox Code Playgroud)

示例代码:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1,
                    cols=2,
                    x_title='Your master x-title',
                    y_title='Your master y-title',
                    subplot_titles=('Subplot title1',  'Subplot title2'))

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

fig.update_layout(title_text="Side By Side Subplots with diffrent sub titles")
fig.show()
Run Code Online (Sandbox Code Playgroud)

输出: 在此输入图像描述