色标 - 接近但不够接近 [英] Color scale - close but not close enough
问题描述
我正在尝试制作一个使用与气象局相同色阶的图,以便我可以轻松地将我的图与他们的图进行比较.他们的一个例子是在这里
I'm trying to produce a plot which uses the same colorscale as the Met Office, so I can easily compare my plots to theirs. An example of theirs is at Here
我目前最接近的努力在这里:这里
My current closest effort is here: Here
我很欣赏我的代码很乱 - 我找不到一种方法来为高于某个阈值的值设置颜色(否则它会变成白色),因此循环.
I appreciate my code is messy - I couldn't find a way to set a color for values above a certain threshold (otherwise it goes white),hence the loop.
我想上传 NetCDF 文件,但我没有足够高的代表来执行此操作.
I would upload the NetCDF File but I haven't got a high enough rep to do this.
非常感谢您的帮助.
我的绘图代码如下所示;
My code for plotting is shown below;
from Scientific.IO.NetCDF import NetCDFFile
from mpl_toolkits.basemap import Basemap
from matplotlib import pyplot as plt
import numpy as np
myfile = NetCDFFile('ERA_Dec_89-94.nc', 'r')
Lat = NetCDFFile('/home/james/Documents/Lat_Lon_NC_Files/latitudes_d02.nc','r')
Long = NetCDFFile('/home/james/Documents/Lat_Lon_NC_Files/longitudes_d02.nc','r')
XLAT = Lat.variables['XLAT'][:]
XLONG = Long.variables['XLONG'][:]
ERA_Data = myfile.variables['Monthlyrain'][:]
plot = np.zeros([1000,1730])
plot[:,:] = np.average(ERA_Data[:,:,:],axis=0)
m = Basemap(projection='merc',resolution='f',llcrnrlat=49,llcrnrlon=-11,urcrnrlat=61,urcrnrlon=3)
m.drawparallels(np.arange(-90., 91., 5.), labels=[1,0,0,0], fontsize=11)
m.drawmeridians(np.arange(-180., 181., 5.), labels=[0,0,0,1], fontsize=11)
m.drawcoastlines()
X, Y = m(XLONG, XLAT)
for i in range(0,1729):
for j in range(0,999):
if plot[j,i] >250:
plot[j,i] = 250.001
if plot[j,i] < 40:
plot[j,i] = 40
scale = [40,40.001,60,80,100,125,150,200,250, 250.001]
cs = m.contourf(X,Y,plot,scale, cmap='PuOr')
cbar = m.colorbar(cs, ticks= [40.0005,50,70,90,112.5,137.5,175,225,250.0005])
cbar.set_ticklabels(['<40','40-60', '60-80', '80-100', '100-125', '125-150', '150-200', '200-250', '>250'])
plt.title('Some Title')
cbar.set_label('Monthly average rainfall (mm)')
print "Finished"
plt.show()
推荐答案
第一部分(正确设置颜色)已经回答.为了将值限制在某个范围内,您有多种选择.
The first part (getting the colors right) was already answered. In order to restrict the values to a certain range you have several options.
使用
cmap.set_over
和cmap.set_under
设置越界颜色,如这里
Use
cmap.set_over
andcmap.set_under
to set out-of-bounds colors, as described here
使用np.clip
而不是将值限制在特定范围内的循环:
use np.clip
instead of the loop to restrict the values to a certian range:
plot = np.clip(plot, 40, 250)
这篇关于色标 - 接近但不够接近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!