Part of the Plotly Learning Log series:

  1. Layout Setting for PlotlyThis post!

Table of Contents Link to this heading

Overview Link to this heading

This post talks about the layout setting for Plotly. Specifically, the layout setting can be evoked by

python
1layout = go.Layout(**kwargs)
2fig = go.Figure(data=data, layout=layout)
3# i.e. plot(fig)

where **kwargs is a series of settings.

Canva Size Link to this heading

The canva size can be set by:

  • width. In pixels (e.g. width=800)
  • height. In pixels (e.g. height=600)
  • autosize. A boolean. If True, the canva will be resized automatically to fit dimensions that are not explicitly set.
  • margin. A dictionary with keys l, r, t, b, pad (e.g. margin=dict(l=20, r=20, t=20, b=20, pad=4))

Example

Plot in specific area Link to this heading

This can be combined to create multiple plots of different sizes in the same figure.

In trace, the xaxis and yaxis can be set to x1, x2 (“x” + integer) and y1, y2 (“y” + integer) to specify the plot area.

In layout, set a group of xaxis and yaxis to match those in trace. Specifically,

trace layout
xaxis1 or xaxis x1 or x
xaxis2 x2
xaxisN xN
Similar for yaxis Similar for `y``

In layout these axis should be anchored to its counterpart in trace by domain like

python
 1trace = go.Scatter(
 2    ...
 3    xaxis='x2',
 4    yaxis='y2'
 5)
 6layout = go.Layout(
 7    xaxis2=dict(
 8        domain=[0.6, 1],
 9        anchor='y2' # Note that it is 'y2' not 'x2'
10    ),
11    yaxis2=dict(
12        domain=[0.6, 1],
13        anchor='x2' # Note that it is 'x2' not 'y2'
14    )
15)

Example

Code Example Link to this heading

Canva Size (Example) Link to this heading

python
1layout = go.Layout(
2    height=2000,
3    autosize=True,
4    margin=dict(l=20, r=20, t=20, b=20, pad=4)
5)

This should enable a canvas that has fixed height and variable width. (e.g. Zooming out in browser -> both dim pixels increase -> (But height pixels fixed) Plot are “longers”)

Plot in specific area (Example) Link to this heading

python
 1trace1 = go.Scatter(
 2    x=[1, 2, 3],
 3    y=[4, 5, 6],
 4    xaxis='x1',
 5    yaxis='y1'
 6)
 7trace2 = go.Scatter(
 8    x=[1, 2, 3],
 9    y=[4, 5, 6],
10    xaxis='x2',
11    yaxis='y2'
12)
13layout = go.Layout(
14    xaxis1=dict(
15        domain=[0, 0.5],
16        anchor='y1'
17    ),
18    yaxis1=dict(
19        domain=[0, 0.5],
20        anchor='x1'
21    ),
22    xaxis2=dict(
23        domain=[0.5, 1],
24        anchor='y2'
25    ),
26    yaxis2=dict(
27        domain=[0.5, 1],
28        anchor='x2'
29    )
30)

View My Interactive Chart Link to this heading

ddsfsdd

Click here to view the chart

View My Chart