我希望在matplotlib绘图中绘制注释图像,并能够在绘图后移动它们
为此,我从演示开始: https://matplotlib.org/3.2.1/gallery/text_labels_and_annotations/demo_annotation_box.html
使用笔记本电脑后端:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage,
AnnotationBbox)
from matplotlib.cbook import get_sample_data
fig, ax = plt.subplots()
# Define a 1st position to annotate (display it with a marker)
xy = (0.5, 0.7)
ax.plot(xy[0], xy[1], ".r")
# Annotate the 1st position with a text box ('Test 1')
offsetbox = TextArea("Test 1", minimumdescent=False)
ab = AnnotationBbox(offsetbox, xy,
xybox=(-20, 40),
xycoords='data',
boxcoords="offset points",
arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
# Annotate the 1st position with another text box ('Test')
offsetbox = TextArea("Test", minimumdescent=False)
ab = AnnotationBbox(offsetbox, xy,
xybox=(1.02, xy[1]),
xycoords='data',
boxcoords=("axes fraction", "data"),
box_alignment=(0., 0.5),
arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
# Define a 2nd position to annotate (don't display with a marker this time)
xy = [0.3, 0.55]
# Annotate the 2nd position with a circle patch
da = DrawingArea(20, 20, 0, 0)
p = Circle((10, 10), 10)
da.add_artist(p)
ab = AnnotationBbox(da, xy,
xybox=(1.02, xy[1]),
xycoords='data',
boxcoords=("axes fraction", "data"),
box_alignment=(0., 0.5),
arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
# Annotate the 2nd position with an image (a generated array of pixels)
arr = np.arange(100).reshape((10, 10))
im = OffsetImage(arr, zoom=2)
im.image.axes = ax
ab = AnnotationBbox(im, xy,
xybox=(-50., 50.),
xycoords='data',
boxcoords="offset points",
pad=0.3,
arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
# Annotate the 2nd position with another image (a Grace Hopper portrait)
with get_sample_data("grace_hopper.png") as file:
arr_img = plt.imread(file, format='png')
imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy,
xybox=(120., -80.),
xycoords='data',
boxcoords="offset points",
pad=0.5,
arrowprops=dict(
arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=90,rad=3")
)
ax.add_artist(ab)
# Fix the display limits to see everything
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()
为了测试如何移动东西,我在下一个单元格中执行:
rnd = fig.canvas.renderer
print(rnd)
print(im.get_window_extent(rnd))
print(im.get_window_extent(rnd).get_points())
print(ab.xybox)
ab.xybox = (130, -70)
im._offset = (135.46666666666667, 301.6355555555555)
im.set_zoom(.5)
ab.update_positions(rnd)
print(ab.xybox)
print(im.get_window_extent(rnd).get_points())
ab.draw(rnd)
im.draw(rnd)
fig.draw(rnd)
ax.plot([0, 1], [0, 1])
plt.show()
fig.canvas.draw_idle()
我看到的是,我确实在绘制的线中得到了一个中断,显示annotationbbox被移动了,但它的所有内容都没有移动。(此处先绘制直线(蓝色),然后执行所有移位,再绘制另一条直线(橙色)) 任何关于我如何实际移动Grace Hopper形象的建议都将受到高度赞赏
这不是对您的问题的直接回答,但我想知道一个类似的问题(而且matplotlib文档也缺乏),所以我对这个示例进行了修改,它似乎是可行的。关键似乎是在每次更改后使用fig.canvas.draw()。此外,我正在使用matplotlib版本3.1.3
相关问题 更多 >
编程相关推荐