matplotlib axis tick labels covered by scatterplot (using spines)

假装没事ソ 提交于 2019-12-23 15:48:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!