1

I have a problem with y axis. It works fine until I put too high number into axis function. Here's the code.

import matplotlib.pyplot as plt
plt.style.available

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]

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

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=100, c=y_values, cmap=plt.cm.Blues)

ax.set_title("Kwadraty liczb", fontsize=24)
ax.set_xlabel("Wartość", fontsize=14)
ax.set_ylabel("Kwadraty wartości", fontsize=14)


ax.axis([0, 1100, 0, 1100000])

plt.show()

When i was trying to figure out why it's not working I typed different values into ymax in ax.axis and for example it works for ax.axis([0, 1100, 0 , 10000]) but when I want to get 1100000 this happens https://i.stack.imgur.com/6gSOE.png

Lukasz
  • 11
  • 2
  • 3
    Can you maybe edit your post and include an image of the results you get? It seems to work fine for me – Solvalou Mar 14 '21 at 19:50
  • 4
    Hi Lukasz, I've tried your script and it looks good. What is the problem that you have? – Francesco Montesano Mar 14 '21 at 19:51
  • I had a problem with adding a photo so i'll leave a link to it. As u can see on a picture values on y axis are completely different from expected ones. – Lukasz Mar 14 '21 at 20:06
  • I feel like this exact question was asked a week ago. All you need to do add `ax.ticklabel_format(style='plain')`. No custom object is needed. – hrokr Mar 14 '21 at 21:04

2 Answers2

2

If you mean that the y axis tick labels are automatically divided by one million (indicated by the "1e6" above the tick labels), you can configure this by creating a custom ScalarFormatter object and turning scientific notation off. For more details, see the matplotlib documentation pages on tick formatters and on ScalarFormatter.

import matplotlib.pyplot as plt
from matplotlib import ticker
plt.style.available

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]

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

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=100, c=y_values, cmap=plt.cm.Blues)

ax.set_title("Kwadraty liczb", fontsize=24)
ax.set_xlabel("Wartość", fontsize=14)
ax.set_ylabel("Kwadraty wartości", fontsize=14)

ax.axis([0, 1100, 0, 1100000])

formatter = ticker.ScalarFormatter()
formatter.set_scientific(False)
ax.yaxis.set_major_formatter(formatter)

plt.show()

Note that I have added a new import statement (from matplotlib import ticker) and the three lines above plt.show(). If you want this to also apply to the x axis (in case you enlarge this axis), just add a similar line of code: ax.xaxis.set_major_formatter(formatter).

enter image description here

luuk
  • 1,630
  • 4
  • 12
0

While you can go through all the extra steps of creating a custom ScalarFormatter object as luuk suggested, you could achieve the same thing by simply configuring what is already provided with ax.ticklabel_format(style='plain'):

import matplotlib.pyplot as plt
#plt.style.available

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]

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

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.ticklabel_format(style='plain')

ax.scatter(x_values, y_values, s=100, c=y_values,cmap=plt.cm.Blues)

ax.set_title("Kwadraty liczb", fontsize=24)
ax.set_xlabel("Wartość", fontsize=14)
ax.set_ylabel("Kwadraty wartości", fontsize=14)

ax.axis([0, 1100, 0, 1100000])

plt.show()

Still results in the same chart desired

enter image description here

Also, you'll notice I commented out plt.style.available because it's useless, being overwritten by plt.style.use('seaborn').

hrokr
  • 3,276
  • 3
  • 21
  • 39