1

I am making figures for a paper, and I want to put a label some fixed distance from the top-left corner of the plot. ax.text(x, y, label, transform=ax.transAxes) nearly does this, but specifies the position as a fraction of the size of the plot. If I could get the absolute size of the plot area I could use that to convert. For example, in the following script how could I get the height and width?

Edit: I want the height and width just of the plot (not of the whole figure), and excluding the labels, ticks, etc.

from matplotlib import pyplot as plt
import numpy as np

data = np.random.rand(10,10)

fig, ax = plt.subplots()

ax.pcolormesh(data)

ax.set_aspect("equal")

# get width and height of plot here?

plt.show()
2
  • fig.get_size_inches()? Commented Oct 14, 2020 at 13:53
  • Sorry, I need to make the question clearer. I meant the size of just the plot area of the Axes (not the whole figure) and excluding labels, ticks, etc. Commented Oct 14, 2020 at 13:55

2 Answers 2

2

Your response above is good, but perhaps better:

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()

trans = (fig.dpi_scale_trans +
         mtransforms.ScaledTranslation(0, 1, ax.transAxes))

ax.set_aspect(1)
ax.text(0.1, -0.2, 'Boo', transform=trans)
plt.show()

You can read more at: https://matplotlib.org/tutorials/advanced/transforms_tutorial.html#plotting-in-physical-coordinates

Sign up to request clarification or add additional context in comments.

Comments

1

I did manage to find a way to do this. It was made more complicated because I used set_aspect() - set_aspect() modifies the bounding box of ax, but by default doesn't apply the modification until ax is drawn (e.g. by plt.show()), which messes up attempts to get the bounding box size.

The solution is:

  1. To avoid problems from set_aspect(), call apply_aspect() before trying to get the bounding box size. This makes the updated aspect ratio actually modify the bounding box size so we can find out what it is.

  2. Get the size of the plot area with ax.get_window_extent() - this gets the size of just the plot area, excluding axis labels, ticks, etc.

  3. The result of ax.get_window_extent() is in 'display units', which we can convert to inches using fig.dpi.

So adding the kind of label I wanted as an example:

from matplotlib import pyplot as plt
import numpy as np

data = np.random.rand(10,10)

fig, ax = plt.subplots()

ax.pcolormesh(data)

ax.set_aspect("equal")
ax.apply_aspect()

bbox = ax.get_window_extent()

# dpi used to convert from display units to inches
dpi = fig.dpi

height = bbox.height / dpi  # in inches
width = bbox.width / dpi  # in inches

x = 0.2 # in inches
y = 0.1 # in inches

ax.text(x / width, 1.0 - y / height, "(a)", verticalalignment="top", color="w", transform=ax.transAxes)

plt.show()

which gives:

enter image description here

Edit: I tested this solution with matplotlib-3.3.2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.