问题
I want to have my axis through the origin (0,0) in a scatter plot, which is why I have set the spines positions in the below example. The problem is that my actual data points on the scatter plot are covering up the axis tick labels so they cannot be seen.
How can I get matplotlib to 'overwrite' the data points with my axis tick labels so that they can be seen?
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('ggplot')
x = np.random.randn(5000)
y = np.random.randn(5000)
f, ax = plt.subplots()
plt.scatter(x,y)
ax.spines['left'].set_position('zero')
ax.spines['left'].set_color('black')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['bottom'].set_color('black')
ax.spines['top'].set_color('none')
plt.show()
回答1:
You can fix this by setting the zorder
of scatter to something <0
For example:
plt.scatter(x,y,zorder=-5)
Unfortunately, even then they are not very visible in the ggplot
style:
You can see that more clearly if you change the color of the scatter plot and the labels (note that the labels are clearly above the scatter plot in the image below), so you may need to experiment to find something you like
ax.tick_params(labelcolor='r')
plt.scatter(x,y,color='w',zorder=-5)
来源:https://stackoverflow.com/questions/32652673/matplotlib-axis-tick-labels-covered-by-scatterplot-using-spines