在Matplotlib中显示具有较大Y轴范围的双对数图的Y轴上的所有次要刻度

dced5bon  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(86)

我试图在Matplotlib中创建一个双对数图,其中y轴的范围很大。但是,Matplotlib似乎无法显示y轴上的所有次要刻度。
下面是我使用的代码:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as tck

# do the plot
fig = plt.figure(figsize=(6, 5))
ax = fig.add_subplot(111)

ax.tick_params(which='major', direction='in', width=2, length=7, top=True, right=True, pad=10)
ax.tick_params(which='minor', direction='in', width=1, length=5, top=True, right=True)

plt.xticks(fontsize=18)
plt.yticks(fontsize=18)

ax.xaxis.set_minor_locator(tck.AutoMinorLocator())
ax.yaxis.set_minor_locator(tck.AutoMinorLocator())

col = ['dodgerblue']

data = np.array([[1.e-1, 4.e-37], [1.e+1, 1.e-33], [1.e+3, 7.e-26]])

plt.loglog(data[:, 0], data[:, 1], col[0], linewidth=2.5)

plt.ylabel(r'$y$', fontsize=22)
plt.xlabel(r'$x$', fontsize=22)

plt.ylim(1e-37, 1e-25)
plt.xlim(0.1, 10000)

locmaj = tck.LogLocator(base=10, numticks=12)
ax.yaxis.set_major_locator(locmaj)

# Use MultipleLocator to set minor ticks at multiples of the base (10 in this case)
ax.yaxis.set_minor_locator(tck.MultipleLocator(base=10))

# Use LogFormatter for both major and minor ticks to display appropriate scientific notation
ax.yaxis.set_major_formatter(tck.LogFormatter())
ax.yaxis.set_minor_formatter(tck.LogFormatter())

plt.show()

字符串
输出图仅显示y轴上的主要刻度,即shown here
但是,我想展示所有的小滴答声。有没有人可以帮助我实现这一点?

cedebl8k

cedebl8k1#

就像使用locmaj获取主刻度一样,也可以设置次刻度。在set_major_ticks()之后,添加这些行并注解掉代码中的其他行,如下所示...

# ....
locmaj = tck.LogLocator(base=10, numticks=12)
ax.yaxis.set_major_locator(locmaj)

## Add the set_minor_locator() code to set the minor ticks.
## Note that the number of ticks will be based on the np.arange(), which has 8 here
locmin = tck.LogLocator(base=10, subs=np.arange(2, 10) * .1)
ax.yaxis.set_minor_locator(locmin)
ax.yaxis.set_minor_formatter(tck.NullFormatter())

# Use MultipleLocator to set minor ticks at multiples of the base (10 in this case)
## Commented
#ax.yaxis.set_minor_locator(tck.MultipleLocator(base=10))

# Use LogFormatter for both major and minor ticks to display appropriate scientific notation
## Commented
#ax.yaxis.set_major_formatter(tck.LogFormatter())
## Commented
#ax.yaxis.set_minor_formatter(tck.LogFormatter())
plt.show()

字符串


的数据

相关问题