14

i am trying to create subplots for the following plots:

enter image description here

enter image description here

the code i used for the the plots are :

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)

and similar for the other plot

now i use the following code i found to create subplots:

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

and i get this : enter image description here

how do i add both benign and malignant results to the subplots ? i cant seem to edit the [0] or [ 1] to show both ie have the first 2 plots exactly into my subplot showing both malignant and benign and not just one or the other.

1 Answer 1

22

The data here is not complete, so I using some demo data to present.

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

Sign up to request clarification or add additional context in comments.

4 Comments

Would it be possible to give each of the two plots a title?
@A.P. Of course, you could just fill the param of plotly.tools.make_subplots, there is a param called subplot_titles. It actually adds two annotations automatically for you.
For the subplot in row=1, column=1: can you shorten the commands for plotting the 2 histograms, so that they fit into 1 line? Or can you have one command for plotting multiple histograms at once?
@NeStack but first you need to have a combined data with indicator of different groups? or something else. you could see the plotly express for help I think. plotly.com/python/plotly-express

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.