ppinard/matplotlib-scalebar

Scale bar does not correspond to the actual size

alexdesiqueira opened this issue ยท 5 comments

Dear all,
first of all, thank you very much for this, scalebars are kinda mandatory in research and having this in python is really nice.
I am having some issues when using ScaleBar. The bar does not correspond to the actual size it would have; could you help me with that?
I'm using this code:

from matplotlib_scalebar.scalebar import ScaleBar
from skimage.io import imread

len_px = 0.00048223304582578836

image = imread('dur_grain1apatite01.tif', as_grey=True)

fig, ax = plt.subplots(figsize=(15, 10))
ax.imshow(testimg_apatite)
scalebar = ScaleBar(dx=len_px,
                    units='mm',
                    fixed_value=25,
                    fixed_units='um',
                    location=4,
                    box_alpha=0,
                    font_properties={'family' : 'monospace',
                                     'weight' : 'semibold',
                                     'size' : 20})
fig.gca().add_artist(scalebar)
plt.show()

This is the example image.
Thank you very much for your support.

I've noticed the same behavior, using ImageJ to measure the scalebar and "set scale", followed by measuring the full width of the image with ctrl-M in ImageJ. The code below makes a 100 pix image with 0.1 micron/pixel, for 10 microns in extent. If we use the default length_fraction the scale bar is 1 micron and the image is about 9.3 microns measured. With length_fraction = 0.7, the scale bar is 5 microns and the extent (measured in ImageJ) is perfect.

Anyway -- thanks for this! It's awesome. I'm writing a script to dump CrossCourt4 data as a batch export and this will help a lot.

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

A = 2 * np.random.rand(110, 100) - 1
A[100:110, :] = np.nan

f, c = plt.subplots()
cax = plt.imshow(A, cmap='bwr')
f.colorbar(cax)
cax.axes.get_xaxis().set_visible(False)
cax.axes.get_yaxis().set_visible(False)
plt.axis('off')

dx = 0.1

scale = ScaleBar(dx, 'um', location='lower left', length_fraction = 0.7)
plt.gca().add_artist(scale)

Thank you both for your comment. You are 100% right there was a mistake in the code. After many attempts to understand the problem I realized that the "bar" of the scale bar was drawn with an edge around it ๐Ÿ˜ž This made the "bar" appeared longer. Making the bar longer (with the length_fraction) helped because the error due to the edge decreases as the length of the bar increases.

I tried @parishcm's example and I got a length of 10.0769 in ImageJ. I committed a fix and I will release a new version soon.

I just noticed this issue also because I tried to make the bar thicker but the size of the bar changed so I got suspicious. Any update on a fix, would be great to get an ETA so I can wait it out or try to apply the commit myself (could you ref the commit here?)

Oops! Sorry just noticed it's commit 7f3683a and v5. Will update and check back

I will close this issue not to cause confusion. Please reopen if you think the problem is still there.