具有多条迹线的情节子图

时间:2017-10-15 00:04:03

标签: python plotly subplot

我正在尝试为以下图表创建子图:

enter image description here

enter image description here

我用于绘图的代码是:

radius_mean1 = df[df['diagnosis']==1]['radius_mean']
radius_mean0 = df[df['diagnosis']==0]['radius_mean']

trace_rm1 = go.Histogram(x = radius_mean1, opacity = 0.75, name = 'malignant')
trace_rm2 = go.Histogram(x = radius_mean0, opacity = 0.75, name = 'benign')

data2 = [trace_rm1,trace_rm2]
layout2 = go.Layout(barmode = 'overlay', title = 'radius mean')
fig2 = go.Figure(data=data2, layout=layout2)

py.iplot(fig2)

和其他情节相似

现在我使用以下代码创建子图:

fig.append_trace(fig1['data'][1], 1, 1)
fig.append_trace(fig2['data'][0], 2, 1)
py.iplot(fig)

我得到了这个: enter image description here

如何在子图中添加良性和恶性结果? 我似乎无法编辑[0]或[1]以显示两者,即将前两个图完全显示在我的子图中,显示恶性和良性,而不仅仅是一个或另一个。

1 个答案:

答案 0 :(得分:5)

这是一个简单的问题。我不知道为什么这里没有答案...尽管已经过去11个月了,但我还是在这里回答。

这里的数据不完整,所以我使用了一些演示数据。

import numpy as np
import plotly.graph_objs as go
import plotly

a = np.random.normal(0,1,100)
b = np.random.normal(-2,5,100)

c = np.random.normal(0,1,100)
d = np.random.normal(-2,5,100)

fig = plotly.tools.make_subplots(rows=2,cols=1)

trace_rm1 = go.Histogram(x = a, opacity = 0.75, name = 'malignant')
trace_rm2 = go.Histogram(x = b, opacity = 0.75, name = 'benign')
fig.append_trace(go.Histogram(x = a, opacity = 0.75, name = 'benign'),1,1)
fig.append_trace(go.Histogram(x = b, opacity = 0.75, name = 'malignant'),1,1)
fig.append_trace(go.Histogram(x = c, opacity = 0.75, name = 'benign'),2,1)
fig.append_trace(go.Histogram(x = d, opacity = 0.75, name = 'malignant'),2,1)
fig.layout.update(go.Layout(barmode = 'overlay',))

plotly.offline.plot(fig)

result