Search code examples
pythonmatplotlibline

How to connect all distant points on a plot which follow a condition?


I am plotting my workout weights against "Date Weekday". I want to connect all the points which match the condition:

weights = ([1471.5, 4026. , 2399.5, 5462. ,    0. , 2758.1, 6229. , 5132. ])
dates = ['16 Thu', '20 Mon', '21 Tue', '22 Wed', '23 Thu', '24 Fri','25 Sat','27 Mon']
any(x in dates[i] for x in ["Mon","Thu"])

I saw this question Plotting lines connecting points But this addresses every two points but my problem is that I might skip some days. I want the code to be robust.

I was using this for the above "dates" and "weights":

plt.pyplot.plot(dates,weights,'ro-')
plt.pyplot.xticks(dates)
plt.pyplot.grid(b=True, which='major', color='#666666', linestyle='-')
plt.pyplot.yticks(weights)
plt.pyplot.show()

Which produced this disconnected plot:

Disconnected plot

I want this output:

enter image description here

So I did this:

MonThudate = []
TueFridate = []
WedSatdate = []
MonThuwt = []
TueFriwt = []
WedSatwt = []
for i in range(len(dates)):
    if any(x in dates[i] for x in ["Mon","Thu"]):
        MonThudate.append(dates[i])
        MonThuwt.append(weights[i])
    if any(x in dates[i] for x in ["Tue","Fri"]):
        TueFridate.append(dates[i])
        TueFriwt.append(weights[i])
    if any(x in dates[i] for x in ["Wed","Sat"]):
        WedSatdate.append(dates[i])
        WedSatwt.append(weights[i])
#MonThuDate = ['16 Thu', '20 Mon', '23 Thu', '27 Mon']
#MonThuwt = [1471.5, 4026.0, 0.0, 5132.0]
plt.pyplot.plot(MonThudate,MonThuwt,'ro-')
plt.pyplot.plot(WedSatdate,WedSatwt,'ro-')
plt.pyplot.plot(TueFridate,TueFriwt,'ro-')

It produced this:

Connected Graph

But here, the x-axis is not the same for the whole graph. It is disjoint.


Solution

  • So basically you want to connect only certain days of the week. Then you need to label those date with the correct group label and plot on each group:

    # create day group:
    day_groups = [['Thu','Mon'], ['Tue','Fri'], ['Wed', 'Sat'],['Sun']]
    day_dict = {day:i for i,groups in enumerate(day_groups) for day in groups}
    # {'Thu': 0, 'Mon': 0, 'Tue': 1, 'Fri': 1, 'Wed': 2, 'Sat': 2, 'Sun': 3}
    
    # label the days in the given data set:
    # if df.dates is datetime, we can do df.dates.dt.day_name().str[:3]
    day_labels = df.dates.str.split(expand=True)[1].map(day_dict)
    
    # groupby and plot:
    
    fig, ax = plt.subplots(figsize=(10,6))
    df.weights.groupby(day_labels).plot(ax=ax, marker='o')
    
    ax.set_xticklabels([0] + df.dates.tolist())
    ax.grid(b=True, which='major', color='#666666', linestyle='-')
    plt.show()
    

    Output:

    enter image description here

    Note: you are recommended to convert your df.dates into datetime for automatically arrangement of the x-axis.