2

I wrote a few lines of code that creates a simple scatter plot. I want the numbers on the y-axis to range from 0 to 1000000. I know it's a big number and sometimes I might experience strange numerical problems but when I run this code the y-axis is ranging from 0 to 1. That's quite strange. I have to manually drag the graph if I want the graph to range in the numbers that I wish using the tools in the python window that pops up when I run the code. Otherwise, the numbers on the y-axis just simply doesn't range in the way I designated. ps: The numbers on the x-axis ranges perfectly fine though.

import matplotlib.pyplot as plt

x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]

plt.scatter(x_values, y_values, s=10)
plt.title('Square Numbers', fontsize=18)
plt.xlabel('Value', fontsize=18)
plt.ylabel('Square of Value', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=14)
plt.axis([0, 1000, 0, 1100000])
plt.show()
gboffi
  • 22,939
  • 8
  • 54
  • 85
  • The y-axis of your plot shows labels from 0 to 1, but at the top, matplotlib shows a scaling factor (1e6) (the official name is "offset text"). `plt.gca().yaxis.get_major_formatter().set_useOffset(False)` can help to avoid it. – JohanC Jul 26 '20 at 14:53
  • Does this answer your question? [Is ticklabel\_format broken?](https://stackoverflow.com/questions/18209462/is-ticklabel-format-broken) – JohanC Jul 26 '20 at 14:55

1 Answers1

0

The matplotlib will take the values of the any axis as a logarithmic value after reaching 1 million or above that. if u wish to see the values in numerical values:

(i) Slightly pan towards the -y axis. You will be able to see the number.

or

(ii) Set plt.axis([0, 1000, 0, 1100000]) to plt.axis([0, 1000, 0, 110000])

I hope it will work for you.

  • It works after changing the value of the y axis. It's unfortunate that I had to change the numbers but it worked anyways. Thanks for the help. – Jason Huawu Jul 27 '20 at 06:24