Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing matplotlib transforms

I would like to add a patch, such as Rectangle, to a Matplotlib plot where the position of the patch uses ax.transData but the size is in pixels or points. Is there a way to set up a transform that works that way? Or some other way other than transforms?

like image 814
RobinDunn Avatar asked Apr 18 '26 18:04

RobinDunn


1 Answers

This answer probably comes a little too late, but if someone else stumbles upon this question:

There is an example for this in the 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()

So basically you encode the position of the patch in the transformation and then draw the patch at position (0, 0).

like image 52
Daniel Sch. Avatar answered Apr 21 '26 07:04

Daniel Sch.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!