如何更改 matplotlib 中 contours/colorbar 的间隔以更详细地可视化温度梯度?
How to change the interval of contours/colorbar in matplotlib to visualize temperature gradient in finer detail?
我正在尝试可视化数据集的温度场,并尝试通过使用 matplotlib 和 cartopy 绘制它来实现。我已经成功地创建了一幅总体图景,但我正试图弄清楚如何纠正一个主要缺陷。我想让轮廓间隔更小(1 开尔文或 0.5 开尔文间隔)以正确显示数据集的微小细节。现在,我的身材是这样的:
Potential Temperature w/ inappropriate interval
可以看到大体,但是细节完全丢失了。我该如何解决这种情况,并在我的温度场中查看更详细的信息。
相关代码:
# FOR SINGLE PLOT ONLY
# Get WRF variables
theta_2m = getvar(ds, 'TH2')
wrf_lats, wrf_lons = latlon_coords(theta_2m)
wrf_lons = to_np(wrf_lons)
wrf_lats = to_np(wrf_lats)
# Timestamp
timestamp = to_np(theta_2m.Time).astype('M8[s]').astype('O').isoformat()
time = theta_2m.Time
time_str = str(time.values)
# Get cartopy projection from data set
cart_proj = get_cartopy(theta_2m)
# Plot the relevant data
fig = plt.figure(figsize=(12,6))
ax = plt.axes(projection=cart_proj)
plt.contour(wrf_lons, wrf_lats, theta_2m, colors='black', transform=ccrs.PlateCarree())
plt.contourf(wrf_lons, wrf_lats, theta_2m, transform=ccrs.PlateCarree(), cmap=get_cmap('coolwarm'))
plot_background(ax)
plt.colorbar(ax=ax, shrink=.98)
ax.set_extent([-104.35, -94.45, 36.37, 44.78])
ax.set_title('2m Potential Temperature (K) ' + time_str[:19])
plt.show()
在plt.contour
中可以用levels
参数设置轮廓线的间隔:
max_level = 2
min_level = -2
step_level = 0.5
ax.contour(xx, yy, zz, colors = 'black', levels = np.arange(min_level, max_level + step_level, step_level))
完整代码
import numpy as np
import matplotlib.pyplot as plt
N = 100
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
zz = np.sin(xx) + np.sin(yy)
max_level = 2
min_level = -2
step_level = 0.5
fig, ax = plt.subplots(figsize = (6, 6))
ax.contour(xx, yy, zz, colors = 'black', levels = np.arange(min_level, max_level + step_level, step_level))
ax.contourf(xx, yy, zz, levels = np.arange(min_level, max_level + step_level, step_level))
plt.show()
step_level = 0.5
step_level = 0.1
我正在尝试可视化数据集的温度场,并尝试通过使用 matplotlib 和 cartopy 绘制它来实现。我已经成功地创建了一幅总体图景,但我正试图弄清楚如何纠正一个主要缺陷。我想让轮廓间隔更小(1 开尔文或 0.5 开尔文间隔)以正确显示数据集的微小细节。现在,我的身材是这样的:
Potential Temperature w/ inappropriate interval
可以看到大体,但是细节完全丢失了。我该如何解决这种情况,并在我的温度场中查看更详细的信息。
相关代码:
# FOR SINGLE PLOT ONLY
# Get WRF variables
theta_2m = getvar(ds, 'TH2')
wrf_lats, wrf_lons = latlon_coords(theta_2m)
wrf_lons = to_np(wrf_lons)
wrf_lats = to_np(wrf_lats)
# Timestamp
timestamp = to_np(theta_2m.Time).astype('M8[s]').astype('O').isoformat()
time = theta_2m.Time
time_str = str(time.values)
# Get cartopy projection from data set
cart_proj = get_cartopy(theta_2m)
# Plot the relevant data
fig = plt.figure(figsize=(12,6))
ax = plt.axes(projection=cart_proj)
plt.contour(wrf_lons, wrf_lats, theta_2m, colors='black', transform=ccrs.PlateCarree())
plt.contourf(wrf_lons, wrf_lats, theta_2m, transform=ccrs.PlateCarree(), cmap=get_cmap('coolwarm'))
plot_background(ax)
plt.colorbar(ax=ax, shrink=.98)
ax.set_extent([-104.35, -94.45, 36.37, 44.78])
ax.set_title('2m Potential Temperature (K) ' + time_str[:19])
plt.show()
在plt.contour
中可以用levels
参数设置轮廓线的间隔:
max_level = 2
min_level = -2
step_level = 0.5
ax.contour(xx, yy, zz, colors = 'black', levels = np.arange(min_level, max_level + step_level, step_level))
完整代码
import numpy as np
import matplotlib.pyplot as plt
N = 100
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
zz = np.sin(xx) + np.sin(yy)
max_level = 2
min_level = -2
step_level = 0.5
fig, ax = plt.subplots(figsize = (6, 6))
ax.contour(xx, yy, zz, colors = 'black', levels = np.arange(min_level, max_level + step_level, step_level))
ax.contourf(xx, yy, zz, levels = np.arange(min_level, max_level + step_level, step_level))
plt.show()
step_level = 0.5
step_level = 0.1