刻度定位器

显示不同的刻度定位器。

刻度定位器示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.ticker as ticker
  4. # Setup a plot such that only the bottom spine is shown
  5. def setup(ax):
  6. ax.spines['right'].set_color('none')
  7. ax.spines['left'].set_color('none')
  8. ax.yaxis.set_major_locator(ticker.NullLocator())
  9. ax.spines['top'].set_color('none')
  10. ax.xaxis.set_ticks_position('bottom')
  11. ax.tick_params(which='major', width=1.00)
  12. ax.tick_params(which='major', length=5)
  13. ax.tick_params(which='minor', width=0.75)
  14. ax.tick_params(which='minor', length=2.5)
  15. ax.set_xlim(0, 5)
  16. ax.set_ylim(0, 1)
  17. ax.patch.set_alpha(0.0)
  18. plt.figure(figsize=(8, 6))
  19. n = 8
  20. # Null Locator
  21. ax = plt.subplot(n, 1, 1)
  22. setup(ax)
  23. ax.xaxis.set_major_locator(ticker.NullLocator())
  24. ax.xaxis.set_minor_locator(ticker.NullLocator())
  25. ax.text(0.0, 0.1, "NullLocator()", fontsize=14, transform=ax.transAxes)
  26. # Multiple Locator
  27. ax = plt.subplot(n, 1, 2)
  28. setup(ax)
  29. ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5))
  30. ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))
  31. ax.text(0.0, 0.1, "MultipleLocator(0.5)", fontsize=14,
  32. transform=ax.transAxes)
  33. # Fixed Locator
  34. ax = plt.subplot(n, 1, 3)
  35. setup(ax)
  36. majors = [0, 1, 5]
  37. ax.xaxis.set_major_locator(ticker.FixedLocator(majors))
  38. minors = np.linspace(0, 1, 11)[1:-1]
  39. ax.xaxis.set_minor_locator(ticker.FixedLocator(minors))
  40. ax.text(0.0, 0.1, "FixedLocator([0, 1, 5])", fontsize=14,
  41. transform=ax.transAxes)
  42. # Linear Locator
  43. ax = plt.subplot(n, 1, 4)
  44. setup(ax)
  45. ax.xaxis.set_major_locator(ticker.LinearLocator(3))
  46. ax.xaxis.set_minor_locator(ticker.LinearLocator(31))
  47. ax.text(0.0, 0.1, "LinearLocator(numticks=3)",
  48. fontsize=14, transform=ax.transAxes)
  49. # Index Locator
  50. ax = plt.subplot(n, 1, 5)
  51. setup(ax)
  52. ax.plot(range(0, 5), [0]*5, color='White')
  53. ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25))
  54. ax.text(0.0, 0.1, "IndexLocator(base=0.5, offset=0.25)",
  55. fontsize=14, transform=ax.transAxes)
  56. # Auto Locator
  57. ax = plt.subplot(n, 1, 6)
  58. setup(ax)
  59. ax.xaxis.set_major_locator(ticker.AutoLocator())
  60. ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
  61. ax.text(0.0, 0.1, "AutoLocator()", fontsize=14, transform=ax.transAxes)
  62. # MaxN Locator
  63. ax = plt.subplot(n, 1, 7)
  64. setup(ax)
  65. ax.xaxis.set_major_locator(ticker.MaxNLocator(4))
  66. ax.xaxis.set_minor_locator(ticker.MaxNLocator(40))
  67. ax.text(0.0, 0.1, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes)
  68. # Log Locator
  69. ax = plt.subplot(n, 1, 8)
  70. setup(ax)
  71. ax.set_xlim(10**3, 10**10)
  72. ax.set_xscale('log')
  73. ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15))
  74. ax.text(0.0, 0.1, "LogLocator(base=10, numticks=15)",
  75. fontsize=15, transform=ax.transAxes)
  76. # Push the top of the top axes outside the figure because we only show the
  77. # bottom spine.
  78. plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)
  79. plt.show()

下载这个示例