【问题标题】:How to remove scientific notation from a log-log plot? [duplicate]如何从对数图中删除科学记数法? [复制]
【发布时间】:2020-08-04 16:08:18
【问题描述】:

我希望 y 轴仅显示数字 100、200 和 300,而不是科学计数法。有什么想法吗?

Current plot

简化代码:

from matplotlib import pyplot as plt
import numpy as np

x = np.logspace(2, 6, 20)
y = np.logspace(np.log10(60), np.log10(300), 20)

plt.scatter(x, y[::-1])
plt.xscale('log')
plt.yscale('log')

plt.show()

【问题讨论】:

  • 我想你想要ax.ticklabel_format(style="plain")。见here

标签: python matplotlib plot formatting scientific-notation


【解决方案1】:

主要和次要定位符确定刻度的位置。标准位置通过AutoLocator 设置。 NullLocator 删除它们。 MultipleLocator(x) 显示每多个 x 的刻度。

对于 y 轴,设置标准刻度位置会显示顶部的刻度彼此更接近,由对数刻度确定。但是,由于范围大,对 x 轴执行相同操作会使它们靠得太近。因此,对于 x 轴,由LogLocator 确定的位置可以保持不变。

格式化程序控制刻度的显示方式。 ScalarFormatter 设置默认方式。有一个选项scilimits 确定应该使用科学计数法的值范围。由于 1.000.000 通常显示为 1e6,因此设置 scilimits=(-6,9) 可以避免这种情况。

from matplotlib import pyplot as plt
from matplotlib import ticker
import numpy as np

x = np.logspace(2, 6, 20)
y = np.logspace(np.log10(60), np.log10(300), 20)

plt.scatter(x, y[::-1])
plt.xscale('log')
plt.yscale('log')
ax = plt.gca()

# ax.xaxis.set_major_locator(ticker.AutoLocator())
ax.xaxis.set_minor_locator(ticker.NullLocator())  # no minor ticks
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())  # set regular formatting

# ax.yaxis.set_major_locator(ticker.AutoLocator())  # major y tick positions in a regular way
ax.yaxis.set_major_locator(ticker.MultipleLocator(100))  # major y tick positions every 100

ax.yaxis.set_minor_locator(ticker.NullLocator())  # no minor ticks
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())  # set regular formatting

ax.ticklabel_format(style='sci', scilimits=(-6, 9))  # disable scientific notation

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多