简体   繁体   English

Matplotlib 离散颜色条,用于绘制不同的土地利用类别

[英]Matplotlib discrete colorbar for plotting different landuse categories

I tried to define a discrete color bar to plot different land categories using separate colors for each category.我试图定义一个离散的颜色条来绘制不同的土地类别,为每个类别使用不同的颜色。 I tried with the following code.我尝试使用以下代码。 I am not getting the red/tomato color for the first chosen category ie "Urban".对于第一个选择的类别,即“城市”,我没有得到红色/番茄颜色。 The color bar is starting with plum color.颜色条以梅色开始。 How can I resolve it?我该如何解决?

My other query is can we put the color bar tick at the middle of discrete color code on the color bar?我的另一个问题是我们可以在颜色条上的离散颜色代码的中间放置颜色条刻度吗?

I am not attaching my dataset here.我没有在这里附上我的数据集。 If it's required any random array can be created.如果需要,可以创建任何随机数组。 Landuse categories are nothing but we represent different categories with few specified numbers(eg: 1 for urban-city, 13 for the forest, 9 for snow etc.)土地利用类别什么都不是,但我们用很少的指定数字代表不同的类别(例如:1 代表城市,13 代表森林,9 代表雪等)

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.colors
from matplotlib.colors import ListedColormap
import numpy as np

col_dict = {
    1:"tomato",
    2:"plum",
    7:"lightgreen",
    11:"green",
    15:"maroon",
    16:"aqua",
    17:"navy",
    19:"goldenrod",
    22:"white",
}
labels = np.array([
    "Urban",
    "I Cropland",
    "Grassland",
    "B Deciduous",
    "Mixed Forest",
    "Water Bodies",
    "H Wetland", 
    "Barren",
    "Tundra",
])
crs = ccrs.PlateCarree()
ax = plt.subplot(111, projection=crs)
cm = ListedColormap([col_dict[x] for x in col_dict.keys()])
conf = ax.contourf(lonn, latt, lu_index, cmap=cm)

So you are only part way there with the colormap.所以你只是在使用颜色图的一部分。 You need to define levels for your contours, and you need to use a BoundaryNorm.您需要为轮廓定义级别,并且需要使用 BoundaryNorm。 Taking your projection out of the problem and just making equal stripes, I've made the contour levels exactly half way between your discrete values.把你的投影排除在问题之外,只是制作相等的条纹,我已经在你的离散值之间制作了正好一半的轮廓水平。 If you don't do this, the linear interpolation between data will put the contour towards one side or another.如果不这样做,数据之间的线性插值将使轮廓朝向一侧或另一侧。

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.colors import ListedColormap
import numpy as np

col_dict = {
    1:"tomato",
    2:"plum",
    7:"lightgreen",
    11:"green",
    15:"maroon",
    16:"aqua",
    17:"navy",
    19:"goldenrod",
    22:"white",
}
labels = np.array([
    "Urban",
    "I Cropland",
    "Grassland",
    "B Deciduous",
    "Mixed Forest",
    "Water Bodies",
    "H Wetland", 
    "Barren",
    "Tundra",
])
cm = ListedColormap([col_dict[x] for x in col_dict.keys()])
np.random.seed(2112)
lu_index = np.zeros((9, 9))
for nn,k in enumerate(col_dict.keys()):
    lu_index[nn, :] = k

bounds = np.array([k for k in col_dict.keys()])
bounds = bounds[:-1] + np.diff(bounds) / 2
bounds = np.concatenate((np.array([0]), bounds, np.array([24])))
norm = mcolors.BoundaryNorm(bounds, 9)

fig, axs = plt.subplots(1, 3, constrained_layout=True, figsize=(6, 4))
ax = axs[0]
conf = ax.contourf(lu_index, cmap=cm)
ax.set_title('No Boundary Norm, \nno levels')

ax = axs[1]
conf = ax.contourf(lu_index, cmap=cm, levels=bounds)
ax.set_title('No Boundary Norm, \nlevels')

ax = axs[2]
conf = ax.contourf(lu_index, cmap=cm, norm=norm, levels=bounds)
ax.set_title('Boundary Norm, \nlevels')

在此处输入图片说明

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

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