使用棘刺自定义matplotlib中刻度线的一侧 [英] customizing just one side of tick marks in matplotlib using spines

查看:157
本文介绍了使用棘刺自定义matplotlib中刻度线的一侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下绘制的matplotlib单杠:

I have a matplotlib horizontal bar drawn as follows:

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

这将产生此图像:

我只想显示使用刺的xaxis,但是现在它为右yaxis绘制了这些悬挂的刻度线.如何删除这些? id希望将ytickmark保留在图的左侧,并使它们朝外(与条相反的方向).我知道我可以通过取消注释行来完全删除ytickmarks:

I wanted to only show the xaxis, which worked using spines, but now it plots these hanging tickmarks for the right-hand yaxis. How can I remove these? id like to keep the ytickmarks on the left hand side of the plot and make them face out (direction opposite to bars). I know that I can remove the ytickmarks altogether by uncommenting the line:

#ax.yaxis.set_tick_params(size=0)

但是我只想在左侧保留ytick标记.谢谢.

but i want to keep the ytick marks only on the left hand side. thank you.

EDIT :经过一番尝试后,我获得了解决方案,尽管我确定我的解决方案可能不是最佳解决方案,所以请让我知道您的想法.我发现我可以做到:

EDIT: I achieved a solution after some trial and error though i'm sure my solution is probably not the best way to do it, so please let me know what you think still. i found that i can do:

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

设置刻度线方向.为了摆脱正确的y轴刻度,我使用:

to set the tick mark direction. to get rid of the right y-axis ticks, i use:

ax.yaxis.set_ticks_position("left")

推荐答案

您可以简单地使用:

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

这将根据需要调整刻度.并且:

this will orientate the ticks as you want. And:

ax.yaxis.tick_left()

这将不会绘制正确的刻度线

this will not plot the right ticks

这篇关于使用棘刺自定义matplotlib中刻度线的一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆