0

I am trying to plot a simple lineplot using matplotlib, but it automatically changes the range of my plot.

My code:

y2=[49973731579774.89, 49973731579774.82, 49973731579774.76, 49973731579774.71, 49973731579774.66, 49973731579774.62, 49973731579774.59, 49973731579774.57, 49973731579774.555, 49973731579774.55, 49973731579774.52, 49973731579774.516, 49973731579774.51, 49973731579774.51, 49973731579774.5, 49973731579774.5, 49973731579774.49, 49973731579774.49, 49973731579774.49, 49973731579774.484, 49973731579774.48, 49973731579774.48, 49973731579774.47, 49973731579774.47, 49973731579774.47, 49973731579774.47, 49973731579774.46, 49973731579774.46, 49973731579774.45, 49973731579774.45, 49973731579774.45, 49973731579774.45, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.43, 49973731579774.43, 49973731579774.42, 49973731579774.42, 49973731579774.42, 49973731579774.42, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39,49973731579774.39]
x=np.arange(len(y2))
plt.ylim(np.min(y2),np.max(y2))
plt.plot(x,y2,label='Validation MSE',color="orange")

This is what I get:

enter image description here

I want to remove what I've enclosed in the red square of the image above and keep the original range, how can I do it?

Thanks in advance.

Edit:

I have tried changing the tick parameters like here: prevent scientific notation in matplotlib.pyplot

Using the following commands:

fig, ax = plt.subplots()
y2=[49973731579774.89, 49973731579774.82, 49973731579774.76, 49973731579774.71, 49973731579774.66, 49973731579774.62, 49973731579774.59, 49973731579774.57, 49973731579774.555, 49973731579774.55, 49973731579774.52, 49973731579774.516, 49973731579774.51, 49973731579774.51, 49973731579774.5, 49973731579774.5, 49973731579774.49, 49973731579774.49, 49973731579774.49, 49973731579774.484, 49973731579774.48, 49973731579774.48, 49973731579774.47, 49973731579774.47, 49973731579774.47, 49973731579774.47, 49973731579774.46, 49973731579774.46, 49973731579774.45, 49973731579774.45, 49973731579774.45, 49973731579774.45, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.445, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.44, 49973731579774.43, 49973731579774.43, 49973731579774.42, 49973731579774.42, 49973731579774.42, 49973731579774.42, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.414, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.41, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.4, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39, 49973731579774.39,49973731579774.39]
x=np.arange(len(y2))
plt.ylim(np.min(y2),np.max(y2))
ax.ticklabel_format(useOffset=False, style='plain')
plt.plot(x,y2,label='Validation MSE',color="orange")
plt.show()

But what just the range is changed, I want to keep the decimals. enter image description here

New Edit:

THIS IS THE IMAGE I WANT: enter image description here

Henry Navarro
  • 943
  • 8
  • 34
  • 1
    What do you mean by original range? Do you mean you want to keep the numbers 0.4062, 0.5078... 0.9062 and just remove the +4.99737...e13? Also, why have you used `x=np.arange(y2)`? I think it should be `x=np.arange(len(y2))` – Sheldore Feb 11 '20 at 15:20
  • Give us the code that reproduces this image. The code you gave is wrong – Sheldore Feb 11 '20 at 15:22
  • Why you set my question as duplicated if I am totally asking other things? It is true the code is not totally right because the copy/paste effect, but first, you should have to read the question. I don't want to prevent the scientific notation, I just want to delete the "+4...e13". Brainy... – Henry Navarro Feb 11 '20 at 16:55
  • If you just want to delete `"+4...e13"` then use the first line from [this answer](https://stackoverflow.com/a/45766598/4932316) which I linked. You should have made it clearer in this answer. I already asked you in my first comment. So yes, I linked the correct duplicates. You should just have tried the answers there – Sheldore Feb 11 '20 at 17:00
  • 1
    Ok, reopened. I still don't understand what your final desired plot should look like. Your question "I want to remove what I've enclosed in the red square of the image above and keep the original range, how can I do it?" is highly unclear.. You still haven't answered my first comment "What do you mean by original range?". Better to include some sample desired figure (even if handmade) – Sheldore Feb 11 '20 at 17:06
  • Ready, now??????? – Henry Navarro Feb 11 '20 at 17:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207605/discussion-between-danny-hern-and-sheldore). – Henry Navarro Feb 11 '20 at 17:21
  • See my answer if that works for you – Sheldore Feb 11 '20 at 17:28

1 Answers1

1

Try the following workaround.

fig, ax = plt.subplots()
y2 = np.array([49973731579774.89, 49973731579774.82, ....]) # Convert to NumPy array

x = np.arange(len(y2))
plt.ylim(np.min(y2), np.max(y2))

ax.ticklabel_format(useOffset=False, style='plain')
ax.plot(x, y2, label='Validation MSE',color="orange")

# Extract the current, default ticklabels and convert them to floats
# Then subtract the lowest, closest integer value to just keep the decimals
fig.canvas.draw()
new_ticks = [round(float(i.get_text().strip('$'))-np.floor(min(y2)), 4) for i in ax.get_yticklabels()]

# Set the new, desired ticklabels
ax.set_yticklabels(new_ticks);

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • I still don't get why `ax.yaxis.offsetText.set_visible(False)` is not the solution to be honest – user8408080 Feb 11 '20 at 18:03
  • @user8408080 : this is the solution BUT the OP wants to have the decimals only.. One way is to just plot `ax.plot(x, y2-np.floor(min(y2))` and get rid of the `ylim` and then use a formatter for float width for the tick labels. I tried that but the ticks are now at `0.4, 0.5, 0.6, 0.7, 0.8`. I think OP wants strictly the values shown in the graph – Sheldore Feb 11 '20 at 18:09