2

我想把一些数字放在另一个图中。目标是这样的:

在此处输入图像描述

注意:我不想要一个带有 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")

我怎样才能做到这一点?

4

0 回答 0