0

I'm trying to get the position of a scalebar in a matplotlib plot and I'm getting wrong results from get_window_extent(), I get zeroes.

I'm using the basic scale bar example from here and adding some print statements:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from matplotlib_scalebar.scalebar import ScaleBar

# Load image
with cbook.get_sample_data("s1045.ima.gz") as dfile:
    im = np.frombuffer(dfile.read(), np.uint16).reshape((256, 256))

# Create subplot
fig, ax = plt.subplots()
ax.axis("off")

# Plot image
ax.imshow(im, cmap="gray")

# Create scale bar
scalebar = ScaleBar(0.08, "cm", length_fraction=0.25)
ax.add_artist(scalebar)

print(scalebar.get_window_extent())

fig.canvas.draw()

print(scalebar.get_window_extent())

# Show
plt.show()

print(scalebar.get_window_extent())

All I get is:

Bbox(x0=0.0, y0=0.0, x1=0.0, y1=0.0)

This is not correct, I should be getting values different from zero

mr_vodoo
  • 23
  • 1
  • 6

0 Answers0