散点图覆盖的 matplotlib 轴刻度标签(使用刺) [英] matplotlib axis tick labels covered by scatterplot (using spines)

查看:86
本文介绍了散点图覆盖的 matplotlib 轴刻度标签(使用刺)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的轴通过散点图中的原点 (0,0),这就是我在下面的示例中设置刺位置的原因.问题是散点图上的实际数据点覆盖了轴刻度标签,因此无法看到它们.

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.

如何让 matplotlib 用我的轴刻度标签覆盖"数据点,以便可以看到它们?

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

推荐答案

您可以通过将 scatter 的 zorder 设置为 <0

You can fix this by setting the zorder of scatter to something <0

例如:

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

不幸的是,即使这样,它们在 ggplot 样式中也不是很明显:

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)

这篇关于散点图覆盖的 matplotlib 轴刻度标签(使用刺)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆