python - Plotting two line plots of different index lengths with disconnects where there is no data -
i have 2 data sets need plot on single pair of axes. each data set contains 2 lists:
- dates (in format of yyyymm)
- values (monthly averages)
the problem having 1 data set contains summer months (plotted in red), while other contains every month (plotted in blue). result, image 1 below:
however, don't want line plot summer months (red) connect between years. is, don't want draw line between data points 200208 , 200306. want gap in red line data not exist, graph joe kuan.
the code used plot first image is:
#data1 x = monthly_avgs_aod.index.tolist() x = [dt.datetime.strptime(i, '%y%m') in x] y = monthly_avgs_aod.values.tolist() #data2 q = monthly_avgs_pm.index.tolist() q = [dt.datetime.strptime(i, '%y%m') in q] w = monthly_avgs_pm.values.tolist() plt.plot(q, w, '-r') plt.plot(x, y, ':b')
the data using looks 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 appreciated.
the easiest solution in case both lists of dates share same dates (i.e. month might either in list or not), fill list contains summer months none
values @ positions there no data , plot against same x
complete y
list plotted against.
import matplotlib.pyplot plt import datetime 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') 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') in q] w=[19.7,18.6,15.2,17.3,16.9,18.2] 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()
Comments
Post a Comment