-1

我正在尝试在 matplotlib 中制作一个非常简单的日志图。但是,似乎我可以在 y 轴上得到小刻度,但由于某些 matplotlib 原因,而不是在 x 轴上。我想在所有轴(底部、左侧、右侧和顶部)上打勾,并在 x 轴(顶部和底部)上有小刻度。

这是我目前拥有的代码:

import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
from matplotlib.ticker import ScalarFormatter
from matplotlib import colors as mcolors
from matplotlib import gridspec

plt.rcParams.update({'font.size': 14})
plt.clf()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, sharex=True, sharey=True, gridspec_kw = {'wspace':0, 'hspace':0})

xmin =   0.006  
xmax = 70.00    
ymin =  0.0003  
ymax = 4.00 

ax1.set_xlim([xmin,xmax])
ax2.set_xlim([xmin,xmax])
ax3.set_xlim([xmin,xmax])
ax4.set_xlim([xmin,xmax])
ax1.set_xscale('log')
ax2.set_xscale('log')
ax3.set_xscale('log')
ax4.set_xscale('log')
ax1.set_ylim([ymin,ymax])
ax2.set_ylim([ymin,ymax])
ax3.set_ylim([ymin,ymax])
ax4.set_ylim([ymin,ymax])
ax1.set_yscale('log')
ax2.set_yscale('log')
ax3.set_yscale('log')
ax4.set_yscale('log')

ax1.plot(log_wavelength, t, alpha=0.75, color='black')
ax2.plot(log_wavelength, x, alpha=0.75, color='red')
ax3.plot(log_wavelength, y, alpha=0.75, color='green')
ax4.plot(log_wavelength, z, alpha=0.75, color='cyan')

ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax4.minorticks_on()

ax1.tick_params(axis='both', which='minor', length=4, color='k')
ax2.tick_params(axis='both', which='minor', length=4, color='k') 
ax3.tick_params(axis='both', which='minor', length=4, color='k')
ax4.tick_params(axis='both', which='minor', length=4, color='k')

ax.minorticks_on() 不起作用是有原因的吗?

4

1 回答 1

0

您可以使用 tick_params 进行更好的控制:

ax1.tick_params(axis='both', which='both', direction='in', top='on')

这是一个相当有用的功能,例如,如果您只想在图形窗口边界的 OUTSIDE 上有刻度:

ax1.tick_params(axis='both', which='both', direction='in', top='on', bottom='off', left='on', right='off')
ax2.tick_params(axis='both', which='both', direction='in', top='on', bottom='off', left='off', right='on')
ax3.tick_params(axis='both', which='both', direction='in', top='off', bottom='on', left='on', right='off')
ax4.tick_params(axis='both', which='both', direction='in', top='off', bottom='on', left='off', right='on')

您甚至可以通过设置“长度”和“宽度”参数来更改刻度长度/宽度,但您可能希望将“哪个”更改为“次要/主要”以单独调整它们。此外,您也可以从此处更改标记标签字体大小。例如

ax1.tick_params(axis='both', which='major', direction='in', length=6, width=1, labelsize=8)

回答你关于为什么 ax.minorticks_on() 不起作用的问题:不知道。我已经为此奋斗并失败了,最终决定改用 tick_params ......这是要走的路。

于 2018-04-11T19:50:52.473 回答