【问题标题】:Is it possible to draw xticklabels on top of the xaxis?是否可以在 x 轴顶部绘制刻度标签?
【发布时间】:2019-01-09 23:49:25
【问题描述】:

我想用在 x 轴顶部绘制的彩色星号来标记特定的 x 轴位置。

我使用 x-tick 标签作为标记(因为我不知道是否可以在无花果坐标中的任何位置放置标记)它已正确对齐但绘制在 x 轴下方,因此它被部分覆盖.

MWE:

import numpy as np
import matplotlib.pyplot as plt

fig,ax=plt.subplots(1,1)

ax.scatter([-1,1],[1,1])
ax.set_xticks([0],minor=True)
ax.set_xticklabels(['*'],minor=True,color='r',fontsize=20,verticalalignment='center')

plt.setp(ax.spines.values(), linewidth=3)

plt.show()

这就是现在的样子:

【问题讨论】:

  • 您现在的输出有什么问题?从你的问题看问题不是很清楚
  • 我希望星号位于轴上方。现在,轴覆盖了符号的一部分,我希望它是相反的。不过,轴越粗越明显。
  • 我现在明白了。检查我的答案,如果这是你想要的,请告诉我

标签: python matplotlib alignment


【解决方案1】:

您可以指定混合系统中散点的坐标(y 轴的数据坐标和 x 轴的轴坐标)。
然后将散射标记设置在刺上方,将散射的zorder 属性设置为高于 2.5。

import matplotlib.pyplot as plt

fig,ax=plt.subplots(1,1)

ax.scatter([-1,1],[1,1])
ax.scatter(0,0, s=100, marker="*", color="red", 
           transform=ax.get_xaxis_transform(), clip_on=False, zorder=3)

plt.show()

【讨论】:

  • 谢谢,这是使用不同标记的非常好的解决方案
【解决方案2】:

您要查找的是zorder 参数。通过使用zorder = 0,您基本上可以定义绘图序列的堆叠顺序。 0 将在后台发送轴/框架,根据需要将星号放在轴线上。我增加了星号的大小以突出显示它。

ax.scatter([-1,1],[1,1])
ax.set_xticks([0],minor=True)
ax.set_xticklabels(['*'],minor=True,color='r',fontsize=30,verticalalignment='center')

plt.setp(ax.spines.values(), linewidth=3, zorder=0)

或者,您也可以为两个绘图命令指定zorder,但对星号使用更高的zorder

ax.set_xticklabels(['*'],minor=True,color='r',fontsize=30,verticalalignment='center', zorder=2)
plt.setp(ax.spines.values(), linewidth=3, zorder=1)

【讨论】:

  • 谢谢,这正是我所需要的
  • @Jonathan:如果您觉得答案有帮助,您可以将其标记为已接受。由于这是您的第一个问题,您应该阅读this
猜你喜欢
  • 1970-01-01
  • 2021-12-11
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2021-11-16
  • 2011-03-28
  • 1970-01-01
相关资源
最近更新 更多