使用尖刺自定义matplotlib中记号的一侧

2024-05-19 15:20:57 发布

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

我有一个matplotlib水平条,绘制如下:

import matplotlib.pyplot as plt
from numpy import *
from scipy import *
bars = arange(5) + 0.1
vals = rand(5)
print bars, vals
plt.figure(figsize=(5,5), dpi=100)
spines = ["bottom"]
ax = plt.subplot(1, 1, 1)
for loc, spine in ax.spines.iteritems():
  if loc not in spines:
    spine.set_color('none')
# don't draw ytick marks
#ax.yaxis.set_tick_params(size=0)
ax.xaxis.set_ticks_position('bottom')
plt.barh(bars, vals, align="center")
plt.savefig("test.png") 

这将生成此图像:enter image description here

我只想展示xaxis,它使用的是脊椎,但现在它为右手的yaxis绘制了这些悬挂的记号。我怎样才能去掉这些?我想保留ytickmarks在绘图的左侧,并使它们朝外(与条形图相反的方向)。我知道可以通过取消对该行的注释来完全删除ytickmarks:

#ax.yaxis.set_tick_params(size=0)

但我只想把ytick标记留在左手边。谢谢您。

编辑:经过一些尝试和错误之后,我获得了一个解决方案,但我确信我的解决方案可能不是最好的方法,所以请让我知道您仍然在想什么。我发现我能做到:

ax.spines["left"].axis.axes.tick_params(direction="outward") 

设置刻度线方向。要去掉右y轴刻度,我使用:

ax.yaxis.set_ticks_position("left")

Tags: fromimportmatplotlib绘制pltparamsaxloc
4条回答

您可以使用:

ax.tick_params(top="off")
ax.tick_params(bottom="off")
ax.tick_params(right="off")
ax.tick_params(left="off")

您只需使用:

ax.tick_params(axis='y', direction='out')

这将根据您的需要确定蜱虫的方位。以及:

ax.yaxis.tick_left()

这不会绘制正确的记号

相关问题 更多 >