Skip to content
Advertisement

python, using seaborn or plotly to show graphs for medication points over time

I’m trying to plot my heart rate and steps against when i have coffee.

The dataset is like this,

    heartRate   steps   coffee
created         
2020-04-14 06:03:00 71.0    NaN 0
2020-04-14 09:03:00 72.0    NaN 1
2020-04-14 09:55:00 61.0    NaN 1
2020-04-14 09:58:00 67.0    NaN 1
2020-04-14 10:01:00 82.0    NaN 2

Where 1 is the hour i had coffee and 2 is 4 hours after and 0 is no coffee.

Currently I’m trying to plot these points out like this,

import seaborn as sns
sns.set_theme(style="darkgrid")

# Load an example dataset with long-form data
fmri = tester

# Plot the responses for different events and regions
sns.lineplot(x="created", y="heartRate", style="medication",
             data=fmri)

But this is giving me a unreadable graph like this,

enter image description here

I’d love to have this be more readable – what am i doing wrong with this library?

Advertisement

Answer

Perhaps better take matplotlib. There you can play around with the graph size and the marker and line width to optimize and adjust.

import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd

import numpy as np
from datetime import datetime, timedelta as delta

font = {'family' : 'Arial',
        'weight' : 'normal',
        'size'   : 60}    
rc('font', **font)    
ndays = 1000
start = datetime(2018, 12, 1)
dates = [start - delta(days = x) for x in range(0, ndays)]

df = pd.DataFrame({'time':dates,
                   'heartrate':np.random.randint(60,70,len(dates)),
                   'coffee':np.random.randint(0,2,len(dates))})

df['heartrate_coff'] = df['heartrate']
df['heartrate_nocoff'] = df['heartrate']
df.loc[df['coffee'].values==0,['heartrate_coff'] ]= np.nan
df.loc[df['coffee'].values==1,['heartrate_nocoff']]= np.nan

fig = plt.figure(num=None, figsize=(200, 10), 
                 dpi=9, facecolor='w', edgecolor='k')
plt.plot(df['time'].values , df['heartrate'].values,'k-', linewidth=10)   
plt.plot(df['time'].values , df['heartrate_coff'].values,'rd',markersize=30)   
plt.plot(df['time'].values , df['heartrate_coff'].values,'rd-', linewidth=20)   
plt.plot(df['time'].values , df['heartrate_nocoff'].values,'ko',markersize=10)   
plt.xticks(rotation=-10)

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement