对数y轴使刻度标签消失

时间:2015-11-21 18:32:53

标签: python matplotlib axis-labels

将行plt.yscale('log')添加到我的简单绘图脚本

import numpy as np
residuals = np.loadtxt('res_jacobi.txt', skiprows=1)

import matplotlib.pyplot as plt
fig = plt.figure()

steps = np.arange(0, len(residuals), 1)

plt.plot(steps, residuals, label='$S$')

plt.xlabel("Step",fontsize=20)
plt.ylabel("$S$",fontsize=20)
plt.ylim(0.95 * min(residuals), 1.05 * max(residuals))
plt.yscale('log')

plt.savefig('jacobi-res.pdf', bbox_inches='tight', transparent=True)

y标签消失。

enter image description here enter image description here

我确定有一个简单的解决办法,但搜索并未解决问题。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

{0.1, 1.0}的正常行为是仅标记对数缩放中的主刻度标记 - 即偶数个数量级,例如plt.gca().set_ylim(0.1, 1.0)。你的价值观都在这些之间。你可以:

  • 将您的轴重新缩放到更大的边界,
    plt.gca().yaxis.set_minor_formatter(FormatStrFormatter("%.2f"))
  • 手动标记刻度标记,
    {{1}}

答案 1 :(得分:0)

semilogy适合我。

变化:

plt.plot(steps, residuals, label='$S$')

分为:

plt.semilogy(steps, residuals, label='$S$')

删除plt.yscale('log')

相关问题