如何在作品图中添加传奇?

发布于 2025-02-06 07:28:35 字数 917 浏览 1 评论 0原文

我目前正在使用一些涉及土地使用类型(即森林,沙漠,水等)的数据,这些数据在我的车展图上以不同的颜色出现。我想添加一个传奇或同等学历,以显示每种颜色代表哪种土地类型,但似乎无法使其起作用。在操作映射的顶部,土地使用类型的数据绘图需要我使用PCOLORMESH。是否有任何方法可以将各种传说显示在表明每种土地使用类型的哪种颜色的彩色图中?

以下是我代码的作品部分:

proj = ccrs.LambertConformal(central_longitude=cLon, central_latitude=cLat)
res = '10m'
fig_1 = plt.figure(figsize=(20,20))
ax_1 = plt.subplot(1,1,1,projection=proj)
ax_1.set_extent([lonW,lonE,latS,latN])
ax_1.add_feature(cfeature.LAND.with_scale(res))
ax_1.add_feature(cfeature.OCEAN.with_scale(res))
ax_1.add_feature(cfeature.COASTLINE.with_scale(res))
ax_1.add_feature(cfeature.LAKES.with_scale(res), alpha = 0.5)
ax_1.add_feature(cfeature.STATES.with_scale(res));
ax_1.set_title('Land Use Type: Western United States', fontsize=20)
ax_1.pcolormesh(cat_land_lons, cat_land_lats, cat_land, transform=ccrs.PlateCarree(), zorder=3)
ax_1.legend(['Forested', 'Water', 'Desert', 'Farmland', 'Urban'])

I'm currently working with some data that involves the land use type (i.e., forested, desert, water, etc.) that comes up as different colors on my cartopy maps. I want to add a legend or equivalent that shows which land type each color represents, but can't seem to get it to work. The land use type data plotting on top of the cartopy map requires me using pcolormesh. Is there any way to get a legend of sorts onto the cartopy plot that shows which color each land use type is?

Below is my cartopy portion of my code:

proj = ccrs.LambertConformal(central_longitude=cLon, central_latitude=cLat)
res = '10m'
fig_1 = plt.figure(figsize=(20,20))
ax_1 = plt.subplot(1,1,1,projection=proj)
ax_1.set_extent([lonW,lonE,latS,latN])
ax_1.add_feature(cfeature.LAND.with_scale(res))
ax_1.add_feature(cfeature.OCEAN.with_scale(res))
ax_1.add_feature(cfeature.COASTLINE.with_scale(res))
ax_1.add_feature(cfeature.LAKES.with_scale(res), alpha = 0.5)
ax_1.add_feature(cfeature.STATES.with_scale(res));
ax_1.set_title('Land Use Type: Western United States', fontsize=20)
ax_1.pcolormesh(cat_land_lons, cat_land_lats, cat_land, transform=ccrs.PlateCarree(), zorder=3)
ax_1.legend(['Forested', 'Water', 'Desert', 'Farmland', 'Urban'])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2025-02-13 07:28:35

在不知道您的数据的情况下,很难具体。但是给出了一些示例陆生数据和分配的颜色。如果您的LCC数据不是这样的,情况可能会有所不同。

lc_colors = {
    'Forested': "g", # value=0
    'Water': "b",    # value=1
    'Desert': "y",   # value=2
    'Farmland': "c", # value=3
    'Urban': "r",    # value=4
}

yy, xx = np.mgrid[35:45:1, -120:-110:1]
zz = np.random.randint(0, len(lc_colors), xx.shape)

您可以定义一个colormap和归一化器:

cmap = mpl.colors.LinearSegmentedColormap.from_list("lcc", list(lc_colors.values()))

bounds = np.arange(len(lc_colors)+1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

使用对于传说:

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.patches as mpatches
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np

proj = ccrs.LambertConformal(central_longitude=-110, central_latitude=40)

fig, ax = plt.subplots(figsize=(6,6), facecolor="w", dpi=86, subplot_kw=dict(projection=proj))
ax.set_title('Land Use Type: Western United States', fontsize=15)

im = ax.pcolormesh(xx, yy, zz, transform=ccrs.PlateCarree(), cmap=cmap, norm=norm)
ax.set_extent([-125, -95, 30, 50], crs=ccrs.PlateCarree())

res = '110m'
ax.add_feature(cfeature.LAND.with_scale(res))
ax.add_feature(cfeature.OCEAN.with_scale(res))
ax.add_feature(cfeature.COASTLINE.with_scale(res))
ax.add_feature(cfeature.LAKES.with_scale(res), alpha=0.5)
ax.add_feature(cfeature.STATES.with_scale(res), lw=0.5, alpha=.5)

labels, handles = zip(*[(k, mpatches.Rectangle((0, 0), 1, 1, facecolor=v)) for k,v in lc_colors.items()])
ax.legend(handles, labels, loc=4, framealpha=1)

”在此处输入图像描述”

或不使用上面摘要中的最后两行,而是将pcoLormesh的结果与colorbar:

cb = fig.colorbar(im, ax=ax, shrink=.3, aspect=8)
cb.set_ticks(bounds[:-1]+0.5)
cb.set_ticklabels(lc_classes.keys())

从Dataviz的角度来看,使用代理艺术家使用适当的传奇可能会更好,因为配色栏可能会向这些类建议某种顺序。

It difficult to be specific without knowing what your data looks like. But given some example Landcover data and assigned colors. Things might be a little different if your LCC data is not consecutive like this.

lc_colors = {
    'Forested': "g", # value=0
    'Water': "b",    # value=1
    'Desert': "y",   # value=2
    'Farmland': "c", # value=3
    'Urban': "r",    # value=4
}

yy, xx = np.mgrid[35:45:1, -120:-110:1]
zz = np.random.randint(0, len(lc_colors), xx.shape)

You can define a colormap and normalizer:

cmap = mpl.colors.LinearSegmentedColormap.from_list("lcc", list(lc_colors.values()))

bounds = np.arange(len(lc_colors)+1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

Using proxy artists for the legend:

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.patches as mpatches
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np

proj = ccrs.LambertConformal(central_longitude=-110, central_latitude=40)

fig, ax = plt.subplots(figsize=(6,6), facecolor="w", dpi=86, subplot_kw=dict(projection=proj))
ax.set_title('Land Use Type: Western United States', fontsize=15)

im = ax.pcolormesh(xx, yy, zz, transform=ccrs.PlateCarree(), cmap=cmap, norm=norm)
ax.set_extent([-125, -95, 30, 50], crs=ccrs.PlateCarree())

res = '110m'
ax.add_feature(cfeature.LAND.with_scale(res))
ax.add_feature(cfeature.OCEAN.with_scale(res))
ax.add_feature(cfeature.COASTLINE.with_scale(res))
ax.add_feature(cfeature.LAKES.with_scale(res), alpha=0.5)
ax.add_feature(cfeature.STATES.with_scale(res), lw=0.5, alpha=.5)

labels, handles = zip(*[(k, mpatches.Rectangle((0, 0), 1, 1, facecolor=v)) for k,v in lc_colors.items()])
ax.legend(handles, labels, loc=4, framealpha=1)

enter image description here

Or instead of using the last two lines from the snippet above, use the result from pcolormesh with a colorbar:

cb = fig.colorbar(im, ax=ax, shrink=.3, aspect=8)
cb.set_ticks(bounds[:-1]+0.5)
cb.set_ticklabels(lc_classes.keys())

enter image description here

Using a proper legend with the proxy artists is probably better from a dataviz perspective, since a colorbar might suggest some sort of order to the classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文