具有轮廓和水平的问题

2024-04-18 11:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要把每一个区域分成两种颜色蓝色和咖啡色

https://i.stack.imgur.com/GqqPj.png

在我下面的代码中,我在级别中进行了指定,但没有完成划分

lon_0 = lons.mean()
lat_0 = lats.mean()
m = Basemap(projection='merc',llcrnrlat=min_lat,urcrnrlat=max_lat,
            llcrnrlon=min_lon, urcrnrlon=max_lon, resolution='l')
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)

fig = plt.figure(figsize=(10,20))
m.drawparallels(np.arange(-20., 40., 3.), labels=[1,0,0,0], fontsize=12) # Latitude  lines every 5 degrees from -5 to 15. Left zone
m.drawmeridians(np.arange(-90.,-40., 3.), labels=[0,0,0,1], fontsize=12) # longitude lines every 5 degrees from -80 to -45. Down zone

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines(linewidth= 1, color='black')
#m.drawstates()
m.drawcountries(linewidth= 1, color='black')
#m.readshapefile('/home/shared/shapes/bogota/Localidades_Lat_lon', 'Localidades_Lat_lon')
vals = [0.00000,0.00005,0.0001,0.0002,0.0004,0.0006,0.0008,0.0012,0.1]
cmap = mpl.colors.ListedColormap(['#2c7bb6','#0a793a','#77a353','#f1d499','#c96a33','#975114'])

#matplotlib.colors.ListedColormap(colors)

colors = ['#b5277c','#f200ff','#1600ff', "#0188ff", "#02ffaf", "#3db728", "#b2ff00", '#ffe100','#b58c27','#ff0000']
cs = m.contourf(xi, yi, (Agricola.variables['E_PM25J'][0,0,:,:]), cmap=cmap, vmin=0, vmax=np.percentile(np.mean(Agricola.variables['E_PM25J'][:,0,:,:], axis=0), 90.5), levels=vals)
## Add Colorbar
plt.title('Emisiones Agricola', fontsize=20)
cbar = m.colorbar(cs, location='right', pad="2%", size="5%", extend='max')
cbar.set_label("PM2.5 " + "$ug /m^2 s^1$", fontsize=1)
plt.show()

Tags: nppltminmeanmaxcmaploncolors
1条回答
网友
1楼 · 发布于 2024-04-18 11:33:52

TL;DR间隔的编号与颜色编号匹配。使用BoundaryNorm。你知道吗


您的代码有很多问题,首先是ListedColormap中的颜色数与vals列表定义的间隔数之间存在差异(请记住,您希望映射1color←→1interval),其次,您要考虑如何将值映射到颜色映射,而Matplotlib有一些工具可以让您精确地指定将要发生的事情(即^{})。你知道吗

我没有你的数据,所以我试图构建一个平行的,更简单的例子,你应该能够适应你的问题。你知道吗

import matplotlib.colors as mcol
import matplotlib.pyplot as plt
import numpy as np

# dummy data, a sine, 0 in the origin, 1 close to NE corner
x = np.linspace(0, 1, N)
X, Y = np.meshgrid(x, x)
R = np.sqrt(X**2+Y**2)
Z = np.sin(R*3/2)

# the levels that I want to draw on the plot,
# the levels, except for the extremes, are quite arbitrary
# as arbitrary do they seem your level specifications
levels = [0.0, .5, .6, .75, .85, .95, 1.0]
# your colormap
cmap = mcol.ListedColormap(['#2c7bb6', '#0a793a', '#77a353',
                            '#f1d499', '#c96a33', '#975114'])
# the nuber of intervals must be equal to the number of listed colors
assert(len(levels)-1==cmap.N)
# the norm that we use to map values to colors, see the docs    
norm = mcol.BoundaryNorm(levels, cmap.N)

# we are ready to plot
plt.contourf(X, Y, Z, cmap=cmap, levels=levels, norm=norm)
plt.colorbar()

# this is not necessary for your problem but it;s nice in my example    
p = plt.contour(X, Y, Z, levels=levels, colors='k')
plt.clabel(p, inline=1)

#  T H E   E N D
plt.show()

这就导致了a beautiful graph

相关问题 更多 >