How to position a matplotlib patch outside of the axes range (so that it could be next to the title, or legend, or anywhere on the figure)

强颜欢笑 提交于 2021-01-28 09:00:53

问题


Given the following code:

fig, ax = plt.subplots()
ax.scatter([1, 2, 3, 4, 5], [34, 22, 11, 4, 6], s=100)
_ = ax.text(x=0, y=1.1, s="This is some text", transform=ax.transAxes, fontsize=20)

rect = mpl.patches.Rectangle(
    (0.5, 0.5), width=0.05, height=0.05, color="red", transform=ax.transAxes,
)
ax.add_patch(rect)

Which creates:

I would like to add the patch to the following location:

So that the plot looks as follows:

It seems that I am unable to have patches outside of the axes spines area though, for example the following code:

fig, ax = plt.subplots()
ax.scatter([1, 2, 3, 4, 5], [34, 22, 11, 4, 6], s=100)
_ = ax.text(x=0, y=1.1, s="This is some text", transform=ax.transAxes, fontsize=20)

rect = mpl.patches.Rectangle(
    (0.5, 0.5), width=0.05, height=0.5, color="red", transform=ax.transAxes,
)
ax.add_patch(rect)

gives

Looking in the method for matplotlib.figure.Figure I can't see anything for add_patch

['_clippath',
 '_path_effects',
 'get_clip_path',
 'get_path_effects',
 'get_transformed_clip_path_and_affine',
 'patch',
 'patches',
 'set_clip_path',
 'set_path_effects']

The above are the methods with pat in them.


回答1:


You can disable clipping by setting clip_on=False, and then you can position the patch where you want. For example:

import matplotlib as mpl
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.scatter([1, 2, 3, 4, 5], [34, 22, 11, 4, 6], s=100)
_ = ax.text(x=0, y=1.1, s="This is some text", transform=ax.transAxes, fontsize=20)

rect = mpl.patches.Rectangle(
    (0.5, 1.1), width=0.05, height=0.05, color="red", transform=ax.transAxes,
    clip_on=False
)

ax.add_patch(rect)
plt.show()

Gives:




回答2:


You can use chr(9607) to represent the patch:

print("This is some text", chr(9607)*3)

Output:

This is some text ▇▇▇

Into your code:

fig, ax = plt.subplots()
ax.scatter([1, 2, 3, 4, 5], [34, 22, 11, 4, 6], s=100)
_ = ax.text(x=0, y=1.1, s=f"This is some text {chr(9607)*3}", transform=ax.transAxes, fontsize=20)

rect = mpl.patches.Rectangle(
    (0.5, 0.5), width=0.05, height=0.05, color="red", transform=ax.transAxes,
)
ax.add_patch(rect)


来源:https://stackoverflow.com/questions/62747295/how-to-position-a-matplotlib-patch-outside-of-the-axes-range-so-that-it-could-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!