2
votes

I am plotting a images with 4 subplots, e.g. using the following code. How can I add title to each subplot?

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

import numpy as np

x = np.linspace(-5, 80, 10)
y = np.linspace(-5, 60, 10)
xGrid, yGrid = np.meshgrid(y, x)
z = xGrid ** 3 + yGrid ** 3

scene = dict(
    xaxis=dict(
        gridcolor='rgb(255, 255, 255)',
        zerolinecolor='rgb(255, 255, 255)',
        showbackground=True,
        backgroundcolor='rgb(230, 230,230)'
    ),
    yaxis=dict(
        gridcolor='rgb(255, 255, 255)',
        zerolinecolor='rgb(255, 255, 255)',
        showbackground=True,
        backgroundcolor='rgb(230, 230,230)'
    ),
    zaxis=dict(
        gridcolor='rgb(255, 255, 255)',
        zerolinecolor='rgb(255, 255, 255)',
        showbackground=True,
        backgroundcolor='rgb(230, 230,230)'
    )
)

fig = tools.make_subplots(rows=2, cols=2,
                        specs=[[{'is_3d': True}, {'is_3d': True}],
                                [{'is_3d': True}, {'is_3d': True}]])

# adding surfaces to subplots.
fig.append_trace(dict(type='surface', x=x, y=y, z=z, colorscale='Viridis',
                    scene='scene1', showscale=False), 1, 1)
fig.append_trace(dict(type='surface', x=x, y=y, z=z, colorscale='RdBu',
                    scene='scene2', showscale=False), 1, 2)
fig.append_trace(dict(type='surface', x=x, y=y, z=z, colorscale='YlOrRd',
                    scene='scene3', showscale=False), 2, 1)
fig.append_trace(dict(type='surface', x=x, y=y, z=z, colorscale='YlGnBu',
                    scene='scene4', showscale=False), 2, 2)

fig['layout'].update(title='subplots with different colorscales',
                    height=800, width=800)
fig['layout']['scene1'].update(scene)
fig['layout']['scene2'].update(scene)
fig['layout']['scene3'].update(scene)
fig['layout']['scene4'].update(scene)

py.plot(fig, filename='multiple_plots.html')

I tried to replace

fig = tools.make_subplots(rows=2, cols=2, specs=[[{'is_3d': True}, {'is_3d': 
True}], [{'is_3d': True}, {'is_3d': True}]])

with the following

fig = tools.make_subplots(rows=2, cols=2, subplot_titles= ('Plot1','Plot2','Plot3','Plot4'),specs=[[{'is_3d': True}, {'is_3d': True}],[{'is_3d': True}, {'is_3d': True}]])

It gives me the following error


IndexError Traceback (most recent call last) in () 33 fig = tools.make_subplots(rows=2, cols=2,subplot_titles= ('p1','p2','p3','p4'), 34 specs=[[{'is_3d': True}, {'is_3d': True}], ---> 35 [{'is_3d': True}, {'is_3d': True}]]) 36 37 # adding surfaces to subplots.

~\AppData\Local\Continuum\anaconda3\lib\site-packages\plotly\tools.py in make_subplots(rows, cols, shared_xaxes, shared_yaxes, start_cell, print_grid, **kwargs) 1373 pass 1374 else: -> 1375 plot_titles.append({'y': subtitle_pos_y[index], 1376 'xref': 'paper', 1377 'x': subtitle_pos_x[index],

IndexError: list index out of range

How can I solve this? Thanks

1

1 Answers

1
votes

This looks like a bug to me. Here it seems like list_of_domains is never appended to for the 3d plots. When I added the kwarg shared_xaxes=True it works for me.

fig = tools.make_subplots(rows=2, cols=2,
                    specs=[[{'is_3d': True}, {'is_3d': True}],
                            [{'is_3d': True}, {'is_3d': True}]],
                    subplot_titles=('a','b','c','d'), shared_xaxes=True)

enter image description here