带有饼图标记的散点图

此示例将自定义 ‘饼图’ 作为散点图的标记。

感谢 Manuel Metz 的例子

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # first define the ratios
  4. r1 = 0.2 # 20%
  5. r2 = r1 + 0.4 # 40%
  6. # define some sizes of the scatter marker
  7. sizes = np.array([60, 80, 120])
  8. # calculate the points of the first pie marker
  9. #
  10. # these are just the origin (0,0) +
  11. # some points on a circle cos,sin
  12. x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
  13. y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
  14. xy1 = np.column_stack([x, y])
  15. s1 = np.abs(xy1).max()
  16. x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
  17. y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
  18. xy2 = np.column_stack([x, y])
  19. s2 = np.abs(xy2).max()
  20. x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
  21. y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
  22. xy3 = np.column_stack([x, y])
  23. s3 = np.abs(xy3).max()
  24. fig, ax = plt.subplots()
  25. ax.scatter(range(3), range(3), marker=xy1,
  26. s=s1 ** 2 * sizes, facecolor='blue')
  27. ax.scatter(range(3), range(3), marker=xy2,
  28. s=s2 ** 2 * sizes, facecolor='green')
  29. ax.scatter(range(3), range(3), marker=xy3,
  30. s=s3 ** 2 * sizes, facecolor='red')
  31. plt.show()

带有饼图标记的散点图示例

参考

本例中显示了下列函数、方法、类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.scatter
  3. matplotlib.pyplot.scatter

下载这个示例