如何在绘图Dash中并排绘制图形?

时间:2020-04-14 13:35:02

标签: python matplotlib plotly data-visualization

以下link提供了代码以html组件并排地绘制图表。但是,我正在寻找类似matplotlib的plt.subplot的功能。下图取自matplotlib的official documentation,是所需输出的示例。

enter image description here

1 个答案:

答案 0 :(得分:2)

解决方案

使用inset plots时,您可以并排设置两个图,在Layout组件中指定一个domain参数,即

import plotly.graph_objs as go

go.Layout(xaxis = dict(domain = [0.0, 0.45]),
          xaxis2 = dict(domain = [0.55, 1.0]),
         )

,您可以在其中调整图形x轴的位置,以在0和1之间切换值。有关完整示例,请参阅以下部分。

示例

例如并排绘制散点图条形图的示例,

# Set plotly in offline mode
import plotly.graph_objs as go
import pandas as pd
offline.init_notebook_mode(connected=True)

# Simple Scatter plot
trace0 = go.Scatter(x = [10, 20, 30],
                    y = [40, 30, 20]
)

# Simple Bar chart
trace1 = go.Bar(x=['cat_1', 'cat_2', 'cat_3', 'cat_4'],
                y=[5, 27, 31, 48],

                xaxis='x2',
                yaxis='y2'
               )

# Data component 
data = [trace0, trace1]

# Layout component
layout = go.Layout(xaxis = dict(domain = [0.0, 0.45]),
                   xaxis2 = dict(domain = [0.55, 1.0]),
                   yaxis2 = dict(overlaying='y',
                                 anchor = 'free',
                                 position = 0.55
                                )

                  )

# Figure component
fig = go.Figure(data=data, layout=layout)

offline.iplot(fig)

输出以下图像。 enter image description here