Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 928
Zelix75 Avatar asked Oct 18 '25 13:10

Zelix75


2 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

like image 130
Ann Zen Avatar answered Oct 20 '25 06:10

Ann Zen


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

like image 20
Rahul Ramesh Avatar answered Oct 20 '25 06:10

Rahul Ramesh