对数演示

具有对数轴的图的示例。

对数演示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Data for plotting
  4. t = np.arange(0.01, 20.0, 0.01)
  5. # Create figure
  6. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
  7. # log y axis
  8. ax1.semilogy(t, np.exp(-t / 5.0))
  9. ax1.set(title='semilogy')
  10. ax1.grid()
  11. # log x axis
  12. ax2.semilogx(t, np.sin(2 * np.pi * t))
  13. ax2.set(title='semilogx')
  14. ax2.grid()
  15. # log x and y axis
  16. ax3.loglog(t, 20 * np.exp(-t / 10.0), basex=2)
  17. ax3.set(title='loglog base 2 on x')
  18. ax3.grid()
  19. # With errorbars: clip non-positive values
  20. # Use new data for plotting
  21. x = 10.0**np.linspace(0.0, 2.0, 20)
  22. y = x**2.0
  23. ax4.set_xscale("log", nonposx='clip')
  24. ax4.set_yscale("log", nonposy='clip')
  25. ax4.set(title='Errorbars go negative')
  26. ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y)
  27. # ylim must be set after errorbar to allow errorbar to autoscale limits
  28. ax4.set_ylim(bottom=0.1)
  29. fig.tight_layout()
  30. plt.show()

下载这个示例