0

The script below creates a figure with time-series data (temperature) for 4 different locations:

enter image description here

The red dots on the lines indicate a storm event at that location.

Now, I want to add a legend for the different time-series graphs (lines) representing the location at which the time-series data was taken (longitude and latitude). The temperature data is one variable of a NetCDF file (time times lat times lon coordinates). I have managed that the 2nd legend does not overlay the first legend, BUT I am still missing that the legend for the lines represents ALL lines, not one only.

Can anyone help? Thanks!

lati = stormtrack_lat.values
loni = stormtrack_lon.values
timei = stormtrack_datetime.values


fig2 = plt.figure(figsize=(20, 20), dpi=300)

for i, dummy in enumerate(lati):
    dsloc = SSTskin_file.sel(lon=loni[i], lat=lati[i], method='nearest')
    dstime = SSTskin_file.sel(time=timei[i], lon=loni[i], lat=lati[i], method='nearest')
    skin_celsius = (dsloc['analysed_sst']) - 273.15
    timesteps = dsloc.time.values
    timestep = dstime.time.values
    timevalue = ((dstime['analysed_sst']).values) - 273.15
    lineplot = plt.plot(timesteps, skin_celsius)
    dotplot = plt.plot(timestep, timevalue, "or") 
plt.title('Skin SST over time at storm track locations', fontsize = 20 )
plt.xlabel('Date', fontsize = 16)
plt.ylabel('Skin SST in $^{\circ}C$',  fontsize = 16)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
legend1 = plt.legend(lineplot, ['Temperature 1', 'Temperature 2', 'Temperature 3', 'Temperature 4'], loc = 1, fontsize = 16);
 #Here I am missing that ALL lines are represented, not only one.
plt.legend(dotplot, ['Storm track footprint at location and time'], loc = 4,  fontsize = 16);
plt.gca().add_artist(legend1)
fig2.savefig('SSTskin_storm_timeseries_test.png', bbox_inches='tight')
user17681970
  • 123
  • 1
  • 1
  • 8
  • 1
    you can pass a list to `plt.legend`. eg: `plt.legend(['Temperature 1', 'Temperature 2', 'Temperature 3', 'Temperature 4'])` –  Jan 10 '22 at 14:25
  • Thanks; I have managed to add a second legend and not to overwrite the first one (see above - I have modified the code accordingly). However, that second legend for the line plot (time series) does only show one line. How can I change that? – user17681970 Jan 10 '22 at 15:05
  • You added a legend containing a list with a single element. You need to call `plt.legend` only once and pass a list with 4 elements (1 for each plot) –  Jan 10 '22 at 15:23
  • Okay; I have again modified the entry and hope you can see my issue now. I am using a list with your example, but the issue is about the loop beforehand as there is only one element leftover after the loop. However, when I am adding the legend into the loop, it still overwrites itself and ends up with only one legend entry. Thanks for your patience. – user17681970 Jan 10 '22 at 15:31
  • 1
    After adding the legend with temperatures in the next line you just call `plt.legend(dotplot, ['Storm track footprint at location and time']` which only has one element. Remove all `plt.legend` calls, then call it once after having plotted everything and pass a list with 4 elements (1 for each plot). You are also saving inside `lineplot` varible just the last plot of your loop, which is wrong. –  Jan 10 '22 at 15:33
  • But I want to have BOTH legends - the legend for the dotplot is as I expect it to be. I only want to call ALL elements for my lineplot legend while creating that legend. So, I want to keep the dotplot legend, and to extend the lineplot legend in the manner that all lines will be represented by that legend. And you are right, I would need to save all lineplot elements within my loop then, but I do not know yet how to do that. – user17681970 Jan 10 '22 at 15:48
  • How about providing an [MCVE](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) instead of guessing what might or might not work. I have also the feeling that some clever pandas grouping might make your life much easier. – Mr. T Jan 10 '22 at 15:55

0 Answers0