代码之家  ›  专栏  ›  技术社区  ›  Kay Jansen

在对数刻度上放置刻度尺

  •  0
  • Kay Jansen  · 技术社区  · 10 年前

    我有一个带有插图的绘图,在我想要的位置设置记号位置和标签时遇到了问题。

    我有一个带插图的图表,我有插图的问题。我的插图代码如下:

    import matplotlib.pyplot as plt 
    fontsmall=16
    left, bottom, width, height = [.14, 0.70, 0.16, 0.20]
    ax3 = fig.add_axes([left, bottom, width, height])
    plt.yscale('log')
    plt.xscale('log')
    
    
    ax3.set_xlim(0.6,10)
    ax3.set_ylim(1,1*10**3)
    
    xaxis=[1,10]
    yaxis=np.power(xaxis,2.5)
    ax3.plot(xaxis,yaxis,lw=2,color='black')
    
    ax3.tick_params(labelleft='off',labelright='on')
    ax3.yaxis.set_label_position("right")
    ax3.tick_params(axis="x", pad=-0.5)
    ax3.tick_params(axis='y',which='minor',left='off',right='off')
    ax3.tick_params(axis='y',which='major',left='on',right='on')
    ax3.set_ylabel('$K\'_0$ (Pa)',fontsize=fontsmall,rotation=270)
    ax3.yaxis.labelpad = 19
    ax3.xaxis.labelpad = -9
    ax3.set_xlabel('$c_p$ (mg/ml)',fontsize=fontsmall)
    
    #I want to have ticks at these positions:
    ax3.yaxis.set_ticks((10**0,10**1,10**2,10**3))
    
    #But the labels I want at position 10**1 and 10**3, in log format
    
    #trying to remove 10**0 and 10**2, but this makes the labels in 10, and 1000 instead of the log format
    #labels = [item.get_text() for item in ax3.get_yticklabels()] 
    #labels[0] = ''
    #labels[2] = ''
    #
    #ax3.set_yticklabels(labels)
    
    #other method I came across:
    a=ax3.get_yticks().tolist()
    a[0]=''
    a[2]=''
    ax3.set_yticklabels(a)
    #again, 10 and 1000 instead of the log format, when trying to change this:
    ax3.yaxis.set_major_formatter(matplotlib.ticker.LogFormatter())
    #now all 4 labels are visible again for some reason, and not in the 10^0  format I want
    #
    #
    

    我看过多篇文章,希望更改记号和标签的位置。然而,我还没有看到一篇文章同时改变了ticklabels的位置,并将其更改为我想要的正确日志格式。我怎么能做到这一点?

    或者,我也看到了一篇帖子,他们将标签设置为“隐形”:

    for label in ax3.get_yticklabels():
        label.set_visible(False)
    

    但这会删除所有标签,这也不是我想要的。是否有方法只选择要删除的标签?

    1 回复  |  直到 10 年前
        1
  •  0
  •   Ajean Vishal Agrawal    10 年前

    你离得很近,你要做的就是关闭两个特定标签的可见性,而不是全部。因此,不要使用for循环,只需像处理文本那样显式地执行。

    labels = ax3.get_yticklabels()
    labels[0].set_visible(False)
    labels[2].set_visible(False)