Matplotlib使用更细的刻度线绘制,但没有标签

2024-05-12 18:40:08 发布

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

enter image description here我正在尝试生成一个绘图,其中我在x和y轴上都自定义了轴刻度。但就像在比例尺上一样,我希望有更细的刻度线,而刻度线只出现在较大的数值上。如附件所示(与我想要的相似)。在

所以我试着:

ax.tick_params(labeltop=True, labelright=True)

因为我需要四个边上的记号。然后我会这样做:

^{pr2}$

然后指定范围:

plt.xticks(np.arange(-1.20, -0.79, 0.04))
plt.yticks(np.arange(-1.0,1.2, 0.1))

但我希望数字标记每5个位置(或每5个记号)


Tags: true绘图附件nppltparamsax数值
1条回答
网友
1楼 · 发布于 2024-05-12 18:40:08

我的解决办法是:(谢谢@Diziet)

from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,AutoMinorLocator)   
# Plotting codes go in here
ax.yaxis.set_ticks_position('both')# Ticks on all 4 sides                                                                                                                                                                                                                                    
ax.xaxis.set_ticks_position('both')                                                                                                                                                                                                                                    
plt.xticks(np.arange(-1.21, -0.79, 0.04)) # Axes ranges                                                                                                                                                                                                                             
plt.yticks(np.arange(-0.9,0.9, 0.1))                                                                                                                                                                                                                                   
ax.xaxis.set_major_locator(MultipleLocator(0.05)) # Intervals for major x-ticks                                                                                                                                                                                                                     
ax.xaxis.set_minor_locator(AutoMinorLocator()) # Minor ticks : Automatic filling based on the ytick range                                                                                                                                                                                                                        
ax.yaxis.set_major_locator(MultipleLocator(0.3))  # For y-ticks                                                                                                                                                                                                                     
ax.yaxis.set_minor_locator(AutoMinorLocator())       

附件是最终产品Plot with finer ticks

相关问题 更多 >