如何更改“ 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的“位置”的三种可能方法:
如果要在数据坐标中设置偏移量,则需要告诉它通过以下方式在数据坐标中应用此偏移量 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] 删除。
我来说两句