python - 如何在plotly中将子图标题添加到3D子图

标签 python plotly

我正在绘制带有 4 个子图的图像,例如使用以下代码。 如何为每个子图添加标题?

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')

我尝试更换

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

具有以下内容

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}]])

它给了我以下错误

<小时/>

IndexError Traceback(最近一次调用最后一次) 在 () 33 Fig = tools.make_subplots(rows=2, cols=2,subplot_titles= ('p1','p2','p3','p4'), 34 规范=[[{'is_3d': True}, {'is_3d': True}], ---> 35 [{'is_3d': True}, {'is_3d': True}]]) 36 37 # 将曲面添加到子图中。

~\AppData\Local\Continuum\anaconda3\lib\site-packages\plotly\tools.py 在 make_subplots(行、列、shared_xaxes、shared_yaxes、start_cell、print_grid、**kwargs) 第1373章 第1374章 -> 1375plot_titles.append({'y': subtitle_pos_y[索引], 1376 'xref': '纸张', 第1377章

索引错误:列表索引超出范围

我该如何解决这个问题? 谢谢

最佳答案

对我来说这看起来像是一个错误。 Here似乎 list_of_domains 从未附加到 3d 图。当我添加 kwarg shared_xaxes=True 它对我有用。

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

关于python - 如何在plotly中将子图标题添加到3D子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991810/

相关文章:

python - 如何读取二进制文件并将数据转换为图像?

python - 装饰器调用实例方法

r - 向聚类散点图 (tSNE) 添加文本注释

python - 这个 html5lib 脚本是怎么回事?

python - 如何在 View 中的 Odoo 11 中从组标签中删除标签

r - 在plotly R中映射标记大小时出现虚假警告

python - 如何将条形图的 x + y 轴与绘图上给定的数据帧列相关联

python - plotly 表达: how to control bars start position?

python - 如何在脚本中使用 Python REPL

r - 使用ggplotly时如何选择要在工具提示中显示的变量?