如何在matplotlib中使子图刻度线更长?

28

我正在尝试调整Python多面板子图中坐标轴上的刻度标记。我有两个共享公共x轴的面板。我已经将图的边框加粗,并使所有沿坐标轴的刻度标记变粗。我的问题如下:

1.如何使所有刻度标记(两个轴)更长,以便更容易看到?

2.如何在主要刻度标记之间添加较小但仍可见的刻度标记?

以下是迄今为止所拥有的最小工作示例。

from numpy import *
from scipy import *
from pylab import *
from random import *
import pylab
import matplotlib.pyplot as plt


#make the axis border thick
pylab.rc("axes", linewidth=4.0)
pylab.rc("lines", markeredgewidth=4)


#create a figure with two panels that shares the x-axis
f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False)
#example figure1
ax1.plot(range(2),range(2),linewidth=2)
#example figure 2
ax2.plot(range(2),range(2),linewidth=2)



# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
show()
1个回答

32

请尝试以下方法:

#example figure1
ax1.plot(range(2),range(2),linewidth=2)
ax1.minorticks_on()
ax1.tick_params('both', length=20, width=2, which='major')
ax1.tick_params('both', length=10, width=1, which='minor')

您可以对ax2重复上述步骤。这样对您有帮助吗?


2
你如何获取axes.tick_params()可用参数的列表? - Atcold
2
@Atcold 这些内容在文档中可以找到:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html - weefwefwqg3
为了隐藏一个不需要的刻度线,我最终使用了(略微不同的语法)ax.xaxis.set_tick_params(length=0, width=0, which='minor') - PatrickT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接