散点图重叠轴

时间:2019-08-13 04:31:01

标签: python matplotlib

由于某种原因,当我在散点图中使用zorder时,点的边缘与轴重叠。我尝试了[here](matplotlib axis tick labels covered by scatterplot (using spines))中的一些解决方案,但它们对我不起作用。有办法防止这种情况发生吗?

我知道我也可以在边界添加一个ax.axvline(),但这对于许多地块来说是个烦人的解决方法。

xval = np.array([0,0,0,3,3,3,0,2,3,0])
yval = np.array([0,2,3,5,1,0,1,0,4,5])
zval = yval**2-4

fig = plt.figure(figsize=(6,6))

ax = plt.subplot(111)

ax.scatter(xval,yval,cmap=plt.cm.rainbow,c=zval,s=550,zorder=20)

ax.set_ylim(0,5)
ax.set_xlim(0,3)


#These don't work
ax.tick_params(labelcolor='k', zorder=100)
ax.tick_params(direction='out', length=4, color='k', zorder=100)

#This will work but I don't want to have to do this for the plot edges every time
ax.axvline(0,c='k',zorder=100)


plt.show()

enter image description here

3 个答案:

答案 0 :(得分:2)

对我来说,您链接的解决方案有效;也就是说,将散点图的z阶设置为负数。例如

xval = np.array([0,0,0,3,3,3,0,2,3,0])
yval = np.array([0,2,3,5,1,0,1,0,4,5])
zval = yval**2-4

fig = plt.figure(figsize=(6,6))

ax = plt.subplot(111)

ax.scatter(xval,yval,cmap=plt.cm.rainbow,c=zval,s=550,zorder=-1)

ax.set_ylim(0,5)
ax.set_xlim(0,3)

plt.show()

Test plot with axes sitting on top of scatter points] 1

答案 1 :(得分:1)

这对我有用

     Name  RegistrationID        RegisterTime  ActionGroup
0  Jordan               1 2017-08-01 00:00:05            1
1  Jordan               2 2017-08-01 00:00:08            1
2  Jordan               3 2017-08-01 00:00:10            1
3   Sarah               4 2017-08-01 00:00:15            2
4  Jordan              42 2017-08-01 00:00:16            1
5   Sarah              54 2017-08-01 00:00:20            2
6  Jordan              53 2017-08-01 00:00:30            3
7  Jordan              55 2017-08-01 00:00:32            3

您的圆的大小足够大,以至于超出了轴范围。因此,我们只需更改import numpy as np import matplotlib.pyplot as plt xval = np.array([0,0,0,3,3,3,0,2,3,0]) yval = np.array([0,2,3,5,1,0,1,0,4,5]) zval = yval**2-4 fig = plt.figure(figsize=(6,6)) ax = plt.subplot(111) ax.scatter(xval,yval,cmap=plt.cm.rainbow,c=zval,s=550,zorder=20) ax.set_ylim(-1,6) ax.set_xlim(-1,4) #These don't work ax.tick_params(labelcolor='k', zorder=100) ax.tick_params(direction='out', length=4, color='k', zorder=100) #This will work but I don't want to have to do this for the plot edges every time ax.axvline(0,c='k',zorder=100) plt.show() ylim

已更改

xlim

ax.set_ylim(0,5)
ax.set_xlim(0,3)

此外,zorder在将点推到边缘时不起作用。

enter image description here

答案 2 :(得分:1)

您可以使用以下带有zorder的大量代码来修复重叠。这将同时在x轴和y轴上起作用。

for k,spine in ax.spines.items():
    spine.set_zorder(1000)