Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change thickness of lines based on a condition

I’ve got this following graph 1 obtained from this piece of code[2]

enter image description here

2:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
dfMonday = pd.read_csv( "0.Mon.csv", encoding = "ISO-8859-1", sep = ';')

dfSlotMean = dfMonday.groupby('slotID', as_index=False).agg( NMonUn=('date', 'nunique'),NMonTot = ('date', 'count'), MeanBPM=('tempo', 'mean') )
#print(dfSlotMean)

dfSlotMean.drop(dfSlotMean[dfSlotMean.NMonUn< 3].index, inplace=True)
    
df = pd.DataFrame(dfSlotMean)

df.to_csv('1.silMonday.csv', sep = ';', index=False)

print(df)

tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
    ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2)
    ax.xaxis.grid(True)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

This is the output i’ve got:

    slotID  NMonUn  NMonTot     MeanBPM
0        7      11       78  129.700564
2       11       6       63  123.372397
3       12       6       33  120.625667
4       13       5       41  124.516341
5       14       4       43  118.904512
6       15       3       13  116.380538
7       16       3       42  119.670881
8       17       5       40  125.424125
9       18       6       45  130.540578
10      19       9       58  128.180172
11      20       5       44  125.596045

I would like to change the thickness of lines based on ‘NMonUn’ values in order to have thicker lines for higher values and vice versa

>Solution :

You can use the linewidth parameter and set it to the NMonUN value

tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
    ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2, linewidth=r['NMonUN'])
    ax.xaxis.grid(True)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading