防止注释显示在图形区域之外 [英] Prevent that an annotation is shown outside the graph area

查看:25
本文介绍了防止注释显示在图形区域之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建带有注释的散点图:

I create a scatter plot with an annotation with the following code:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(12)
plt.scatter(np.random.random(10), np.random.random(10), zorder = 2)
bbox_props = dict(boxstyle = "circle, pad = 10", fc = "w", ec = "k")
plt.annotate("interesting", (0.6, 0.8), bbox = bbox_props, zorder = 1)
plt.show()

示例输出:

如何防止注释显示在图形外部?我搜索了它,但只能找到人们想要在图表外打印注释/文本/图例的相反情况.

How do you prevent that the annotation is shown outside the graph? I searched for it but could only find the opposite case that people want to print an annotation/text/legend outside the graph.

推荐答案

您需要使用 plt.annotate clip_on 参数:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(12)
plt.scatter(np.random.random(10), np.random.random(10), zorder = 2)
bbox_props = dict(boxstyle = "circle, pad = 10", fc = "w", ec = "k")
plt.annotate("interesting", (0.6, 0.8), bbox = bbox_props, zorder = 1, clip_on=True)
plt.show()

这篇关于防止注释显示在图形区域之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆