Zelix75
Zelix75

Reputation: 81

How to include negative values in y-axis with matplotlib?

So, I'm making a scatter plot, and one of my data columns has lot of negative values. That is my y-axis.
However when I plot it starts my axes from 0. How do I make it start from the actual lowest value?
My Chart

Upvotes: 4

Views: 13109

Answers (3)

Rahul Ramesh
Rahul Ramesh

Reputation: 100

Set your limit.
For example : plt.ylim(-5, 5)

Upvotes: 3

David George Grundy
David George Grundy

Reputation: 21

A quick solution might be matplotlib.pyplot.ylim(). Get your lowest y value and pass it into this to set it as your lowest y axis value.

Something like matplotlib.pyplot.ylim(bottom= np.min(y_array)).

See https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.ylim.html for more details.

Upvotes: 2

Red
Red

Reputation: 27547

Use plt.ylim(), along with the built-in min() and max() functions:

import matplotlib.pyplot as plt

x = [38, -21, 37, 54, 10, 10, -40, -37, -24, 53, -10, 45, -32, -37, 12, -29, 18, 5, -45, 19, 48, 48, 27, -10, -17, -15, -25, -44, 41, -8]
y = [31, -43, -3, 18, -48, 4, -54, -34, -42, 13, 31, -4, 17, 3, 16, -30, -23, -27, -9, 13, -40, 13, 0, -41, 5, -26, 16, -9, 40, 16]

plt.ylim(min(y), max(y))
plt.scatter(x, y)
plt.show()

Output:

enter image description here

Upvotes: 3

Related Questions