解剖图

下图显示了组成一个图的几个matplotlib元素的名称。

解剖图

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
  4. np.random.seed(19680801)
  5. X = np.linspace(0.5, 3.5, 100)
  6. Y1 = 3+np.cos(X)
  7. Y2 = 1+np.cos(1+X/0.75)/2
  8. Y3 = np.random.uniform(Y1, Y2, len(X))
  9. fig = plt.figure(figsize=(8, 8))
  10. ax = fig.add_subplot(1, 1, 1, aspect=1)
  11. def minor_tick(x, pos):
  12. if not x % 1.0:
  13. return ""
  14. return "%.2f" % x
  15. ax.xaxis.set_major_locator(MultipleLocator(1.000))
  16. ax.xaxis.set_minor_locator(AutoMinorLocator(4))
  17. ax.yaxis.set_major_locator(MultipleLocator(1.000))
  18. ax.yaxis.set_minor_locator(AutoMinorLocator(4))
  19. ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))
  20. ax.set_xlim(0, 4)
  21. ax.set_ylim(0, 4)
  22. ax.tick_params(which='major', width=1.0)
  23. ax.tick_params(which='major', length=10)
  24. ax.tick_params(which='minor', width=1.0, labelsize=10)
  25. ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')
  26. ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)
  27. ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
  28. ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
  29. ax.plot(X, Y3, linewidth=0,
  30. marker='o', markerfacecolor='w', markeredgecolor='k')
  31. ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
  32. ax.set_xlabel("X axis label")
  33. ax.set_ylabel("Y axis label")
  34. ax.legend()
  35. def circle(x, y, radius=0.15):
  36. from matplotlib.patches import Circle
  37. from matplotlib.patheffects import withStroke
  38. circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,
  39. edgecolor='black', facecolor=(0, 0, 0, .0125),
  40. path_effects=[withStroke(linewidth=5, foreground='w')])
  41. ax.add_artist(circle)
  42. def text(x, y, text):
  43. ax.text(x, y, text, backgroundcolor="white",
  44. ha='center', va='top', weight='bold', color='blue')
  45. # Minor tick
  46. circle(0.50, -0.10)
  47. text(0.50, -0.32, "Minor tick label")
  48. # Major tick
  49. circle(-0.03, 4.00)
  50. text(0.03, 3.80, "Major tick")
  51. # Minor tick
  52. circle(0.00, 3.50)
  53. text(0.00, 3.30, "Minor tick")
  54. # Major tick label
  55. circle(-0.15, 3.00)
  56. text(-0.15, 2.80, "Major tick label")
  57. # X Label
  58. circle(1.80, -0.27)
  59. text(1.80, -0.45, "X axis label")
  60. # Y Label
  61. circle(-0.27, 1.80)
  62. text(-0.27, 1.6, "Y axis label")
  63. # Title
  64. circle(1.60, 4.13)
  65. text(1.60, 3.93, "Title")
  66. # Blue plot
  67. circle(1.75, 2.80)
  68. text(1.75, 2.60, "Line\n(line plot)")
  69. # Red plot
  70. circle(1.20, 0.60)
  71. text(1.20, 0.40, "Line\n(line plot)")
  72. # Scatter plot
  73. circle(3.20, 1.75)
  74. text(3.20, 1.55, "Markers\n(scatter plot)")
  75. # Grid
  76. circle(3.00, 3.00)
  77. text(3.00, 2.80, "Grid")
  78. # Legend
  79. circle(3.70, 3.80)
  80. text(3.70, 3.60, "Legend")
  81. # Axes
  82. circle(0.5, 0.5)
  83. text(0.5, 0.3, "Axes")
  84. # Figure
  85. circle(-0.3, 0.65)
  86. text(-0.3, 0.45, "Figure")
  87. color = 'blue'
  88. ax.annotate('Spines', xy=(4.0, 0.35), xycoords='data',
  89. xytext=(3.3, 0.5), textcoords='data',
  90. weight='bold', color=color,
  91. arrowprops=dict(arrowstyle='->',
  92. connectionstyle="arc3",
  93. color=color))
  94. ax.annotate('', xy=(3.15, 0.0), xycoords='data',
  95. xytext=(3.45, 0.45), textcoords='data',
  96. weight='bold', color=color,
  97. arrowprops=dict(arrowstyle='->',
  98. connectionstyle="arc3",
  99. color=color))
  100. ax.text(4.0, -0.4, "Made with http://matplotlib.org",
  101. fontsize=10, ha="right", color='.5')
  102. plt.show()

下载这个示例