如何更改PolyCollection的位置?

马修·梅

如何更改“ matplotlib.collections.PolyCollection”的位置?

我已经创建了PolyCollection,然后我想更改位置。

%matplotlib notebook
import matplotlib.pyplot as plt

p=plt.figure()

area=plt.gca().fill_between((0,4), 1, 2, facecolor='red', alpha=0.25)
type(area)

在此处输入图片说明

该文档位于此处:https : //matplotlib.org/api/collections_api.html#matplotlib.collections.PolyCollection

我认为set_offsets()是更改位置的功能。但是我找不到例子。

我努力了

area.set_offsets([[1.1, 2.1]])

没有效果。

认真的重要性

由fill_between创建的PolyCollection不使用任何偏移量。它只是数据坐标中的路径集合。更新通过fill_between以下方式创建的PolyCollection的“位置”的三种可能方法

设置offset_position

如果要在数据坐标中设置偏移量,则需要告诉它通过以下方式在数据坐标中应用此偏移量 area.set_offset_position("data")

import numpy as np
import matplotlib.pyplot as plt

p=plt.figure()

area=plt.gca().fill_between((0,4), 1, 2, facecolor='red', alpha=0.25)

area.set_offsets(np.array([[1.1, 2.1]]))

area.set_offset_position("data")

plt.show()

改变路径

您可以根据需要更改路径:

import numpy as np
import matplotlib.pyplot as plt

p=plt.figure()

area=plt.gca().fill_between((0,4), 1, 2, facecolor='red', alpha=0.25)

offset = np.array([1.1, 2.1])

area.get_paths()[0].vertices += offset

plt.gca().dataLim.update_from_data_xy(area.get_paths()[0].vertices)

plt.gca().autoscale()
plt.show()

改变变换

import numpy as np
import matplotlib.transforms
import matplotlib.pyplot as plt

p=plt.figure()

area=plt.gca().fill_between((0,4), 1, 2, facecolor='red', alpha=0.25)

offset = np.array([1.1, 2.1])

transoffset = matplotlib.transforms.Affine2D().translate(*offset)
area.set_transform(transoffset + area.get_transform())

plt.show()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章