gpt4 book ai didi

python - 散点图覆盖的 matplotlib 轴刻度标签(使用脊柱)

转载 作者:行者123 更新时间:2023-11-28 17:33:36 25 4
gpt4 key购买 nike

我想让我的轴通过散点图中的原点 (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

最佳答案

您可以通过设置 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

关于python - 散点图覆盖的 matplotlib 轴刻度标签(使用脊柱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com