Joey Dumont
Joey Dumont

Reputation: 938

Specify the yaxis length separately from the drawing limits in matplotlib

Is it possible to specify the yaxis line length independently of the drawing distance in matplotlib?

I currently use something like that to generate the figure

# -- We plot the data
figSpectrum   = plt.figure(figsize=(4,2))
axSpectrum    = figSpectrum.add_subplot(111)
plt.subplots_adjust(hspace=0.5)

input_spec,   = plt.plot(x, yTheo, color='#ee923b', lw=2, label='Input spectrum')
samples_spec, = plt.plot(x, ySamples, 'o', ms=1, color='k', label='Samples')

axSpectrum.set_ylim((0.0,1.0))

where yTheo and ySamples are in the set [0,1]. When I use axSpectrum.set_ylim((0.0,1.0)), the lineplot gets clipped like in the figure below (clipped area is shown in the red rectangle). The top portion of the lineplot gets clipped.

However, if I use axSpectrum.set_ylim((0,1.1)), then the y-axis is goes beyond the 1.0 tick, which I find annoying (see below). Is there a way to tell matplotlib to draw the lineplots from y=0 to y=1.1 while drawing the yaxis from y=0 to y=1?

The yaxis extends too far above the last tick at y=1.0

Upvotes: 1

Views: 40

Answers (1)

tmdavison
tmdavison

Reputation: 69213

Set clip_on=False as an option to plt.plot. This will allow the lines drawn by plot to go outside the axes area.

From the docs:

set_clip_on(b)

Set whether artist uses clipping.

When False artists will be visible out side of the axes which can lead to unexpected results

for your example:

input_spec,   = plt.plot(x, yTheo, color='#ee923b', lw=2, label='Input spectrum', clip_on=False)
samples_spec, = plt.plot(x, ySamples, 'o', ms=1, color='k', label='Samples', clip_on=False)

Upvotes: 2

Related Questions