使用ScalarFormat标记格式

该示例显示了ScalarFormatter与不同设置的使用。

例子1:默认

例子2:没有图形偏移

例子3:使用Mathtext

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.ticker import ScalarFormatter

例子1:

  1. x = np.arange(0, 1, .01)
  2. fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6))
  3. fig.text(0.5, 0.975, 'The new formatter, default settings',
  4. horizontalalignment='center',
  5. verticalalignment='top')
  6. ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5)
  7. ax1.xaxis.set_major_formatter(ScalarFormatter())
  8. ax1.yaxis.set_major_formatter(ScalarFormatter())
  9. ax2.plot(x * 1e5, x * 1e-4)
  10. ax2.xaxis.set_major_formatter(ScalarFormatter())
  11. ax2.yaxis.set_major_formatter(ScalarFormatter())
  12. ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10)
  13. ax3.xaxis.set_major_formatter(ScalarFormatter())
  14. ax3.yaxis.set_major_formatter(ScalarFormatter())
  15. ax4.plot(-x * 1e5, -x * 1e-4)
  16. ax4.xaxis.set_major_formatter(ScalarFormatter())
  17. ax4.yaxis.set_major_formatter(ScalarFormatter())
  18. fig.subplots_adjust(wspace=0.7, hspace=0.6)

使用ScalarFormat标记格式示例

例子2:

  1. x = np.arange(0, 1, .01)
  2. fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6))
  3. fig.text(0.5, 0.975, 'The new formatter, no numerical offset',
  4. horizontalalignment='center',
  5. verticalalignment='top')
  6. ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5)
  7. ax1.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  8. ax1.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  9. ax2.plot(x * 1e5, x * 1e-4)
  10. ax2.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  11. ax2.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  12. ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10)
  13. ax3.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  14. ax3.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  15. ax4.plot(-x * 1e5, -x * 1e-4)
  16. ax4.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  17. ax4.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
  18. fig.subplots_adjust(wspace=0.7, hspace=0.6)

使用ScalarFormat标记格式示例2

例子3:

  1. x = np.arange(0, 1, .01)
  2. fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6))
  3. fig.text(0.5, 0.975, 'The new formatter, with mathtext',
  4. horizontalalignment='center',
  5. verticalalignment='top')
  6. ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5)
  7. ax1.xaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  8. ax1.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  9. ax2.plot(x * 1e5, x * 1e-4)
  10. ax2.xaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  11. ax2.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  12. ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10)
  13. ax3.xaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  14. ax3.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  15. ax4.plot(-x * 1e5, -x * 1e-4)
  16. ax4.xaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  17. ax4.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
  18. fig.subplots_adjust(wspace=0.7, hspace=0.6)
  19. plt.show()

使用ScalarFormat标记格式示例3

下载这个示例