0

is it possible to create plot labels based on conditions? As one can see below, I am creating plots within a loop. The issue is that not all locations of the dataset contain data to plot as time series. Hence I want to label only those timesteps which contain data. I have 8 locations of which only 4 contain data. Currently, a legend for all locations is plotted:

enter image description here

I would like to do something like:

lineplot = plt.plot(timesteps, skin_celsius,  label='%s. storm location' % i with condition if time series contains no NaN data)

is something like this possible?

This is the whole code for the plot

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

# change this loop to extract time series data for day -10 + 10days around stormtrack data.
for i, dummy in enumerate(lati):
    dsloc = SSTskin_subfile_masked.sel(lon=loni[i], lat=lati[i], method='nearest') 
    dstime = SSTskin_subfile_masked.sel(time=timei[i], lon=loni[i], lat=lati[i], method='nearest') #find point data within timeseries data
    skin_celsius = (dsloc['analysed_sst']) - 273.15
    timesteps = dsloc.time.values
    timestep = dstime.time.values
    timevalue = ((dstime['analysed_sst']).values) - 273.15

    #Here I want to add a condition to the labelling. 
    lineplot = plt.plot(timesteps, skin_celsius,  label='%s. storm location' % I) 
    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)

plt.legend()
user17681970
  • 123
  • 1
  • 1
  • 8
  • `is something like this possible?` - Are you asking how to filter the data? Is this really a plotting question? When you attempted to filter the data prior to plotting it what went wrong and how did you try to do it? – wwii Jan 11 '22 at 15:35
  • I have tried this approach before: https://stackoverflow.com/questions/70665736/how-to-select-data-from-netcdf-file-by-specific-variable-value – user17681970 Jan 11 '22 at 17:50

0 Answers0