如何在对数刻度下显示所有主要和次要刻度标签

huangapple go评论77阅读模式
英文:

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.4matplotlib 3.7.1中测试
  • 根据重复问题:
    • 答案验证所有主要和次要刻度都已显示。
    • 答案显示如何格式化和显示次要xtick标签。
  • 如图所示,包括次要x刻度标签会拥挤x轴。
    • 答案显示如何显示特定的次要xtick标签,通过选择性设置subs(例如subs=[.2, .4, .6, .8])。
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
  • 交换xy,设置次要刻度标签格式和字体大小,可以提供更好的视觉体验。
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:
    • This answer verifies all major and minor ticks are displayed.
    • This answer shows how to format and show the minor xtick labels.
  • 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]).
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 and y, 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')

如何在对数刻度下显示所有主要和次要刻度标签

huangapple
  • 本文由 发表于 2023年7月20日 22:01:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定