带多个记录道的绘图子图

2024-09-27 23:25:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

另一个情节也差不多

现在,我使用找到的以下代码创建子批次:

^{pr2}$

我明白了: enter image description here

如何在子图中同时添加良性和恶性结果? 我似乎无法编辑[0]或[1]来显示这两个图,即在我的子图中有前2个图同时显示恶性和良性,而不是一个或另一个。在


Tags: 代码namego绘图dftracemeanhistogram
1条回答
网友
1楼 · 发布于 2024-09-27 23:25:01

这是一个简单的问题。我不知道为什么这里没有答案。。。虽然已经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

相关问题 更多 >

    热门问题