从中国天气网爬取雷达图,由于获取到的图片均是 png 位图,因此读取图片数据并重新绘图,从而得到质量更高的图。
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
fip = '/home/storm/python/radar/'
fin = '20160929060000000.png'
data = plt.imread(fip fin)
x, y, z = data.shape
cbsd = { 5 : [0, 0, 246],
510 : [1, 160, 246],
1015: [0, 236, 236],
1520: [1, 255, 0],
2025: [0, 200, 0],
2530: [1, 144, 0],
3035: [255, 255, 0],
3540: [231, 192, 0],
4045: [255, 144, 0],
4550: [255, 0, 0],
5055: [214, 0, 0],
5560: [192, 0, 0],
6065: [255, 0, 240],
6570: [120, 0, 132],
7075: [173, 144, 240]}
cbvalue = {5 : 1,
510 : 5,
1015: 10,
1520: 15,
2025: 20,
2530: 25,
3035: 30,
3540: 35,
4045: 40,
4550: 45,
5055: 50,
5560: 55,
6065: 60,
6570: 65,
7075: 70}
x = data.shape[0]
dbz = np.zeros((x, x))
maps = np.zeros((x, x, 3)) 255
for i in sorted(list(cbsd.keys())):
cbna = np.array(cbsd[i])
cdata = data[:, :x,:]*255
rgb1 = np.logical_and(cdata[:,:,0] >= cbna[0] * 0.97, cdata[:,:,0] <= cbna[0] * 1.03)
rgb2 = np.logical_and(cdata[:,:,1] >= cbna[1] * 0.97, cdata[:,:,1] <= cbna[1] * 1.03)
rgb3 = np.logical_and(cdata[:,:,2] >= cbna[2] * 0.97, cdata[:,:,2] <= cbna[2] * 1.03)
route1 = np.logical_and(abs(cdata[:,:,0] - cdata[:, :, 1]) <=10, abs(cdata[:,:,1] - cdata[:, :, 2]) <=10)
route2 = np.logical_and(abs(cdata[:,:,0] - cdata[:, :, 1]) <=10, route1)
# route3 = np.logical_and(route2, abs(cdata[:,:, 1] -146 > 2))
rgb = np.logical_and(rgb1, rgb2)
dbz[np.logical_and(rgb, rgb3)] = cbvalue[i]
# maps[route2] = cdata[route2]
collev = [ "#FFFFFF", "#0000F6", "#01A0F6", "#00ECEC",
"#01FF00", "#00C800", "#019000", "#FFFF00",
"#E7C000", "#FF9000", "#FF0000", "#D60000",
"#C00000", "#FF00F0", "#780084", "#AD90F0"]
cmaps = colors.ListedColormap(collev,'indexed')
cm.register_cmap(name = 'dbzcmap',data = collev,lut = 128)
clevels = range(0, 76, 5)
fig, ax = plt.subplots()
con = ax.contourf(dbz, clevels, cmap = cmaps)
ax.invert_yaxis()
cb = fig.colorbar(con, ax = ax)
cb.set_ticks(clevels)
#ax.hold(True)
#ax.imshow(maps/255)
plt.show()
对比一下两张图可以发现重新绘制后的效果也还是不错的,当然也存在一些问题,原来的图中的地理分界线在重新绘制的图中出现了一些线,这是导致不完美的地方。
两个图的配色还是有一些差异,比如[15, 20], [30, 35] 两个区间。