glayne
glayne

Reputation: 175

Plotting two line plots of different index lengths with disconnects where there is no data

I have two data sets that I need to plot on a single pair of axes. Each data set contains two lists:

The problem I am having is that one data set only contains summer months (plotted in red), while the other contains every month (plotted in blue). As a result, I get an image like the one below: Red and blue line plots both extend continuously from left side to right side of a graph.

However, I don't want the line plot for the summer months (red) to connect between years. That is, I don't want to draw a line between data points for 200208 and 200306. I want a gap in the red line where data does not exist, like this graph from Joe Kuan.

Blue line has continuous data. Red line has breaks where no data exist.

The code I used to plot the first image is:

#data1
x = monthly_avgs_aod.index.tolist()
x = [dt.datetime.strptime(i, '%Y%m') for i in x] 
y = monthly_avgs_aod.values.tolist()

#data2
q = monthly_avgs_pm.index.tolist()
q = [dt.datetime.strptime(i, '%Y%m') for i in q] 
w = monthly_avgs_pm.values.tolist()
plt.plot(q, w, '-r')    
plt.plot(x, y, ':b')

The data that I am using looks like this:

#data1
x=['200101','200102','200103','200104','200105','200106','200107','200108','200109','200110','200111','200112','200201','200202','200203','200204','200205','200206','200207','200208','200209']
y=[30.2,37.6,34.7,27.1,31.9,43.9,42.4,42.3,34.4,40.0,47.2,40.8,34.7,27.1,31.9,43.9,42.4,42.3,34.4,40.0,47.2]

#data2   
q=['200106','200107','200108','200206','200207','200208']
w=[19.7,18.6,15.2,17.3,16.9,18.2]

Any help with this would be appreciated.

Upvotes: 1

Views: 2847

Answers (2)

Bennett Brown
Bennett Brown

Reputation: 5383

Create a separate list for each set of contiguous months. Plot each list separately.

sublist_q = [q.pop()] # algorithm reverses order, shouldn't matter
sublist_w = [w.pop()]
while q:
    # Just-popped last month is sublist_q[-1] .
    # Next-to-pop month preceding it in data is q[-1]%100 .
    # Represent next-to-pop December as 0 with q[-1]%100%12
    # so that contiguous months always have a difference of 1
    if sublist_q[-1] - q[-1]%100%12 != 1: # True if months not contiguous
        # We're done with the current sublists. 
        plot(sublist_q, sublist_w, 'r-o') # 'o' to show singletons
        # Reinitialize
        sublist_q = [q.pop()]
        sublist_w = [w.pop()]
    else:
        sublist_q.append(q.pop()) 
        sublist_w.append(w.pop())

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339705

The easiest solution in a case like this where both lists of dates share the same dates (i.e. a month might either be in the list or not), would be to fill the list which contains only the summer months with None values at the positions where there is no data and plot it against the same x that the complete y list is plotted against.

import matplotlib.pyplot as plt
import datetime as dt
#data1
x=['200101','200102','200103','200104','200105','200106','200107','200108',
   '200109','200110','200111','200112','200201','200202','200203','200204',
   '200205','200206','200207','200208','200209']
x = [dt.datetime.strptime(i, '%Y%m') for i in x] 
y=[30.2,37.6,34.7,27.1,31.9,43.9,42.4,42.3,34.4,40.0,47.2,
   40.8,34.7,27.1,31.9,43.9,42.4,42.3,34.4,40.0,47.2]

#data2   
q=['200106','200107','200108','200206','200207','200208']
q = [dt.datetime.strptime(i, '%Y%m') for i in q] 
w=[19.7,18.6,15.2,17.3,16.9,18.2]

for i, xi in enumerate(x):
    if xi not in q:
        w.insert(i, None)


plt.plot(x,y, '-r')    
plt.plot(x,w,':b')


plt.show()

enter image description here

Upvotes: 2

Related Questions