Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale bar does not correspond to the actual size #14

Closed
alexdesiqueira opened this issue May 21, 2018 · 5 comments
Closed

Scale bar does not correspond to the actual size #14

alexdesiqueira opened this issue May 21, 2018 · 5 comments

Comments

@alexdesiqueira
Copy link

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.

@parishcm
Copy link

parishcm commented Jun 9, 2018

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)

ppinard added a commit that referenced this issue Jul 8, 2018
The bar was drawn with an edge around it which made it longer than the
actual size.
@ppinard
Copy link
Owner

ppinard commented Jul 8, 2018

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.

@firasm
Copy link

firasm commented Jul 14, 2018

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?)

@firasm
Copy link

firasm commented Jul 14, 2018

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

@ppinard
Copy link
Owner

ppinard commented Jul 15, 2018

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

@ppinard ppinard closed this as completed Jul 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants