python-3.x - 如何将 mpl_toolkits 的“AnchoredSizeBar”定位在其给定轴之外
问题描述
在将 AnchoredSizeBar 放置在其给定轴之外时,我面临着严重的困难。从 AnchoredSizeBar 参考中,loc 属性仅接受与用于创建 AnchoredSizeBar 的给定轴相关的“字符串”方法。
因此,如果我想将 AnchoredSizeBar 位置设置在给定轴之外,则 loc 属性将不起作用。事实上,它会引发错误消息。
有人知道绕过这个问题的方法吗?
如果可能的话,我想创建一个 AnchoredSizeBar,它的条形大小仍然相对于图中的给定轴,但是 AnchoredSizeBar 的位置可以放置在图实例内的任何位置。
这是我想要的代码片段:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
fig, ax = plt.subplots(figsize=(3, 3))
x_position = 0.15
y_position = 0.35
Figure_location = (x_position, y_position) # figure xy locations relative to fig.transFigure.
axes_width_to_size_bar = 0.3
bar0 = AnchoredSizeBar(ax.transData, axes_width_to_size_bar, 'unfilled', loc=Figure_location, frameon=False, size_vertical=0.05, fill_bar=False)
ax.add_artist(bar0)
bar0_extent = bar0.get_extent()
fig.show()
我感谢你的时间。您忠诚的,
菲利普·里斯卡拉·里尔
解决方案
AnchoredSizeBar
子类matplotlib.offsetbox.AnchoredOffsetbox
。额外的参数因此被传递给AnchoredOffsetbox
. 这提供了参数bbox_to_anchor
和bbox_transform
. 这些与图例相同,因此有关解释,请参见任何图例示例,例如How to put the legend out of the plot。
比如把AnchoredSizeBar放在图的右上角,
import matplotlib.pyplot as plt
def draw_sizebar(ax):
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
from matplotlib.transforms import Bbox
asb = AnchoredSizeBar(ax.transData,
0.1,
"5 warp units",
loc='upper right',
pad=0.1, borderpad=0.5, sep=5,
frameon=False,
bbox_to_anchor=Bbox.from_bounds(0, 0, 1, 1),
bbox_transform=ax.figure.transFigure)
ax.add_artist(asb)
fig, ax = plt.subplots()
draw_sizebar(ax)
plt.show()
推荐阅读
- sql - SQL (bigquery) - SUM 在时间间隔内以开始/停止 DATETIME 继续值
- asp.net-core - EF Core AutoMapper:未应用包含中的过滤器嵌套集合
- templates - C ++提取元组元组中每个元组的第k个元素
- php - xampp phpmyadmin 中缺少 mysqli 扩展
- python-3.x - python的except块可以同时有2个条件吗?
- javascript - 如何从对象数组中渲染所有未知属性,除了一些反应
- javascript - 网站支持页面中的 HTML 中的 JavaScript 代码
- bash - 在 bash 脚本中的函数内部读取 -p
- visual-studio - 调试器未正确安装 SSIS 2019
- c++ - 组成情况下的班级规模