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

Viewed 4474

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

3 Answers

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

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

Related