Thanks guys! Yes I'm using matplotlib. I re-adapted the code you linked from scale indicator and I have obtained what I wanted.
Here the code (as example):
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.offsetbox
from matplotlib.lines import Line2D
import numpy as np
class AnchoredHScaleBar(matplotlib.offsetbox.AnchoredOffsetbox):
""" size: length of bar in data units
extent : height of bar ends in axes units """
def __init__(self, size=1, extent = 0.03, label="", loc=2, ax=None,
pad=0.4, borderpad=0.5, ppad = 0, sep=2, prop=None,
frameon=True, linekw={}, **kwargs):
if not ax:
ax = plt.gca()
trans = ax.get_yaxis_transform()
size_bar = matplotlib.offsetbox.AuxTransformBox(trans)
line = Line2D([0,0],[size,0], **linekw)
hline1 = Line2D([-extent/2.,extent/2.],[0,0], **linekw)
hline2 = Line2D([-extent/2.,extent/2.],[size,size], **linekw)
size_bar.add_artist(line)
size_bar.add_artist(hline1)
size_bar.add_artist(hline2)
txt = matplotlib.offsetbox.TextArea(label, minimumdescent=False)
self.vpac = matplotlib.offsetbox.VPacker(children=[size_bar,txt],
align="center", pad=ppad, sep=sep)
matplotlib.offsetbox.AnchoredOffsetbox.__init__(self, loc, pad=pad,
borderpad=borderpad, child=self.vpac, prop=prop, frameon=frameon,
**kwargs)
x = np.linspace (0 , 10 , 10)
y = np.linspace (10 ,20 , 10)
ob = AnchoredHScaleBar(size=2, label="2 units", loc=2, frameon=False,
pad=1,sep=4, linekw=dict(color="black"),)
fig = plt.figure()
ax = fig.gca()
ax.plot(x, y)
ax.add_artist(ob)
result