Python matplotlib draws curve outside axis by default?
Question:
Does Python matplotlib by default plot outside the axis if matplotlib.pyplot.xlim
is applied?
This is the code I have written.
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(-10, 10, 11)
y = x**2
plt.plot(x, y)
plt.xlim((-5, 5))
This is what I get: enter image description here
This is what I want:
enter image description here
I am using matplotlib version 3.5.3 with Spyder IDE.
Answers:
I solved it by updating all libs in anaconda by :
conda update --all
and disabling inline graphics generation in Spyder.
It seems like ‘clip_on’ is setted to ‘False’.
Try to set it to ‘True’ when you plot:
plt.plot(x, y, clip_on = True)
Another option is to clip your x and y before plotting:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(-10, 10, 11)
y = x**2
mask = np.logical_and(np.less_equal(x,5), np.greater_equal(x,-5))
new_x = x[mask]
new_y = y[mask]
plt.plot(new_x, new_y, clip_on=False) #clip_on=False just for demonstration
plt.xlim((-5, 5))
Thats way, eventhough ‘clip_on’ is setted to ‘False’, there won’t be data outside your grid.
I updated Spyder from 5.1.5 to 5.3.3 by
conda uninstall spyder
conda install spyder=5.3.3
Now Spyder does not plot across the axes anymore.
edit:
I was wrong! The problem still occurs. I could find out that the Spyder IPython console format option causes the error. By default "PNG" is set. I typically use "SVG". That is why I first thought the update fixed the issue.
When I choose "SVG" lines still are drawn outside the axis. I executed
plt.plot([1,2,3,4,5,6,7]), plt.xlim((2, 3))
However, when I choose "PNG" everything works fine.
I will ignore this bug, because when I choose "SVG", execute
plt.savefig("myfig.pdf")
and then open the file, all lines are within the box.
Does Python matplotlib by default plot outside the axis if matplotlib.pyplot.xlim
is applied?
This is the code I have written.
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(-10, 10, 11)
y = x**2
plt.plot(x, y)
plt.xlim((-5, 5))
This is what I get: enter image description here
This is what I want:
enter image description here
I am using matplotlib version 3.5.3 with Spyder IDE.
I solved it by updating all libs in anaconda by :
conda update --all
and disabling inline graphics generation in Spyder.
It seems like ‘clip_on’ is setted to ‘False’.
Try to set it to ‘True’ when you plot:
plt.plot(x, y, clip_on = True)
Another option is to clip your x and y before plotting:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(-10, 10, 11)
y = x**2
mask = np.logical_and(np.less_equal(x,5), np.greater_equal(x,-5))
new_x = x[mask]
new_y = y[mask]
plt.plot(new_x, new_y, clip_on=False) #clip_on=False just for demonstration
plt.xlim((-5, 5))
Thats way, eventhough ‘clip_on’ is setted to ‘False’, there won’t be data outside your grid.
I updated Spyder from 5.1.5 to 5.3.3 by
conda uninstall spyder
conda install spyder=5.3.3
Now Spyder does not plot across the axes anymore.
edit:
I was wrong! The problem still occurs. I could find out that the Spyder IPython console format option causes the error. By default "PNG" is set. I typically use "SVG". That is why I first thought the update fixed the issue.
When I choose "SVG" lines still are drawn outside the axis. I executed
plt.plot([1,2,3,4,5,6,7]), plt.xlim((2, 3))
However, when I choose "PNG" everything works fine.
I will ignore this bug, because when I choose "SVG", execute
plt.savefig("myfig.pdf")
and then open the file, all lines are within the box.