python - 在 Python 中使用 matplotlib 在 semilogy plot 上防止轴采用科学记数法(10 的幂)

标签 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()

最佳答案

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

参见 http://matplotlib.org/api/ticker_api.html对于所有可用的 Formatter 类。

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

关于python - 在 Python 中使用 matplotlib 在 semilogy plot 上防止轴采用科学记数法(10 的幂),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40336020/

相关文章:

python - TypeError 'set' 对象不支持项目分配

python - ModuleNotFoundError : No module named 'pymysql' in jupyter

python - 如何在同一张图上绘制多条具有偏移量的曲线

python - 如何使用 strftime 以正确的顺序绘制月份名称?

python - TensorFlow 将图像张量调整为动态形状

python - sklearn auc 分数 - diffmetrics.roc_auc_score & model_selection.cross_val_score

python - 在Python中循环运行多个数据帧的回归

python - 有什么方法可以获取使用 parser.add_option 创建的选项的输入,而不获取同一选项的任何输入?

python - 使用每个烛台单独着色的自定义烛台图表绘制

python - LSTM 模型不会预测高于特定值的值(始终不相同的值)