matplotlib-scalebar

raw JSON →
0.9.0 verified Mon Apr 27 auth: no python

Artist for matplotlib to display a scale bar. Current version: 0.9.0. Release cadence: infrequent, couple of years.

pip install matplotlib-scalebar
error ImportError: cannot import name 'ScaleBar' from 'scalebar'
cause Wrong import path. The package name is matplotlib_scalebar, not scalebar.
fix
Use: from matplotlib_scalebar.scalebar import ScaleBar
error AttributeError: 'ScaleBar' object has no attribute 'set_font_properties'
cause You may be trying to set font properties on ScaleBar, which does not expose that directly. The text properties are internal.
fix
Modify the text via scalebar.text_props dict or by recreating the artist.
error TypeError: __init__() got an unexpected keyword argument 'location'
cause The location parameter was renamed to 'loc' in version 0.8.0.
fix
Use loc='lower right' instead of location='lower right'.
breaking ScaleBar no longer automatically rescales when axis limits change (fixed scale factor). Prior to 0.6.0 it auto-rescaled; now you must call scalebar.update() if you change limits after adding.
fix Use scalebar.update() or recreate the ScaleBar after setting axis limits.
gotcha The unit string is not validated; if you pass an unknown unit like 'myunit', it will be displayed as-is. No error is raised.
fix Use known units: 'm', 'cm', 'mm', 'um', 'nm', 'ft', 'in', etc.
gotcha ScaleBar uses the axes' x-axis data coordinates. If your data is not in physical units, the scale bar will be meaningless. Ensure you provide the correct calibration factor (dx) matching your data coordinates.
fix Set dx to the length represented by one data unit in your desired physical unit.

Basic usage: create a ScaleBar with calibration factor (dx) and unit, then add to axes.

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

# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlabel('Distance (μm)')

# Add a scale bar: dx=1 means one unit in data coordinates = 1 μm
scalebar = ScaleBar(1, 'um', length_fraction=0.25)
ax.add_artist(scalebar)
plt.show()