在符号图上使用Python中的matplotlib防止轴处于科学计数法(10的幂) [英] Prevent axes from being in scientific notation (powers of 10) using matplotlib in Python on semilogy plot

查看:2505
本文介绍了在符号图上使用Python中的matplotlib防止轴处于科学计数法(10的幂)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里阅读了( Matplotlib:在对数图中禁用10的幂),并尝试使用其解决方案无济于事.

I've read here (How to prevent numbers being changed to exponential form in Python matplotlib figure) and here (Matplotlib: disable powers of ten in log plot) and tried their solutions to no avail.

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

How can I convert my y-axis to display normal decimal numbers instead of scientific notation? Note this is 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()

推荐答案

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

How do you want to display 10^15? As 1000000000000000 ?! The other answer applies to the default formatter, when you switch to log scale a LogFormatter is used which has a different set of rules. You can switch back to ScalarFormatter and disable the offset

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的所有最新版本上都可以使用)

(this image is generated off of the 2.x branch, but should work on all recent version of mpl)

这篇关于在符号图上使用Python中的matplotlib防止轴处于科学计数法(10的幂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆