英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论