Black Lives Matter. Please consider donating to Black Girls Code today.

Chart plotting physiological Blood pressure in Python3

Hello, I’m having trouble finding a way to plotting blood pressure. Ideally, I would like to have a floating box, where the upper limit of the box is the systolic pressure and the lower limit is diastolic. I’ve tried to use box plots but when they encounter zero values weird things start happening, whiskers disappear, values of adjacent bp disappear. I thought about waterfall plots but they depend on adjacent fields to set their value and that wouldn’t work. Any ideas would be greatly appreciated as I’m at my wits end. Thanks!

from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
import plotly.graph_objs as go
from plotly import tools

trace = []
trace2 = []
group_a = (['135.0', '96.0', '75.0'], ['133.0', '86.0', [0]], [[0], [0], [0]], ['133.0', '86.0', [0]])
x = (['10:00', '10:00', '10:00'], ['10:05', '10:05', '10:05'], ['10:10', '10:10', '10:10'], ['10:15', '10:15', '10:15'])
hr = [[63], [53], [0], [53]]
     
     

for i in range(0,len(group_a)):
               trace.append(go.Box(y= group_a[i],
           x=x[i],
           name="BP",
           hoverinfo='none'))
        

for a in range(0,len(hr)):
            trace2.append(go.Scatter(y= hr[a],
            x=x[a],
            name='Heart rate at Time x',
            marker=dict(size=[20]),
            text=['(Time, hr)']))

# layout = go.Layout(
#     yaxis=go.layout.YAxis(
#         title='Blood Pressure',
#         range=[0, 180]))

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

fig.append_trace(trace[0],1,1)
fig.append_trace(trace2[0],1,1)

fig.append_trace(trace[1],1,1)
fig.append_trace(trace2[1],1,1)

fig.append_trace(trace[2],1,1)
fig.append_trace(trace2[2],1,1)
fig.update_yaxes(title_text="Blood Presure", row=1, col=1)
fig.update_xaxes(title_text="Time", row=1, col=1)


fig.layout.update(title='Blood Pressure',
                  showlegend=False)



iplot(fig)

Hi @romarcin,

Welcome to plotly forum!

  • You are using an old version of make_subplots and possibly of Plotly. The latest Plotly version is 4.6.5.
  • You defined a subplot with 1 row and 2 columns but you added traces only to row=1 col=1
  • In a Box plot definition y must be a 1d array or a simple list, but you passed to the third Box trace,
    y = [[0], [0], [0]]

I removed the inner lists and modified the code as follows:

from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
import plotly.graph_objs as go
from plotly.subplots import make_subplots

trace = []
trace2 = []
group_a = (['135.0', '96.0', '75.0'], ['133.0', '86.0', 0], [0, 0, 0], ['133.0', '86.0', 0])
x = (['10:00', '10:00', '10:00'], ['10:05', '10:05', '10:05'], ['10:10', '10:10', '10:10'], ['10:15', '10:15', '10:15'])
hr = [[63], [53], [0], [53]]
     
     

for i in range(0,len(group_a)):
               trace.append(go.Box(y= group_a[i],
           x=x[i],
           name=f"BP{i+1}",
           hoverinfo='none'))
        

for a in range(0,len(hr)):
            trace2.append(go.Scatter(y= hr[a],
            x=x[a],
            name='Heart rate at Time x{a}',
            marker=dict(size=[20]),
            text=['(Time, hr)']))

# layout = go.Layout(
#     yaxis=go.layout.YAxis(
#         title='Blood Pressure',
#         range=[0, 180]))

fig = make_subplots(rows =1,
                        cols =1)

fig.add_trace(trace[0],1,1)
fig.add_trace(trace2[0],1,1)

fig.add_trace(trace[1],1,1)
fig.add_trace(trace2[1],1,1)

fig.add_trace(trace[2],1,1)
fig.add_trace(trace2[2],1,1)
#fig.add_yaxes(title_text="Blood Presure", row=1, col=1)
#fig.add_xaxes(title_text="Time", row=1, col=1)


fig.layout.update(width =600, height=450,
                  title_text='Blood Pressure', title_x=0.5,
                  showlegend=False,
                  xaxis_title='Time',
                  yaxis_title='Blood Pressure')
iplot(fig)

and got this plot:
blood

Is this image what you are expecting? If not, please explain why, and give more details.

1 Like

You’re a life saver!