- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我编写了一个例程来根据大气模型输出绘制垂直横截面。一个例子如下所示。我想做的是显示两个垂直轴:在左侧,我以对数刻度显示压力值,在右侧,我以公里为单位显示高度。我认为在模型级别的位置显示高度会很好 - 这就是它们不规则间隔的原因。一切都很好,除了右边的标签在底部附近重叠。我发现我可以使用 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()
最佳答案
这是 plotZM 例程的更新版本,它将模型级别绘制到右侧的单独面板中,并使用线性等距标记作为高度轴。添加了另一个选项来屏蔽低于表面压力的区域。
此代码是“缩放安全”的(即,当您放大或平移绘图时,高度和压力标签会发生很好的变化,并且模型级别会持续变化)。它还包含相当多的轴和标签调整,因此可能希望对其他人有用,作为您可以使用 matplotlib 做什么的更复杂的示例。示例图如下所示。
def plotZM(data, x, y, plotOpt=None, modelLevels=None, surfacePressure=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
modelLevels: a list of pressure values indicating the model vertical resolution. If present,
a small side panel will be drawn with lines for each model level
surfacePressure: a list (dimension len(x)) of surface pressure values. If present, these will
be used to mask out regions below the surface
"""
# explanation of axes:
# ax1: primary coordinate system latitude vs. pressure (left ticks on y axis)
# ax2: twinned axes for altitude coordinates on right y axis
# axm: small side panel with shared y axis from ax2 for display of model levels
# right y ticks and y label will be drawn on axr if modelLevels are given, else on ax2
# axr: pointer to "right axis", either ax2 or axm
if plotOpt is None: plotOpt = {}
labelFontSize = "small"
# 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)
# mask out surface pressure if given
if not surfacePressure is None:
ax1.fill_between(x, surfacePressure, surfacePressure.max(), color="white")
# add a title
title = plotOpt.get('title', 'Vertical cross section')
ax1.set_title(title)
# 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(labelFontSize)
# 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(10.*np.ceil(y.max()/10.), y.min()) # avoid truncation of 1000 hPa
subs = [1,2,5]
if y.max()/y.min() < 30.:
subs = [1,2,3,4,5,6,7,8,9]
y1loc = matplotlib.ticker.LogLocator(base=10., subs=subs)
ax1.yaxis.set_major_locator(y1loc)
fmt = matplotlib.ticker.FormatStrFormatter("%g")
ax1.yaxis.set_major_formatter(fmt)
for t in ax1.get_yticklabels():
t.set_fontsize(labelFontSize)
# 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()
# change values and font size of x labels
ax1.set_xlabel('Latitude [degrees]')
xloc = matplotlib.ticker.FixedLocator(np.arange(-90.,91.,30.))
ax1.xaxis.set_major_locator(xloc)
for t in ax1.get_xticklabels():
t.set_fontsize(labelFontSize)
# draw horizontal lines to the right to indicate model levels
if not modelLevels is None:
pos = ax1.get_position()
axm = fig.add_axes([pos.x1,pos.y0,0.02,pos.height], sharey=ax2)
axm.set_xlim(0., 1.)
axm.xaxis.set_visible(False)
modelLev = axm.hlines(altitude, 0., 1., color='0.5')
axr = axm # specify y axis for right tick marks and labels
# turn off tick labels of ax2
for t in ax2.get_yticklabels():
t.set_visible(False)
label_xcoor = 3.7
else:
axr = ax2
label_xcoor = 1.05
axr.set_ylabel("Altitude [km]")
axr.yaxis.set_label_coords(label_xcoor, 0.5)
axr.set_ylim(altitude.min(), altitude.max())
yrloc = matplotlib.ticker.MaxNLocator(steps=[1,2,5,10])
axr.yaxis.set_major_locator(yrloc)
axr.yaxis.tick_right()
for t in axr.yaxis.get_majorticklines():
t.set_visible(False)
for t in axr.get_yticklabels():
t.set_fontsize(labelFontSize)
# show plot
plt.show()
关于python - Matplotlib 轴标签 : how to find out where they will be located?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14153816/
我正在尝试在 python 中编写正则表达式来查找目录路径:我的文本如下所示: text = "The public disk is: \\\\diskA\\FolderB\\SubFolderC\\
我想写一个LocationListener,它把最近最精确的位置作为它的位置。我打算在我的 LocationListener 中使用此代码: @Override public void
我想建立一个有光泽和 plotly 的交互式图表。 Shiny 有一个内置功能来获取有关用户交互的信息。比如:input$plot_click、input$plot_dblclick、input$pl
我正在使用 MobileFirst 提供的 WL.Device.Geo.acquirePosition(onGeoLocationSuccess, onGeoLocationFailure, opti
我最近开始使用 ionic 框架,它里面有 angular js。为了在屏幕之间导航,我使用了 $location.path 并且效果很好。但是,在我下载的一个示例中,我看到 $state.go 被用
谁能解释一下这种行为?我使用 history.pushState(null, null, '#test'); 推送历史状态。然后,当尝试使用console.log(window.location.ha
这里是相关代码: https://www.facebook.com/sharer/sharer.php?u={{$location.absUrl()}} https://www.facebook.c
这两个重定向之间有什么区别?我有一个应用程序,当我使用时,它可以在 chrome 和 android 4 上正常工作,但在 android 2.x.x 上不能正常工作 document.locatio
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
有什么区别 window.location.href="http://example.com"; window.location.replace("http://example.com"); wind
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
以下 3 个指令之间有区别吗? location ~* \.(png)$ { expires max; log_not_found off; } location ~ \.(png)$ {
位于正文末尾之前的以下脚本在 Internet Explorer 和 Chrome(以及任何其他浏览器)中都会被调用。但重定向到指定的 URL 仅发生在 IE 中。我还尝试了 window.locat
我正在使用 Angular ngRouter。我需要更改 url 路径以及搜索参数。我知道 $location.path 和 $location.search,但是有没有一个函数可以同时设置它们? 最
在angularjs中用$location和window.location哪个更好。 例如,我们可以使用$location.path() 或window.location.href 来完成同样的工作。
我在我的网站上使用上述 2 个命令。似乎它们对 95% 访问它应该触发的页面的人有效,但对其他人则不会。 有谁知道是否可以完全阻止这些 javascript 命令?我真的很头疼为什么它们有时不起作用。
这是我无法弄清楚的另一个错误。 我有这个类ExtendedLocation extends Location实例化时抛出 ClassCastExceptioncurrentGpsLocation =
我一直在尝试简单地将一个包含两个变量(一个字符串和一个位置)的类推送和读取到 firebase,但我一直收到此错误。 **com.google.firebase.database.DatabaseEx
我注意到 iPhone 上的“常用位置”似乎比监控 iOS 访问的应用程序 (https://developer.apple.com/reference/corelocation/clvisit) 使
在我的 javascript 代码中,在某些时候,我需要刷新窗口(用户上传了新图片但在页面中仍然可以看到它) 我想知道为什么 location.href = location.href 不刷新窗口?
我是一名优秀的程序员,十分优秀!