如何根据特定范围获得具有不同颜色的散点图的散点

ymzxtsji  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(364)

希望每个人都在这艰难的时刻做得很好。我正试图在底图上用散布点绘制气旋的轨迹。我成功地绘制了它,但是我想要的是散点图按照一定的范围用不同的颜色绘制,比如黑色小于17节,蓝色介于17-27节,青色介于27-33节,绿色介于33-47节,橙色介于47-63节,红色大于120节。我能用不同的颜色绘制散点图,但不能按范围绘制。因此,如果有人能帮助我,告诉我怎么做,将不胜感激。我使用的代码和生成的绘图附在下面。

import os
os.environ["PROJ_LIB"] = "C:\\Utilities\\Python\\Anaconda\\Library\\share"; #fixr
from mpl_toolkits.basemap import Basemap
import proplot as plot
import pandas as pd
import matplotlib as mpl
m=Basemap()

# dt = pd.read_excel('E:/tracks.xlsx',sheet_name='amp')

data = [25,25,35,40,45,55,70,80,100,120,125,130,125,115,105,100,95]
lon= [87,86.3,86.3,86.1,86,86,86,86.1,86.3,86.2,86.3,86.5,86.7,86.9,87,87.2,87.5]
lat= [10.4,10.9,10.9,11.1,11.4,11.5,12,12.5,13.2,13.4,14,14.9,15.6,16.5,17.4,18.4,19.1]
cmap = mpl.cm.get_cmap('bwr')
norm = mpl.colors.Normalize(vmin=25, vmax=130)
colors = cmap(norm(data))
x,y=m(lon,lat)
fig, axs = plot.subplots(ncols=1, axwidth=3, proj='merc', proj_kw={'lon_0': 180})
axs.format(
latlim=(0, 30), lonlim=(50, 100),linewidth=1,
gridlinewidth=0,coast=True, latlines=30, lonlines=60,)
l=axs.scatter(x,y,c=colors,s=10)
fig.colorbar(colors)

c90pui9n

c90pui9n1#

你可以用 ListedColormapBoundaryNorm 生成自定义离散色条。下面是一个使用 cartopy :

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib as mpl

data = [25,25,35,40,45,55,70,80,100,120,125,130,125,115,105,100,95]
lon = [87,86.3,86.3,86.1,86,86,86,86.1,86.3,86.2,86.3,86.5,86.7,86.9,87,87.2,87.5]
lat = [10.4,10.9,10.9,11.1,11.4,11.5,12,12.5,13.2,13.4,14,14.9,15.6,16.5,17.4,18.4,19.1]

cmap = mpl.colors.ListedColormap(['blue', 'cyan', 'green', 'orange'])
norm = mpl.colors.BoundaryNorm([17, 27, 33, 47, 63], cmap.N) 
colors = cmap(norm(data))

cmap.set_under('black')
cmap.set_over('red')

fig, ax = plt.subplots(figsize=(10, 5), subplot_kw={'projection': ccrs.PlateCarree()})
ax.coastlines() # draw coastlines
ax.set_extent([65, 95, 5, 25]) # zoom into India
ax.scatter(lon, lat, s=20, c=colors, cmap=cmap, norm=norm, transform=ccrs.Geodetic())
fig.colorbar(mpl.cm.ScalarMappable(norm, cmap), extend='both')

相关问题