Ric*_*son 8 python plot matplotlib axis-labels
在matplotlib,当我log在一个轴上使用刻度时,可能会发生该轴没有主要刻度,只有较小的刻度.所以这意味着没有为整个轴显示标签.
如何指定我还需要标签以用于次要刻度?
我试过了:
plt.setp(ax.get_xticklabels(minor=True), visible=True)
Run Code Online (Sandbox Code Playgroud)
......但它并没有成功.
小智 13
您可以set_minor_tickformatter在相应的轴上使用:
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter
axes = plt.subplot(111)
axes.loglog([3,4,7], [2,3,4])
axes.xaxis.set_minor_formatter(FormatStrFormatter("%.2f"))
plt.xlim(1.8, 9.2)
plt.show()
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多方法来让小标记在日志图中正常工作.如果您可以显示可以使用的刻度值的日志matplotlib.ticker.LogFormatterExponent.我记得尝试matplotlib.ticker.LogFormatter但是我不喜欢它:如果我记得很清楚它会把所有东西放进去base^exp(也是0.1,0,1).在这两种情况下(以及所有其他情况matplotlib.ticker.LogFormatter*),您必须设置labelOnlyBase=False为获得次要滴答.
我最终创建了一个自定义函数并使用matplotlib.ticker.FuncFormatter.我的方法假设刻度线为整数值,并且您需要基数10日志.
from matplotlib import ticker
import numpy as np
def ticks_format(value, index):
"""
get the value and returns the value as:
integer: [0,99]
1 digit float: [0.1, 0.99]
n*10^m: otherwise
To have all the number of the same size they are all returned as latex strings
"""
exp = np.floor(np.log10(value))
base = value/10**exp
if exp == 0 or exp == 1:
return '${0:d}$'.format(int(value))
if exp == -1:
return '${0:.1f}$'.format(value)
else:
return '${0:d}\\times10^{{{1:d}}}$'.format(int(base), int(exp))
subs = [1.0, 2.0, 3.0, 6.0] # ticks to show per decade
ax.xaxis.set_minor_locator(ticker.LogLocator(subs=subs)) #set the ticks position
ax.xaxis.set_major_formatter(ticker.NullFormatter()) # remove the major ticks
ax.xaxis.set_minor_formatter(ticker.FuncFormatter(ticks_format)) #add the custom ticks
#same for ax.yaxis
Run Code Online (Sandbox Code Playgroud)
如果你没有删除主要的刻度并使用subs = [2.0, 3.0, 6.0]主要和次要刻度的字体大小是不同的(这可能是因为text.usetex:False在我使用matplotlibrc)
| 归档时间: |
|
| 查看次数: |
16552 次 |
| 最近记录: |