在对数刻度上放置刻度标签

Placing ticklabels on logscale

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

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

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
#
#

我看过多个 post 想要更改刻度和标签的位置。但是,我还没有遇到 post 既可以更改滴答标签的位置,又可以将其更改为我希望在此处拥有的正确日志格式。我该怎么做?

或者,我也看到了 post 他们把标签放在 'invisible':

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

但这会删除所有标签,这也不是我想要的。有没有办法只删除 select 个我想删除的标签?

您非常接近,您只想关闭两个特定标签的可见性,而不是全部。因此,不要使用 for 循环,只需像您尝试处理文本那样明确地执行它。

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