![](/img/trans.png)
[英]Setting longitude of latitude tick labels in NorthPolarStereo Cartopy
[英]How to Add More Lines of Longitude/latitude in NorthPolarStereo Projection
我正在嘗試增加在 Python 中的 NorthPolarStereo 投影上出現經線的間隔。 我曾嘗試增加/定義刻度/lim(對於 x 和 y 坐標)並返回“具有多個元素的數組的真值不明確。使用 a.any() 或 a.all()”的錯誤. 我也嘗試過改變范圍的界限。 我找到了一篇提到使用底圖的文章,但我認為我不需要整體使用它。 我用於投影的基本代碼的鏈接在這里。 代碼片段在這里:
# Generate axes, using Cartopy, drawing coastlines, and adding features
fig=plt.figure(figsize=(10,10))
projection = ccrs.NorthPolarStereo()
ax = plt.axes(projection=projection)
ax.coastlines(linewidths=0.5)
ax.add_feature(cfeature.LAND, facecolor='lightgray')
ax.set_extent([-180,180,90,0],ccrs.PlateCarree())
gl = ax.gridlines(crs=ccrs.PlateCarree(),linestyle="--", linewidth=1, color='k', alpha=0.5)
theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_xticks([180,150,120,90,60,30])
ax.set_boundary(circle, transform=ax.transAxes)
p=wrap_U.plot.contour(ax=ax,vmin=-8,vmax=16, transform=ccrs.PlateCarree(),
levels = 7, linewidths=0.5, cmap='k')
ax.clabel(p, np.arange(-8,16,8),fmt='%d', inline=1, fontsize=14)
gvutil.set_titles_and_labels(ax,
lefttitle="Zonal Wind", righttitle="m/s")
#This line leads to truth value error
gvutil.set_axes_limits_and_ticks(ax, xticks=np.linspace(-180, 180), xticklabels=['180S', '90S', '0', '90N', '180N'],
ylim=ax.get_ylim()[::-1], yticks=U)
# Show the plot
plt.show()
我確實設置了一個自定義的 anaconda 環境,所以如果需要更多信息,我也可以鏈接到哪里可以找到環境。
看起來您正在嘗試設置一組不匹配的 xticks 和 xticklabels
gvutil.set_axes_limits_and_ticks(ax, xticks=np.linspace(-180, 180), xticklabels=['180S', '90S', '0', '90N', '180N'],
ylim=ax.get_ylim()[::-1], yticks=U)
您的代碼生成 50 個 xticks np.linspace(-180,180)
( numpy linspace默認值)
但只有 5 個標簽xticklabels=['180S', '90S', '0', '90N', '180N']
作為替代方案,我建議嘗試xticks=np.linspace(-180,180,5)
或xticks=np.linspace(-180,180,5)
刪除特定的 xticklabels
要根據您的規范繪制緯度的平行線和子午線的弧線,您必須在ax.gridlines()
指定xlocs
和ylocs
正確值。 對於它們的標簽,您可以使用ax.text()
。 嘗試運行下面的代碼並根據需要對其進行修改。
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.path as mpath
fig = plt.figure(figsize=[8,8])
projection = ccrs.NorthPolarStereo()
ax = plt.axes(projection=projection)
ax.coastlines(linewidths=0.5)
ax.add_feature(cfeature.LAND, facecolor='lightgray')
ax.set_extent([-180,180,90,0], ccrs.PlateCarree())
# specifying xlocs/ylocs yields number of meridian/parallel lines
dmeridian = 30 # spacing for lines of meridian
dparallel = 15 # spacing for lines of parallel
num_merid = 360/dmeridian + 1
num_parra = 90/dparallel + 1
gl = ax.gridlines(crs=ccrs.PlateCarree(), \
xlocs=np.linspace(0, 360, num_merid), \
ylocs=np.linspace(0, 90, num_parra), \
linestyle="--", linewidth=1, color='k', alpha=0.5)
theta = np.linspace(0, 2*np.pi, 120)
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
center, radius = [0.5, 0.5], 0.5
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes) #without this; get rect bound
# for label alignment
va = 'center' # also bottom, top
ha = 'center' # right, left
degree_symbol=u'\u00B0'
# for locations of (meridional/longitude) labels
lond = np.linspace(0, 360, num_merid)
latd = np.zeros(len(lond))
for (alon, alat) in zip(lond, latd):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
if alon>0 and alon<180:
ha = 'left'
va = 'center'
if alon>180 and alon<360:
ha = 'right'
va = 'center'
if np.abs(alon-180)<0.01:
ha = 'center'
va = 'bottom'
if alon==0.:
ha = 'center'
va = 'top'
if (alon<360.):
txt = ' {0} '.format(str(int(alon)))+degree_symbol
ax.text(projx1, projy1, txt, \
va=va, ha=ha, color='g')
# for locations of (meridional/longitude) labels
# select longitude: 315 for label positioning
lond2 = 315*np.ones(len(lond))
latd2 = np.linspace(0, 90, num_parra)
va, ha = 'center', 'center'
for (alon, alat) in zip(lond2, latd2):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
txt = ' {0} '.format(str(int(alat)))+degree_symbol
ax.text(projx1, projy1, \
txt, va=va, ha=ha, \
color='r')
# Show the plot
plt.show()
輸出圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.