Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you limit the y-axis height in matplotlib?

How do you limit the height of the y-axis in matplotlib figure? I am trying to both display the x axis, and reduce the height of the figure for this 1D plot.

I have tried setting ticks, figure sizes, tight_layout, margin, etc. with no luck.

Also, changing the ylimits just spans the full figure height no matter what limits I choose.

import numpy as np
import matplotlib.pyplot as plot
from matplotlib import rcParams

x = np.array([3500])
y = np.array([0])

rcParams['toolbar'] = 'None'

plot.figure(num=None, figsize=(4, 1), dpi=80, facecolor='w')
plot.axes(frameon=False)
plot.yticks([0])
plot.xlim(0, 6000)
plot.ylim(-0.1, 0.1)
plot.plot(x, y, 'x', markersize=10)

plot.show()

Current figure:

Current figure

Desired figure:

Desired figure

like image 953
vaderade Avatar asked Oct 29 '25 08:10

vaderade


2 Answers

Try this:

plot.ylim(lower_limit, upper_limit)

Where lower_limit is the value you want to set for the bottom of the y-axis and upper_limit is the value for the top.

np.random.seed(0)
x = np.random.randn(100)
y = np.random.randn(100)

plot.figure(num=None, figsize=(4, 1), dpi=80, facecolor='w')
plot.axes(frameon=False)
plot.ylim(-10, 10)
plot.plot(x, y, '.')

enter image description here

like image 130
Alexander Avatar answered Oct 31 '25 02:10

Alexander


The code in the question already produces the desired result, when adding plot.tight_layout() at the end.

enter image description here

Of course decreasing figure size even further, shrinks the plot even more. E.g. figsize=(4, 0.7)

enter image description here

like image 34
ImportanceOfBeingErnest Avatar answered Oct 31 '25 03:10

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!