Search code examples
pythonarraysmax

How to plot a line joining maximum point on a spatial plot of a parameter in python? #python


I have a relative vorticity plot at latitude and longitude. Now I want to draw a line over that plot which will join only maximum points at each longitude. I have attached the relative vorticity plot Relative vorticity

I am expecting a line that will join maximum point at each longitude. I have tried the following codePython code But I am getting a 1D array. I need 2d array so that I can get a spatial plot.


Solution

  • I'm assuming you're using xarray, what you're looking for is argmax instead of max. max will grab the actual maximum value, where argmax displays the index of the value. Note that this will produce a 1d array, not a 2d array. I'm not sure what you're using for your graphing, but if the positions on the image are indexes then you can just plot it like you would a normal line, like y = mx + b type of stuff. If you're using matplotlib:

    dset2 = dset1.argmax(dim='latitude')
    plt.plot(range(len(dset2), dset2))
    plt.show()
    
    

    https://docs.xarray.dev/en/stable/generated/xarray.DataArray.argmax.html

    (also you may want to put your original code in the text instead of an image, some people will downvote for that because the url may eventually expire, good luck!)