鉴于我有来自此链接的以下代码:
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()
这段代码的问题是,
xaxis
和yaxis
没有任何标签。除此之外,当前代码仅对所有图应用一个标题,但是我想对每个散点图应用不同的 titles。
我该怎么做?
这段代码的问题是,xaxis和yaxis没有任何标签。
您可以通过子集化图形结构来编辑任何轴:
fig['layout']['xaxis']['title']='Label x-axis 1'
除此之外,当前代码仅对所有绘图应用一个标题
根据用户 shaik moeed 提到的绘图版本,您可以在图形定义中包含
subplot_titles
:
fig = make_subplots(rows=1, cols=2, subplot_titles=('Subplot title1', 'Subplot title2'))
剧情:
代码:
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()
从 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'))
示例代码:
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()
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# plotly fig setup
fig = make_subplots(rows=1,
cols=2,vertical_spacing=0.09,
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
)
#fig.add_trace(go.Scatter(x=time_list, y=C_hl0),
# row=1, col=1)
fig.update_layout(height=1600, width=1000,
title_text="Whatever you want")
for i in range(1,5):
fig['layout']['xaxis{}'.format(i)]['title']='Label X axis 1'
fig['layout']['yaxis{}'.format(i)]['title']='Label X axis 2'
fig.show()
#If you want to save your plot into your local directory
import plotly.io as pio
pio.kaleido.scope.default_format = "png"
fig.write_image(r"C:\fig1.png")
当前的最佳实践是在
subplot_titles
中使用参数 make_subplots
并使用 fig.update_xaxes
和 fig.update_yaxes
设置轴标签,如下面从 plotly 文档复制的示例所示。
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# Initialize figure with subplots
fig = make_subplots(
rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4")
)
# Add 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)
fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]), row=2, col=1)
fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]), row=2, col=2)
# Update xaxis properties
fig.update_xaxes(title_text="xaxis 1 title", row=1, col=1)
fig.update_xaxes(title_text="xaxis 2 title", range=[10, 50], row=1, col=2)
fig.update_xaxes(title_text="xaxis 3 title", showgrid=False, row=2, col=1)
fig.update_xaxes(title_text="xaxis 4 title", type="log", row=2, col=2)
# Update yaxis properties
fig.update_yaxes(title_text="yaxis 1 title", row=1, col=1)
fig.update_yaxes(title_text="yaxis 2 title", range=[40, 80], row=1, col=2)
fig.update_yaxes(title_text="yaxis 3 title", showgrid=False, row=2, col=1)
fig.update_yaxes(title_text="yaxis 4 title", row=2, col=2)
# Update title and height
fig.update_layout(title_text="Customizing Subplot Axes", height=700)
fig.show()