混合matplotlib变换

时间:2012-09-13 23:35:13

标签: python matplotlib

我想在Matplotlib图中添加一个补丁(如Rectangle),其中补丁的位置使用ax.transData,但大小以像素或点为单位。有没有办法设置一个以这种方式工作的变换?或者除变换以外的其他一些方式?

1 个答案:

答案 0 :(得分:1)

这个答案来得太晚了,但是如果其他人偶然发现了这个问题:

Matplotlib documentation中有一个示例:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.transforms as transforms
fig, ax = plt.subplots()
xdata, ydata = (0.2, 0.7), (0.5, 0.5)
ax.plot(xdata, ydata, "o")
ax.set_xlim((0, 1))

trans = (fig.dpi_scale_trans +
         transforms.ScaledTranslation(xdata[0], ydata[0], ax.transData))

# plot an ellipse around the point that is 150 x 130 points in diameter...
circle = mpatches.Ellipse((0, 0), 150/72, 130/72, angle=40,
                          fill=None, transform=trans)
ax.add_patch(circle)
plt.show()

因此,基本上,您可以在转换中对补丁的位置进行编码,然后在(0,0)位置绘制补丁。