如何将图形嵌套在python中?

时间:2019-06-26 18:27:15

标签: python plotly plotly-python

我想以图形方式将一些图形放在另一个图形中。目标是这样的:

enter image description here

注意:我不希望有2 x 2的子图。相反,我想要一个具有1 x 2子图的图形,每个图都由具有2 x 1子图的图形填充。

这是我尝试过的:

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

#Fake data
left_xs = [1, 2, 3, 4, 5]
left_top_ys = [1,4,9,16,25]
left_bot_ys = [0, 0, 0, 50, 0]

right_xs = [1, 2, 3, 4, 5, 6]
right_top_ys = [2, 4, 7, 11, 16, 22]
right_bot_ys = [0, 0, 10, 20, 0 ,0]

left_title = "Left"
right_title = "Right"

#The figure that will contain the sub figures
bigfig = tools.make_subplots(rows=1, cols=2, subplot_titles=[left_title, right_title], vertical_spacing=0.02)

#Left subfigure (in the red circle)
left_fig = tools.make_subplots(rows=2, cols=1, vertical_spacing=0.05, subplot_titles=["left_top", "left_bot"], shared_xaxes=True)
left_fig.append_trace(
    go.Scatter(x=left_xs, y=left_top_ys, name="Left Top", showlegend=False),
    1, 1
    )
left_fig.append_trace(
    go.Scatter(x=left_xs, y=left_bot_ys, name="Left Bot", showlegend=False),
    2, 1
    )

#Right subfigure (in the blue circle)
right_fig = tools.make_subplots(rows=2, cols=1, vertical_spacing=0.05, subplot_titles=["right_top", "right_bot"], shared_xaxes=True)
right_fig.append_trace(
    go.Scatter(x=right_xs, y=right_top_ys, name="right Top", showlegend=False),
    1, 1
    )
right_fig.append_trace(
    go.Scatter(x=right_xs, y=right_bot_ys, name="right Bot", showlegend=False),
    2, 1
    )

#Doesn't work, only puts in the first trace of each fig
#How do I put both the left figure and the right figure in bigfig?
bigfig.append_trace(left_fig.data[0], 1, 1)
bigfig.append_trace(right_fig.data[0], 1, 2)

#Show results
py.plot(bigfig, filename="bigfig.html")
py.plot(left_fig, filename="left_fig.html")
py.plot(right_fig, filename="right_fig.html")

我该怎么做?

0 个答案:

没有答案