Plot.ly:具有共享X轴的子图的高度不同

时间:2018-10-17 14:04:44

标签: python plotly plotly-dash

背景

  • 高度相同(✘)的不同子图(✔)

我可以创建带有共享X轴的子图(从Plot.ly doc改编而成的示例)的图形,在子图之间进行适当的分隔,并可以在其中通过每个子图插入特定的标题subplot_titles

from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=3, cols=1, specs=[[{}], [{}], [{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.1, subplot_titles=('subtitle 1', 
                          'subtitle 2', 'subtitle 3'))
fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes')
py.plot(fig, filename='subplots-shared-xaxes')

enter image description here

  • 具有不同高度(✔)的合并子图(✘)

我还可以创建带有共享X轴的堆叠子图的图形(示例从Plot.ly doc改编而成),您可以在其中通过{{1 }}:

domain

enter image description here

问题

  

如何创建具有共享x轴的子图,其中您同时具有标题(图1)和不同的相对高度(图2)每个子图


我尝试过的事情

  • 使用from plotly import tools import plotly.plotly as py import plotly.graph_objs as go trace1 = go.Scatter( x=[0, 1, 2], y=[10, 11, 12] ) trace2 = go.Scatter( x=[2, 3, 4], y=[100, 110, 120], yaxis='y2' ) trace3 = go.Scatter( x=[3, 4, 5], y=[1000, 1100, 1200], yaxis='y3' ) data = [trace1, trace2, trace3] layout = go.Layout( yaxis=dict( domain=[0, 0.25] ), legend=dict( traceorder='reversed' ), yaxis2=dict( domain=[0.25, 0.75] ), yaxis3=dict( domain=[0.75, 1] ) ) fig = go.Figure(data=data, layout=layout) fig['layout'].update(height=600, width=600, title='Stacked Subplots with Shared X-Axes') py.plot(fig, filename='stacked-subplots-shared-x-axis') subplots

第一个技巧是创建一个额外的行,在其中两个行上绘制一个图跨度:

rowspan

enter image description here

结果正是我想要实现的。但是,对于我来说,这种破解似乎错误:从语法上来讲,我不希望出现一个跨越两个的图行。此外,如果要进行任何修改,必须将from plotly import tools import plotly.plotly as py import plotly.graph_objs as go trace1 = go.Scatter( x=[0, 1, 2], y=[10, 11, 12] ) trace2 = go.Scatter( x=[2, 3, 4], y=[100, 110, 120], ) trace3 = go.Scatter( x=[3, 4, 5], y=[1000, 1100, 1200], ) fig = tools.make_subplots( rows=4, cols=1, specs=[ [{}], [{'rowspan':2}], [None], [{}], ], shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.1, subplot_titles=( 'subtitle 1', 'subtitle 2', None, 'subtitle 3', ) ) fig.append_trace(trace3, 1, 1) fig.append_trace(trace2, 2, 1) fig.append_trace(trace1, 4, 1) fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes, span rows') py.plot(fig, filename='subplots-shared-x-axis-span-rows', auto_open=True) 添加到[None]并将specs添加到None是很丑陋且容易出错的。

还要考虑您想要subplot_titles高度的子图分布的情况!

  • 使用13% | 70% | 17%

一个更好的选择是将更新轴与domain一起使用,但子图标题却被弄乱了(它们仍然均匀地垂直分布):

domain

enter image description here

1 个答案:

答案 0 :(得分:0)

Kully已在PR #1245中解决了此错误,并已在dash v3.4.0 中对其进行了修复。

enter image description here

下面的代码-仅使用3个子图和row_width=[0.2, 0.4, 0.2]-应该可以正常工作:

from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=3, cols=1,
                          shared_xaxes=True,
                          vertical_spacing=0.1,
                          subplot_titles=('subtitle 1', 'subtitle 2', 'subtitle 3'),
                          row_width=[0.2, 0.4, 0.2]
                         )

fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes')
go.FigureWidget(fig)