-1

any idea how to insert a scale bar inside a plot? With the same unit of the Y axes. Something like in the image? [example of the scale bar]

1

What I would like in the end, is to remove the scale from the Y axes and just insert a scale bar as reference for my graph.

eshirvana
  • 23,227
  • 3
  • 22
  • 38
Sartorix
  • 1
  • 1
  • 2
    Can you provide your code as well? It helps to recreating your diagram. – Blaco Jan 28 '22 at 14:35
  • What are you using? `matplotlib`? If yes check out these questions [1](https://stackoverflow.com/questions/39786714/how-to-insert-scale-bar-in-a-map-in-matplotlib) [2](https://stackoverflow.com/questions/43258638/is-there-a-convenient-way-to-add-a-scale-indicator-to-a-plot-in-matplotlib) – Sparky05 Jan 28 '22 at 14:35

1 Answers1

0

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

Sartorix
  • 1
  • 1