Plotly python 子图

Posted

技术标签:

【中文标题】Plotly python 子图【英文标题】:Plotly python subplots 【发布时间】:2018-09-11 20:52:18 【问题描述】:

我正在尝试用这两个图制作子图,但无法正常工作。我需要输出是一个“div”,两个图垂直堆叠为子图。通常我只会使用 Matplotlib——但我需要 Plotly 的 3D 带状图。

像这样:

情节 1

情节2

感谢您的帮助!

埃里克

import plotly.graph_objs as go
import plotly.plotly as py
import plotly.figure_factory as ff
import plotly.offline as offline
from plotly import tools
import numpy as np

data0 = list(np.random.normal(-5,.5,25))
data1 = list(np.random.normal(-3.5,1,25))
data2 = list(np.random.normal(0,2,25))
data3 = list(np.random.normal(1,1,25))
data4 = list(np.random.normal(5,3,25))
data5 = list(np.random.normal(7,5,25))
index = list(range(0,len(data0),1))

spectra = [
index,
data0,
data1,
data2,
data3,
data4,
data5
]

spectra = np.transpose(spectra)

traces1 = []
y_raw = spectra[:, 0] # wavelength
sample_size = spectra.shape[1]-1
for i in range(1, sample_size):
z_raw = spectra[:, i]
x = []
y = []
z = []
ci = int(255/sample_size*i) # ci = “color index”
for j in range(0, len(z_raw)):
    z.append([z_raw[j], z_raw[j]])
    y.append([y_raw[j], y_raw[j]])
    x.append([i*2, i*2+1])
    traces1.append(dict(
    z=z,
    x=x,
    y=y,
    colorscale=[ [i, 'rgb(100,%d,255)'%ci] for i in np.arange(0, 1.1, 0.1)],
    showscale = False,
    showlegend = True,
    type='surface',
))

# First subplot
fig1 = 'data':traces1, 'layout':'title':'Ribbon Plot'
div1 = offline.plot(fig1, filename='Distplot with Multiple Datasets',show_link=False, include_plotlyjs=False, output_type='div')

traces2 = [data0, data1, data2, data3, data4, data5]

group_labels = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5']
# Second subplot

fig2 = ff.create_distplot(traces2, group_labels, bin_size=.2)
div2 = offline.plot(fig2, filename='Distplot with Multiple Datasets', show_link=False, include_plotlyjs=False, output_type='div')

【问题讨论】:

情节 1 和 2 是通过“fig1”、“fig2”命令以及“div1”和“div2”命令生成的。 好的,还有一件事:你使用'和'而不是'像'Blackbody'替换为'Blackbody',你能解决你代码中的缩进问题吗? 为了清晰起见,修复了意图并删除了“注释掉”代码。 还有缩进问题以及‘和’。另一方面,您想要哪种布局? plot.ly/python/subplots .. 可能是堆叠子图? 是的,堆叠子图。我不知道你所说的'和'是什么意思。 【参考方案1】:

来自 Plotly 的用户 Empet 在此处发布了我的问题的解决方案:https://plot.ly/~empet/14824

我添加了一些自定义颜色以使绘图看起来更好。

非常有帮助!谢谢Empet!

import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as offline
import plotly.plotly as py
import plotly.figure_factory as ff
import plotly.offline as offline
from plotly import tools
import numpy as np

fig = tools.make_subplots(specs=[['is_3d': True], ['is_3d':False], ['is_3d':False]], vertical_spacing=0.005, rows=3, cols=1)

data0 = list(np.random.normal(-5,.5,25))
data1 = list(np.random.normal(-3.5,1,25))
data2 = list(np.random.normal(0,2,25))
data3 = list(np.random.normal(1,1,25))
data4 = list(np.random.normal(5,3,25))
data5 = list(np.random.normal(7,5,25))
index = list(range(0,len(data0),1))

spectra = [
    index,
    data0,
    data1,
    data2,
    data3,
    data4,
    data5
]

spectra = np.transpose(spectra)

y_raw = spectra[:, 0] # wavelength
sample_size = spectra.shape[1]-1
for i in range(1, sample_size):
    z_raw = spectra[:, i]
    x = []
    y = []
    z = []
    ci = int(255/sample_size*i) # ci = “color index”
    for j in range(0, len(z_raw)):
        z.append([z_raw[j], z_raw[j]])
        y.append([y_raw[j], y_raw[j]])
        x.append([i*2, i*2+1])
    fig.append_trace(dict(
                z=z,
                x=x,
                y=y,
                colorscale=[ [i, 'rgb(100,, 255)'.format(ci)] for i in np.arange(0, 1.1, 0.1)],
                showscale = False,
                showlegend = True,
                type='surface',
                ), 1, 1)


colors = ['rgb(100,000,255)', 'rgb(100,50,255)','rgb(100,100,255)','rgb(100,150,255)','rgb(100,200,255)','rgb(100,250,255)']

traces2 = [data0, data1, data2, data3, data4, data5]

group_labels = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5']

# Second subplot
fig2 = ff.create_distplot(traces2, group_labels, bin_size=.2,colors =.  colors)

for mydata in fig2['data']:
    if mydata['yaxis']=='y1':
        fig.append_trace(mydata, 2, 1)
    else:
        fig.append_trace(mydata, 3, 1)

fig['layout']['scene1'].update(camera=dict(eye=dict(x=.25, y=.25, z=.25)))
fig['layout']['scene1']['domain'].update(y=[0.6, 1])

fig['layout']['yaxis2'].update(domain=[0, 0.1375])
fig['layout']['yaxis1'].update(domain=[0.1575, 0.55])
fig['layout']['xaxis2'].update(zeroline=False)#remove the line x=0 in the lower cell

fig['layout'].update(height=900, width=1000, autosize=True, legend=dict(x=1.1, y=0.40), barmode='overlay')

my_div = offline.plot(fig, filename='Distplot with Multiple Datasets', show_link=False, include_plotlyjs=False, output_type='div')

print(my_div)

【讨论】:

以上是关于Plotly python 子图的主要内容,如果未能解决你的问题,请参考以下文章

Plotly python 子图

matplotlib饼图用主图的实际标题替换最后一个饼图子图标题[重复]

Plotly:热图颜色图例我的子图

Plotly:将多个图形绘制为子图

Plotly Express:处理子图

Plotly:如何使子图的 x 和 y 轴标题更大?