英文:
How to display all major and minor tick labels with log scale
问题
我试图绘制一个x-y图,其中x轴是对数坐标。我使用以下命令实现了这个目标:
import matplotlib.pyplot as plt
plt.plot(X, Y)
plt.xscale('log')
x轴是对数坐标,但是图中仅显示了10的倍数的值,我需要所有网格线上的值都显示在图中。
如图所示,x轴上只显示了10^6
,但我需要其他点的值也显示出来。
英文:
I am trying to plot an xy-graph whose x-axis is logarithmic. I did that using this command:
import matplotlib as plt
plt.plot(X,Y)
plt.xscale('log')
The x-axis is logarithmic, but only values which are multiples of 10 are displayed in the graph, but I need values for all gridlines to be shown in the graph.
As can be seen in the picture, only 10^6
is displayed on the x-axis, but I need values for other points to also be displayed.
答案1
得分: 1
- 已在
python 3.11.4
和matplotlib 3.7.1
中测试 - 根据重复问题:
- 如图所示,包括次要x刻度标签会拥挤x轴。
- 此答案显示如何显示特定的次要xtick标签,通过选择性设置
subs
(例如subs=[.2, .4, .6, .8]
)。
- 此答案显示如何显示特定的次要xtick标签,通过选择性设置
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import numpy as np
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(25, 6))
ax.semilogx(x, y)
# 显示所有次要和主要xticks,以及所有主要xtick标签
ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
# 删除此行以删除次要xtick标签
ax.xaxis.set_minor_formatter(tkr.FormatStrFormatter('%d'))
- Stack Overflow上有许多问题讨论了自定义格式化程序的用法。
.set_minor_formatter
.set_major_formatter
- 交换
x
和y
,设置次要刻度标签格式和字体大小,可以提供更好的视觉体验。
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(10, 20))
# 交换x和y
ax.semilogy(y, x)
ax.yaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.yaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
# 调整次要标签格式
mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.yaxis.set_minor_formatter(mkformatter)
# 调整字体大小
ax.yaxis.set_tick_params(which='minor', labelsize=5)
ax.yaxis.set_tick_params(which='major', labelsize=8)
# 设置y轴限制
ax.set_ylim(1, 10**10)
# 保存图像
fig.savefig('logscale.png')
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(25, 6))
ax.semilogx(x, y)
ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.xaxis.set_minor_formatter(mkformatter)
# 设置次要xtick标签旋转(在这种情况下,隐式pyplot命令最简单)
plt.xticks(rotation=90, ha='right', minor=True)
# 调整字体大小
ax.xaxis.set_tick_params(which='minor', labelsize=5)
ax.xaxis.set_tick_params(which='major', labelsize=8)
ax.set_xlim(1, 10**10)
fig.savefig('logscale.png')
英文:
- Tested in
python 3.11.4
,matplotlib 3.7.1
- As per the duplicates:
- As the plot shows, including the minor x-tick labels crowds the x-axis.
- This answer shows how to show specific minor xtick labels, by selectively setting
subs
(e.g.subs=[.2, .4, .6, .8]
).
- This answer shows how to show specific minor xtick labels, by selectively setting
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import numpy as np
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(25, 6))
ax.semilogx(x, y)
# show all minor and major xticks, and all major xtick labels
ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
# remove this line to remove the minor xtick labels
ax.xaxis.set_minor_formatter(tkr.FormatStrFormatter('%d'))
- There are many questions on Stack Overflow, which discuss the use of customized formatters.
.set_minor_formatter
.set_major_formatter
- Swapping
x
andy
, setting the minor tick label format, and the font size, can make for an improved visual experience.
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(10, 20))
# swap x and y
ax.semilogy(y, x)
ax.yaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.yaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
# adjust the minor label format
mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.yaxis.set_minor_formatter(mkformatter)
# adjust the font size
ax.yaxis.set_tick_params(which='minor', labelsize=5)
ax.yaxis.set_tick_params(which='major', labelsize=8)
# set the y limits
ax.set_ylim(1, 10**10)
# save figure
fig.savefig('logscale.png')
y = np.arange(11)
x = 10.0**y
fig, ax = plt.subplots(figsize=(25, 6))
ax.semilogx(x, y)
ax.xaxis.set_major_locator(tkr.LogLocator(numticks=999))
ax.xaxis.set_minor_locator(tkr.LogLocator(numticks=999, subs="all"))
mkfunc = lambda x, pos: '%1.0fM' % (x * 1e-6) if x >= 1e6 else '%1.0fK' % (x * 1e-3) if x >= 1e3 else '%1.0f' % x
mkformatter = tkr.FuncFormatter(mkfunc)
ax.xaxis.set_minor_formatter(mkformatter)
# set the minor xtick label rotation (in this case, the implicit pyplot command is easiest)
plt.xticks(rotation=90, ha='right', minor=True)
# adjust the font size
ax.xaxis.set_tick_params(which='minor', labelsize=5)
ax.xaxis.set_tick_params(which='major', labelsize=8)
ax.set_xlim(1, 10**10)
fig.savefig('logscale.png')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论