简体   繁体   English

如何使用底图库在Python的轮廓线上绘制点?

[英]How to plot dots over contourf in Python using Basemap lib?

I am looking for a simple way to plot dots over certain values in a contourf Basemap plot 我正在寻找一种在等高线底图图中将点绘制到特定值上的简单方法

Here is an example code: 这是示例代码:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

dx, dy = 0.5, 0.5

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(-15, 15 + dy, dy),
                slice(-90, -60 + dx, dx)]

z = np.sin(x) + np.cos(5 + y*x) * np.cos(x) + x*y/100
n, m = z.shape
z = z - np.exp(np.random.rand(n,m))

#Setup the map
m = Basemap(projection='merc', llcrnrlat=-15, urcrnrlat=15,\
            llcrnrlon=-90, urcrnrlon=-60, resolution='l')
m.drawcoastlines()

xx, yy = m(x,y)
cs = m.contourf(xx,yy,z,cmap=plt.cm.Spectral_r)
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)

plt.show()

I'd like that where a = np.where(abs(z) > 5,np.nan,z) Basemap plots dots just like in the figure attached 我想在其中a = np.where(abs(z) > 5,np.nan,z)底图绘制点的情况下,如附图所示

例

You can use hatches in a second call to contourf() : 您可以在第二次调用contourf()使用阴影线

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

dx, dy = 0.5, 0.5

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(-15, 15 + dy, dy),
                slice(-90, -60 + dx, dx)]

z = np.sin(x) + np.cos(5 + y*x) * np.cos(x) + x*y/100
n, m = z.shape
z = z - np.exp(np.random.rand(n,m))

#Setup the map
m = Basemap(projection='merc', llcrnrlat=-15, urcrnrlat=15,\
            llcrnrlon=-90, urcrnrlon=-60, resolution='l')
m.drawcoastlines()

xx, yy = m(x,y)
cs = m.contourf(xx,yy,z,cmap=plt.cm.Spectral_r)
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)

##adding hatches:
cs = m.contourf(xx, yy, z, levels=[np.min(z),5,np.max(z)], colors='none',
                  hatches=[None,'.',],
                  extend='lower')

plt.show()

gives the following image: 给出以下图像:

以上代码的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM