Reputation: 689
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color='red')
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
If one was to plot this data, the dots on the axes are partially cut off. Is there a way to prevent this (i.e. can the dots be plotted on top of the axes)?
Upvotes: 1
Views: 1075
Reputation: 31
(For those who would like to try a different method)
I was facing the same issue for quite a while. I realized that I had set the global rc_params at a place and in that I set my x/y margins to 0. Changing these margins to a small value like 0.1 helped me.
Using Matplotlib:
import matplotlib as mpl
mpl.rcParams["axes.xmargin"] = 0.1
mpl.rcParams["axes.ymargin"] = 0.1
Using Seaborns sns.set
sns.set(
rc={
"axes.xmargin": 0.1,
"axes.ymargin": 0.1,
}
)
Upvotes: 1
Reputation: 4852
Setting the clip_on
attribute to False
allows you to go beyond the axes, but by default the axes will be on top. For example, the script
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color="red", clip_on=False)
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
Yields the following.
Note that the axes "cut through" the dots. If you want the dots to go on top of the axes/labels, you need to change the default zorder
. For example, the script
x = [1, 2, 3, 4]
y = [0, 1, 7, 2]
plt.scatter(x, y, color="red", clip_on=False, zorder = 10)
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])
yields
Note: any zorder
value 3 or greater will work here.
Upvotes: 2