我已经编写了一个从大气模型输出中绘制垂直横截面的例程。下面是一个例子。我想做的是,显示两个垂直轴:在左边,我以对数刻度显示压力值,在右边,我以公里为单位显示高度。我想最好在模型层的位置显示高度-这就是为什么它们的间距不规则。所有的工作都很好,除了右边的标签在底部重叠。我发现我可以使用ax2.get_yticklabels()[index].set_visible(False)
隐藏特定的标签。我的问题是:如何确定要隐藏哪些标签(索引)?我认为应该可以找出记号标签的位置(在坐标轴或图形坐标中)。然后我可以用一个阈值距离
yp = -1
for t in ax2.get_yticklabels():
y = t.get_position().y0 # this doesn't yield any useful bbox!
if y-yp < threshold:
t.set_visible(False)
else:
yp = y
不幸的是,我还没有找到一种方法来获取标签坐标。有什么线索吗?
下面是示例图:
下面是完成绘图的完整代码(数据是二维数组,x是纬度,y是压力值):
def plotZM(data, x, y, plotOpt=None):
"""Create a zonal mean contour plot of one variable
plotOpt is a dictionary with plotting options:
'scale_factor': multiply values with this factor before plotting
'units': a units label for the colorbar
'levels': use list of values as contour intervals
'title': a title for the plot
"""
if plotOpt is None: plotOpt = {}
# create figure and axes
fig = plt.figure()
ax1 = fig.add_subplot(111)
# scale data if requested
scale_factor = plotOpt.get('scale_factor', 1.0)
pdata = data * scale_factor
# determine contour levels to be used; default: linear spacing, 20 levels
clevs = plotOpt.get('levels', np.linspace(data.min(), data.max(), 20))
# map contour values to colors
norm=matplotlib.colors.BoundaryNorm(clevs, ncolors=256, clip=False)
# draw the (filled) contours
contour = ax1.contourf(x, y, pdata, levels=clevs, norm=norm)
# add a title
title = plotOpt.get('title', 'Vertical cross section')
ax1.set_title(title) # optional keyword: fontsize="small"
# add colorbar
# Note: use of the ticks keyword forces colorbar to draw all labels
fmt = matplotlib.ticker.FormatStrFormatter("%g")
cbar = fig.colorbar(contour, ax=ax1, orientation='horizontal', shrink=0.8,
ticks=clevs, format=fmt)
cbar.set_label(plotOpt.get('units', ''))
for t in cbar.ax.get_xticklabels():
t.set_fontsize("x-small")
# change font size of x labels
xlabels = ax1.get_xticklabels()
for t in xlabels:
t.set_fontsize("x-small")
# set up y axes: log pressure labels on the left y axis, altitude labels
# according to model levels on the right y axis
ax1.set_ylabel("Pressure [hPa]")
ax1.set_yscale('log')
ax1.set_ylim(y.max(), y.min())
subs = [1,2,5]
print "y_max/y_min = ", y.max()/y.min()
if y.max()/y.min() < 30.:
subs = [1,2,3,4,5,6,7,8,9]
loc = matplotlib.ticker.LogLocator(base=10., subs=subs)
ax1.yaxis.set_major_locator(loc)
fmt = matplotlib.ticker.FormatStrFormatter("%g")
ax1.yaxis.set_major_formatter(fmt)
ylabels = ax1.get_yticklabels()
for t in ylabels:
t.set_fontsize("x-small")
# calculate altitudes from pressure values (use fixed scale height)
z0 = 8.400 # scale height for pressure_to_altitude conversion [km]
altitude = z0 * np.log(1015.23/y)
# add second y axis for altitude scale
ax2 = ax1.twinx()
ax2.set_ylabel("Altitude [km]")
ax2.set_ylim(altitude.min(), altitude.max())
ax2.set_yticks(altitude)
fmt = matplotlib.ticker.FormatStrFormatter("%6.1f")
ax2.yaxis.set_major_formatter(fmt)
# tweak altitude labels
ylabels = ax2.get_yticklabels()
for i,t in enumerate(ylabels):
t.set_fontsize("x-small")
# show plot
plt.show()
我宁愿不做这种事。这可能会产生问题,例如,调整绘图窗口大小或更改输出图像的dpi时。而且看起来肯定很尴尬。
你也应该
如果你需要帮助,尽管问。
以下是plotZM程序的更新版本,该程序将模型层绘制到右侧的单独面板中,并为高度轴使用线性等距标记。已添加另一个选项来遮罩低于表面压力的区域。
此代码是“缩放安全”(即,当您放大或平移绘图时,高度和压力标签会很好地更改,并且模型级别也会一致地更改)。它还包含许多轴和标签调整,因此作为一个更复杂的matplotlib示例,可能对其他人有用。示例图如下所示。
相关问题 更多 >
编程相关推荐