python - Matplotlib - 用三角形框注释图
问题描述
我想用一个尖尖的三角形框来注释我的情节的左上角,里面有一个字母,如下图所示。我只能做一个方形盒子,但我想要一个三角形盒子。
plt.annotate("A",
xy = (0.05, 0.92),
xycoords = "axes fraction",
bbox = dict(boxstyle = "square", fc = "red"))
解决方案
从这里,您可以绘制任何您想要的任意形状。
这不完全是你想要的,但可能会让你通过。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
def label(xy, text):
y = xy[1] - 0.15 # shift y-value for label so that it's below the artist
plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)
fig, ax = plt.subplots()
patches = []
# add a path patch
Path = mpath.Path
path_data = [
(Path.MOVETO, [0.00, 0.00]),
(Path.LINETO, [0.0, 1.0]),
(Path.LINETO, [1.0, 1.0]),
(Path.CLOSEPOLY, [0.0, 1.0])
]
codes, verts = zip(*path_data)
path = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path)
patches.append(patch)
colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, alpha=0.3)
collection.set_array(np.array(colors))
ax.add_collection(collection)
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
plt.axis('equal')
plt.axis('off')
plt.show()
推荐阅读
- spring - Spring 上下文启动期间的就绪探测
- laravel - laravel 雄辩的关系使用“with”,“whereHas”和“whereIn”子句
- elasticsearch - 弹性搜索错误 56
- node.js - 使用 .bat 在 Windows 上调度 node.js 命令
- sql - 在 select 语句的第一行中使用 '*' 和 '.*' 有什么区别?
- python-3.x - 在 windows 上的 python 3 上安装斜纹布时出现问题。
- java - 休眠搜索:Elasticsearch 传输线程 3" java.lang.OutOfMemoryError:
- html - 复选框右属性在从左到右更改时不起作用
- node.js - nodejs:自定义函数的 setInterval
- c# - 试图从 WPF 窗口打开一个新窗口,我得到 STAThread 异常