【问题标题】:Prevent axes from being in scientific notation (powers of 10) using matplotlib in Python on semilogy plot在符号学图上使用 Python 中的 matplotlib 防止轴采用科学记数法(10 的幂)
【发布时间】:2016-10-31 11:59:30
【问题描述】:

我已阅读此处 (How to prevent numbers being changed to exponential form in Python matplotlib figure) 和此处 (Matplotlib: disable powers of ten in log plot) 并尝试了他们的解决方案,但均无济于事。

如何将我的 y 轴转换为显示普通十进制数字而不是科学记数法?请注意这是 Python 3.5.2。

这是我的代码:

#Imports:
import matplotlib.pyplot as plt

possible_chars = 94
max_length = 8
pw_possibilities = [] 
for num_chars in range(1, max_length+1):
    pw_possibilities.append(possible_chars**num_chars)

x = range(1, max_length+1)
y = pw_possibilities 

#plot 
plt.figure()
plt.semilogy(x, y, 'o-')
plt.xlabel("num chars in password")
plt.ylabel("number of password possibilities")
plt.title("password (PW) possibilities verses # chars in PW")
plt.show()

【问题讨论】:

    标签: python python-3.x matplotlib plot scientific-notation


    【解决方案1】:

    你想如何显示10^15?作为1000000000000000?!另一个答案适用于默认格式化程序,当您切换到对数刻度时,使用具有不同规则集的 LogFormatter。您可以切换回ScalarFormatter 并禁用偏移量

    import matplotlib.pyplot as plt
    import matplotlib.ticker as mticker
    plt.ion()
    
    possible_chars = 94
    max_length = 8
    pw_possibilities = [] 
    for num_chars in range(1, max_length+1):
        pw_possibilities.append(possible_chars**num_chars)
    
    x = range(1, max_length+1)
    y = pw_possibilities 
    
    #plot 
    fig, ax = plt.subplots()
    ax.semilogy(x, y, 'o-')
    ax.set_xlabel("num chars in password")
    ax.set_ylabel("number of password possibilities")
    ax.set_title("password (PW) possibilities verses # chars in PW")
    ax.yaxis.set_major_formatter(mticker.ScalarFormatter())
    ax.yaxis.get_major_formatter().set_scientific(False)
    ax.yaxis.get_major_formatter().set_useOffset(False)
    fig.tight_layout()
    plt.show()
    

    请参阅 http://matplotlib.org/api/ticker_api.html 了解所有可用的 Formatter 类。

    (此图像是从 2.x 分支生成的,但应该适用于所有最新版本的 mpl)

    【讨论】:

    • 看起来很棒!我正在向一个中学生展示这个,所以是的,虽然 10^15 作为 1000000000000000 是不可读的,但它对所有零的影响更大。是否可以强制使用每 3 个零放置逗号的格式?
    • 查看编辑,您可能想使用matplotlib.org/api/…matplotlib.org/api/…
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2022-10-10
    • 1970-01-01
    • 2018-10-13
    • 2023-03-07
    • 2014-11-03
    • 2011-08-23
    相关资源
    最近更新 更多