ggplot样式表

此示例演示了“ggplot”样式,该样式调整样式以模拟ggplotR的流行绘图包)。

这些设置被无耻地从[1]窃取(经允许)。

[1] https://web.archive.org/web/20111215111010/http://www.huyng.com/archives/sane-color-scheme-for-matplotlib/691/

ggplot样式表示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. plt.style.use('ggplot')
  4. # Fixing random state for reproducibility
  5. np.random.seed(19680801)
  6. fig, axes = plt.subplots(ncols=2, nrows=2)
  7. ax1, ax2, ax3, ax4 = axes.ravel()
  8. # scatter plot (Note: `plt.scatter` doesn't use default colors)
  9. x, y = np.random.normal(size=(2, 200))
  10. ax1.plot(x, y, 'o')
  11. # sinusoidal lines with colors from default color cycle
  12. L = 2*np.pi
  13. x = np.linspace(0, L)
  14. ncolors = len(plt.rcParams['axes.prop_cycle'])
  15. shift = np.linspace(0, L, ncolors, endpoint=False)
  16. for s in shift:
  17. ax2.plot(x, np.sin(x + s), '-')
  18. ax2.margins(0)
  19. # bar graphs
  20. x = np.arange(5)
  21. y1, y2 = np.random.randint(1, 25, size=(2, 5))
  22. width = 0.25
  23. ax3.bar(x, y1, width)
  24. ax3.bar(x + width, y2, width,
  25. color=list(plt.rcParams['axes.prop_cycle'])[2]['color'])
  26. ax3.set_xticks(x + width)
  27. ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
  28. # circles with colors from default color cycle
  29. for i, color in enumerate(plt.rcParams['axes.prop_cycle']):
  30. xy = np.random.normal(size=2)
  31. ax4.add_patch(plt.Circle(xy, radius=0.3, color=color['color']))
  32. ax4.axis('equal')
  33. ax4.margins(0)
  34. plt.show()

下载这个示例