Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用matplotlib和seaborn在打印值上显示轴刻度标签?_Python_Matplotlib_Seaborn_Scatter Plot - Fatal编程技术网

Python 如何使用matplotlib和seaborn在打印值上显示轴刻度标签?

Python 如何使用matplotlib和seaborn在打印值上显示轴刻度标签?,python,matplotlib,seaborn,scatter-plot,Python,Matplotlib,Seaborn,Scatter Plot,我使用matplotlib和seaborn来散布包含在两个数组中的一些数据,x和y(二维图)。这里的问题是,在数据上显示两个轴标签时,因为打印的数据与值重叠,因此不可见 我尝试过不同的可能性,比如在绘图完成后重置标签,稍后再设置标签,或者使用注释标记。不管怎样,这些选择中的任何一个都对我有用 我用来生成散点图的代码是: sns.set_style("whitegrid") ax = sns.scatterplot(x=x, y=y, s=125) ax.set_xlim(-20, 20) a

我使用matplotlib和seaborn来散布包含在两个数组中的一些数据,x和y(二维图)。这里的问题是,在数据上显示两个轴标签时,因为打印的数据与值重叠,因此不可见

我尝试过不同的可能性,比如在绘图完成后重置标签,稍后再设置标签,或者使用注释标记。不管怎样,这些选择中的任何一个都对我有用

我用来生成散点图的代码是:

sns.set_style("whitegrid")

ax = sns.scatterplot(x=x, y=y, s=125)

ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)

ax.spines['left'].set_position('zero')
ax.spines['left'].set_color('black')

ax.spines['right'].set_color('none')
ax.yaxis.tick_left()

ax.spines['bottom'].set_position('zero')
ax.spines['bottom'].set_color('black')

ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()

values = ax.get_xticks()
ax.set_xticklabels(["{0:.0%}".format(x/100) for x in values])

values = ax.get_yticks()
ax.set_yticklabels(["{0:.0%}".format(y/100) for y in values])

ax.tick_params(axis='both', which='major', labelsize=15)

ax.grid(True)
生成的绘图如下所示:

但期望的输出应该是这样的:


提前感谢您的任何建议或帮助

您需要两件事:
zorder
和标签的
bold
权重。分散点的
zorder
需要低于刻度标签,以便后者显示在顶部<代码>'fontweight':'bold'将有粗体刻度标签

轴似乎偏移了0,但这是因为您没有提供任何数据。所以我必须选择一些随机数据

# import commands here

x = np.random.randint(-100, 100, 10000)
y = np.random.randint(-100, 100, 10000)
ax = sns.scatterplot(x=x, y=y, s=125, zorder=-1)

# Rest of the code

values = ax.get_xticks()
ax.set_xticklabels(["{0:.0%}".format(x/100) for x in values], fontdict={'fontweight': 'bold'})

values = ax.get_yticks()
ax.set_yticklabels(["{0:.0%}".format(y/100) for y in values], fontdict={'fontweight': 'bold'})

ax.tick_params(axis='both', which='major', labelsize=15, zorder=1)
ax.grid(True)

谢谢!zorder参数是我问题的解决方案。