vaderade
vaderade

Reputation: 55

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

Upvotes: 4

Views: 17740

Answers (3)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

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

Upvotes: 3

Alexander
Alexander

Reputation: 109546

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

Upvotes: 9

fuglede
fuglede

Reputation: 18201

Simply changing figsize to (4, 0.3), I get something that more or less looks like your desired output (except with ticks at the top as well, but those can also be removed):

Output

Upvotes: 0

Related Questions