Home > Software engineering >  Matplotlib changes values of input vector and outputs wrong plot
Matplotlib changes values of input vector and outputs wrong plot

Time:02-16

I'm using Python version 3.9.7 and Matplotlib 3.4.2 and when I run the following commands in Jupyter Lab, I get the output shown below:

import matplotlib.pyplot as plt

plt.plot([0.65945484, 0.65945478])

Matplotlib changes the input values and adds a weird text to the top of the output

What's going on?

CodePudding user response:

Maybe this can be better explained using different values.

1. If you run plt.plot([12, 5]), you may see that you get a similar descending line. Taking a look at the y-axis we can see that it spans from 5 to 12, i.e. our input values, whereas the x-axis spans from 0 to 1.

2. If you then run plt.plot([0,1], [12, 5]) we can see the exact same line with the exact same axes values.

enter image description here

So what's happening?

Looking at 2. our inputs lists are taken as coordinates. In particular, it is as if we were providing two points, say A = (0, 12) and B = (1, 5), and plt.plot() is drawing a line from point A to point B. In other words, we are doing

x0 = 0
x1 = 1

y0 = 12
y1 = 5

plt.plot([x0, x1], [y0, y1])

The same happens with your case, where you provide only one list. Here, the single list is interpreted as [y0, y1], and the missing [x0, x1], since it is not provided, is set as its default [0, 1].

In conclusion, your code is drawing a line from the point (0, 0.65945484) to the point (1, 0.65945478). And yes, the axes values suck.

CodePudding user response:

plt.plot is changing the scale of the y-axis to the point that the labels become hard to understand. The text at the top is the scale. 1e-8 is multiplied by the y-tick labels and added to 6.5945400000e-1 and that gives the input values.

  • Related