散点图(使用轴脊)遮盖了Matplotlib轴刻度标签

3

我希望在散点图中将坐标轴轴线通过原点(0,0),因此我在下面的示例中设置了脊柱位置。问题是,我的实际数据点覆盖了坐标轴刻度标签,因此无法看到它们。

如何让matplotlib“覆盖”数据点并显示坐标轴刻度标签?

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

2个回答

3

您可以通过将散点图的zorder设置为<0来解决此问题。

例如:

plt.scatter(x,y,zorder=-5)

很遗憾,即使是在ggplot风格中,它们也不是很明显:enter image description here。如果您更改散点图和标签的颜色(请注意,标签在下面的图像中明显位于散点图上方),则可以更清楚地看到这一点,因此您可能需要尝试找到自己喜欢的东西。
ax.tick_params(labelcolor='r')
plt.scatter(x,y,color='w',zorder=-5)

enter image description here


ax.tick_params(labelcolor='r', zorder=100) 不起作用,这有意义吗? - Rutger Kassies
1
@RutgerKassies,请看这里,似乎这是一个已知的问题/错误/什么东西? - tmdavison

0

另一个选项是添加

ax.set_axisbelow(False)

在你执行plt.scatter()之后,输出结果为

axis zorder


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接