混叠的散点图点在轴上

2024-09-28 01:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某些原因,当我使用zorder散点图时,点的边与轴重叠。我尝试了[这里](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


Tags: tonppltparamsaxarrayworkset
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()

你的圆尺寸足够大,它们超出了轴的范围。所以我们只需更改ylimxlim

已更改

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

ax.set_ylim(-1,6)
ax.set_xlim(-1,4)

而且,zorder在将点推到边缘方面也不起作用。你知道吗

enter image description here

对我来说,您链接到的解决方案是有效的;也就是说,将散点图的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

可以使用以下代码修复重叠,代码中的zorder有一个较大的数字。这将在x轴和y轴上工作。你知道吗

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

相关问题 更多 >

    热门问题