如何将每个点的值添加到等高线图中?

huangapple go评论95阅读模式
英文:

How can I add the value in every point to a Contour Plot?

问题

现在我正在使用matplotlib生成气候变量的等高线图。现在我想在每个点上添加数值,使得最终的图像看起来像这样:

如何将每个点的值添加到等高线图中?

所以,我想知道如何在图像上显示这些数值。

我目前用于绘制图像的代码如下:

from matplotlib.cm import get_cmap
import numpy as np
from cartopy import crs
from cartopy.feature import NaturalEarthFeature
import matplotlib
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import xarray as xr

from wrf import (getvar, interplevel, vertcross, 
                 CoordPair, ALL_TIMES, to_np,
                 get_cartopy, latlon_coords,
                 cartopy_xlim, cartopy_ylim)


lats, lons = latlon_coords(ctt)

cart_proj = get_cartopy(ctt)

fig = plt.figure(figsize=(12,9))
ax_ctt = fig.add_subplot(1,1,1,projection=cart_proj)

contour_levels = [-10, 0, 10, 20, 30, 40]
ctt_contours = ax_ctt.contourf(to_np(lons), to_np(lats), to_np(ctt),
                               contour_levels, cmap=get_cmap("Blues_r"),
                               transform=crs.PlateCarree())


cb_ctt = fig.colorbar(ctt_contours, ax=ax_ctt, shrink=.60)
cb_ctt.ax.tick_params(labelsize=9)

ax_ctt.set_xlim(cartopy_xlim(ctt))
ax_ctt.set_ylim(cartopy_ylim(ctt))
ax_ctt.gridlines(color="white", linestyle="dotted")

states = NaturalEarthFeature(category="cultural", scale="10m",
                             facecolor="none",
                             name="admin_1_states_provinces")
ax_ctt.add_feature(states, linewidth=0.2, edgecolor="black")
ax_ctt.coastlines('10m', linewidth=0.4)

plt.show()

希望能对如何实现这个目标有所帮助!

英文:

Right now I'm using matplotlib to generate contour plots of climatic variables. Now I wanted to add the numeric value on each point so that the final plot looks something like this:

如何将每个点的值添加到等高线图中?

So, I was wondering if I could get some help on how I could display this values.

The code I currently use to make my plots is the following:

from matplotlib.cm import get_cmap
import numpy as np
from cartopy import crs
from cartopy.feature import NaturalEarthFeature
import matplotlib
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import xarray as xr

from wrf import (getvar, interplevel, vertcross, 
                 CoordPair, ALL_TIMES, to_np,
                 get_cartopy, latlon_coords,
                 cartopy_xlim, cartopy_ylim)


lats, lons = latlon_coords(ctt)

cart_proj = get_cartopy(ctt)

fig = plt.figure(figsize=(12,9))
ax_ctt = fig.add_subplot(1,1,1,projection=cart_proj)

contour_levels = [-10, 0, 10, 20, 30, 40]
ctt_contours = ax_ctt.contourf(to_np(lons), to_np(lats), to_np(ctt),
                               contour_levels, cmap=get_cmap("Blues_r"),
                               transform=crs.PlateCarree())


cb_ctt = fig.colorbar(ctt_contours, ax=ax_ctt, shrink=.60)
cb_ctt.ax.tick_params(labelsize=9)

ax_ctt.set_xlim(cartopy_xlim(ctt))
ax_ctt.set_ylim(cartopy_ylim(ctt))
ax_ctt.gridlines(color="white", linestyle="dotted")

states = NaturalEarthFeature(category="cultural", scale="10m",
                             facecolor="none",
                             name="admin_1_states_provinces")
ax_ctt.add_feature(states, linewidth=0.2, edgecolor="black")
ax_ctt.coastlines('10m', linewidth=0.4)

plt.show()

Any insights in how to do this would be appreciated!

答案1

得分: 1

目前,实现这一目标最简单的方法是使用MetPy的StationPlot类来绘制文本值:

from metpy.plots import StationPlot

sp = StationPlot(ax_ctt, to_np(lons), to_np(lats),
    transform=ccrs.PlateCarree(), fontsize=14)
sp.plot_parameter('C', to_np(ctt))
英文:

Currently, the easiest way to accomplish this would be to use MetPy's StationPlot class to plot the text values:

from metpy.plots import StationPlot

sp = StationPlot(ax_ctt, to_np(lons), to_np(lats),
    transform=ccrs.PlateCarree(), fontsize=14)
sp.plot_parameter('C', to_np(ctt))

huangapple
  • 本文由 发表于 2023年8月8日 23:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定