在半月形图上使用Python中的matplotlib防止轴采用科学记数法(10的幂)

时间:2016-10-31 03:00:19

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

我已经在这里(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。

enter image description here

这是我的代码:

#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()

1 个答案:

答案 0 :(得分:3)

您想如何显示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()

example output

有关所有可用的Formatter课程,请参阅http://matplotlib.org/api/ticker_api.html

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