user17681970
user17681970

Reputation: 123

How to plot a point on a time series in python

I am facing an issue with plotting points in a time series since I cannot identify the y-axis value. I have 2 datasets: one NetCDF file with satellite data (sea surface temperature), and another CSV file with storm track data (time, longitude, latitude, wind speed, etc.). I can plot the desired temperature time series for all storm track locations located in the ocean. However, I want to indicate the time of the storm footprint occurrence within each time series line. So, one line represents one location and the changing temperature over time, but I also want to show WHEN the storm occurred at that location.

This is my code so far (it works):

lati = stormtrack_lat.values
loni = stormtrack_lon.values

for i, dummy in enumerate(lati):
    dsloc = SSTskin_file.sel(lon=loni[i], lat=lati[i], method='nearest')
    dsloc['analysed_sst'].plot()
    #plt.plot(dsloc,stormtrack_time[i], "or") #here I want to add one point to each line indicating the time when the storm occured at this location
plt.title('My title')
plt.xlabel('time')
plt.ylabel('SST skin in K')

blah

The netcdf file contains the time data as datetime coordinate, but my CSV file contains this data:

|    da   | INDEX | SIZE  |  AREA     |   x     |  y |  RADIUS   |  MEANV

2020021018  1505    2934    177.363     -2.82   49.87   1782.18     16.18

2020021100  1505    3812    220.078     5.25    49.57   2811.51     16.17
...

where 'da' represents the date and time (YYYYMMDDHH). So, I think I need to (1) convert the CSV 'da' values into a datetime format in order to use them for the line plt.plot(dsloc,stormtrack_time[i], "or") and then (2) to find those datetime values within the netcdf file and then (3) use than time point for plotting the corresping SST value/time point.

The problem is that I do not know HOW to do this. Can anyone help?

Thanks!

Upvotes: 3

Views: 1217

Answers (1)

user17681970
user17681970

Reputation: 123

I have found the way to do this:

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)
#plt.legend(lineplot) #Here I would like to plot the legend for the line plots (time series data). I want to indicate the location (longitude and latitude) of the time series
plt.legend(dotplot[:1], ['Storm track at location and time'], fontsize = 16);
fig2.savefig('SSTskin_storm_timeseries_test.png', bbox_inches='tight')

new plot

Upvotes: 1

Related Questions