【问题标题】:Matplotlib Plot with Finer tick marks but no labelsMatplotlib Plot 带有更精细的刻度但没有标签
【发布时间】:2019-06-13 08:53:38
【问题描述】:

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

所以我尝试了:

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

因为我需要所有 4 个侧面的刻度。然后我会做这样的事情:

ax.yaxis.set_ticks_position('both')
ax.xaxis.set_ticks_position('both') 

然后指定范围:

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

但我想要每 5 个位置(或每 5 个刻度)的数字标记

【问题讨论】:

标签: python matplotlib plot label axes


【解决方案1】:

这是我的解决方案:(感谢@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())       

附上成品

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多