Gridspec演示06

Gridspec演示06

  1. import matplotlib.pyplot as plt
  2. import matplotlib.gridspec as gridspec
  3. import numpy as np
  4. from itertools import product
  5. def squiggle_xy(a, b, c, d):
  6. i = np.arange(0.0, 2*np.pi, 0.05)
  7. return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)
  8. fig = plt.figure(figsize=(8, 8))
  9. # gridspec inside gridspec
  10. outer_grid = gridspec.GridSpec(4, 4, wspace=0.0, hspace=0.0)
  11. for i in range(16):
  12. inner_grid = gridspec.GridSpecFromSubplotSpec(3, 3,
  13. subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0)
  14. a = i // 4 + 1
  15. b = i % 4 + 1
  16. for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
  17. ax = plt.Subplot(fig, inner_grid[j])
  18. ax.plot(*squiggle_xy(a, b, c, d))
  19. ax.set_xticks([])
  20. ax.set_yticks([])
  21. fig.add_subplot(ax)
  22. all_axes = fig.get_axes()
  23. #show only the outside spines
  24. for ax in all_axes:
  25. for sp in ax.spines.values():
  26. sp.set_visible(False)
  27. if ax.is_first_row():
  28. ax.spines['top'].set_visible(True)
  29. if ax.is_last_row():
  30. ax.spines['bottom'].set_visible(True)
  31. if ax.is_first_col():
  32. ax.spines['left'].set_visible(True)
  33. if ax.is_last_col():
  34. ax.spines['right'].set_visible(True)
  35. plt.show()

Total running time of the script: ( 0 minutes 2.041 seconds)

下载这个示例