英文:
How to plot a line joining maximum point on a spatial plot of a parameter in python? #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.
答案1
得分: 0
我假设你在使用 xarray
,你需要用 argmax
而不是 max
。max
会获取实际的最大值,而 argmax
则会显示该值的索引。请注意,这将生成一个一维数组,而不是二维数组。我不确定你在图表上使用的是什么,但如果图像上的位置是索引,你可以像绘制普通线条一样绘制它,比如 y = mx + b
类型的东西。如果你正在使用 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
(另外,你可能想将你的原始代码放在文本中,而不是放在图像中,因为网址可能最终会失效,祝你好运!)
英文:
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!)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论