简体   繁体   English

如何使用matplotlib和seaborn在绘制的值上显示轴刻度标签?

[英]How to display axis tick labels over plotted values using matplotlib and seaborn?

I am using matplotlib and seaborn in order to scatter plot some data contained in two arrays, x and y, (2-dimensional plot). 我使用matplotlib和seaborn来散布两个数组x和y(二维绘图)中包含的某些数据。 The issue here is when it comes to displaying both axis labels over the data, because plotted data overlaps the values so the are unseen. 这里的问题是要在数据上同时显示两个轴标签,因为绘制的数据与值重叠,因此看不到。

I have tried different possibilities such as resetting the labels after the plot is done and setting them later, or using annotation marks. 我尝试了各种可能性,例如在绘制完成后重置标签并稍后进行设置,或者使用注释标记。 Anyways any of those options worked for me... 无论如何,这些选择都对我有用。

The piece of code I am using to generate this scatter plot is: 我用来生成散点图的代码是:

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)

The generated plot is as follows: 生成的图如下: 生成的散点图

But the desired output should be something like this: 但是所需的输出应该是这样的: 所需散点图

Thank you in advance for any advice or help! 预先感谢您的任何建议或帮助!

You need two things: zorder and a bold weight for the ticklabels. 您需要两件事: zorderbold The zorder of scatter points needs to be lower than that of the tick labels so that the latter appear on the top. 散点的zorder必须低于对勾标签的zorder ,以便后者显示在顶部。 'fontweight': 'bold' is to have boldfaced tick labels. 'fontweight': 'bold'是带有粗体的勾号标签。

The axis appears shifted off the 0 but that is because you did not provide any data. 轴显示偏离0,但这是因为您未提供任何数据。 So I have to choose some random data 所以我必须选择一些随机数据

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Matplotlib 和 Seaborn 中的 x 轴刻度标记(不是刻度标签)和 x 轴之间添加填充 - How do you add padding between the x-axis tick marks (not tick labels) and the x-axis in Matplotlib and Seaborn 如何使用matplotlib在动态tkinter图表中更改轴刻度线标签 - How to Change Axis Tick Labels in dynamic tkinter chart using matplotlib 隐藏轴值但在 matplotlib 中保留轴刻度标签 - Hide axis values but keep axis tick labels in matplotlib Matplotlib pyplot 对数刻度轴刻度线标签(值)消失。 如何设置刻度线和标签? - Matplotlib pyplot Log-scale axis tick mark labels (values) dissapear. How to set tick marks and labels? 如何将 seaborn/matplotlib 轴刻度标签从数字格式化为数千或数百万? (125,436 到 125.4K) - How to format seaborn/matplotlib axis tick labels from number to thousands or Millions? (125,436 to 125.4K) 如何旋转 seaborn 条形图 x 轴刻度标签 - How to rotate seaborn barplot x-axis tick labels 如何在使用matplotlib的pyplot绘制的图形上设置x轴标签? - How to set x-axis labels on a figure plotted using matplotlib's pyplot? 如何在 Matplotlib 中更改刻度标签和轴标签之间的间隔 - How to change separation between tick labels and axis labels in Matplotlib Matplotlib线图位移X轴值和刻度标签 - Matplotlib Line Graph Shift X-Axis Values and Tick Labels 轴标签与matplotlib中的刻度标签一致 - Axis labels in line with tick labels in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM