3

How can I draw shapes in matplotlib using point/inch dimensions?

I've gone through the patch/transform documentation so I understand how to work in pixel, data, axes or figure coordinates but I cannot figure out how to dimension a rectangle in points/inches.

Ideally I would like to position a rectangle in data coordinates but set its size in points, much like how line markers work.

Here is an example of the plot I am trying to create. I currently position the black and red boxes in (data, axes) coordinates. This works when the graph is a known size, but fails when it gets rescaled as the boxes become smaller even through the text size is constant.

enter image description here

enter image description here

user65
  • 1,223
  • 1
  • 9
  • 12
  • Have you seen [this](http://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib). You could do the transform at the end. – Shawn Mehan Nov 10 '15 at 17:03
  • I've attached some pictures showing the problem. I suppose I could read the figure size and calculate what portion of the axes would represent a certain number of inches but that seems very fragile. – user65 Nov 10 '15 at 17:51

1 Answers1

3

Ended up figuring it out with help from this question: How do I offset lines in matplotlib by X points

There is no built in way to specify patch dimensions in points, so you have to manually calculate a ratio of axes or data coordinates to inches/points. This ratio will of course vary depending on figure/axes size.

This is accomplished by running a (1, 1) point through the axes transform and seeing where it ends up in pixel coordinates. Pixels can then be converted to inches or points via the figure dpi.

t = axes.transAxes.transform([(0,0), (1,1)])
t = axes.get_figure().get_dpi() / (t[1,1] - t[0,1]) / 72

# Height = 18 points
height = 18 * t

enter image description here enter image description here

Community
  • 1
  • 1
user65
  • 1,223
  • 1
  • 9
  • 12