3

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()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
mat4mlw
  • 121
  • 1
  • 2
  • 9

2 Answers2

3

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:

enter image description here

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)

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Does it makes sense that `ax.tick_params(labelcolor='r', zorder=100)` doesnt work? – Rutger Kassies Sep 18 '15 at 13:51
  • 1
    @RutgerKassies See [here](http://stackoverflow.com/questions/19677963/matplotlib-keep-grid-lines-behind-the-graph-but-the-y-and-x-axis-above), it seems like this is a known issue/bug/something? – tmdavison Sep 18 '15 at 14:02
0

Another option is to add

ax.set_axisbelow(False)

after you do plt.scatter(), and the output is

axis zorder

zyy
  • 1,271
  • 15
  • 25