matplotlib 朝阳/扇形图

cu6pst1q  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(155)

我对在matplotlib中为二叉搜索树(例如在家谱中使用的二叉搜索树)开发一个自定义的朝阳图很感兴趣。我试图实现以下目标:

正如你所看到的,它是一个sunburst chart(如Plotly提供的),带有一个移除的楔形。

fruv7luv

fruv7luv1#

这里有一些东西至少可以让你开始。你可以使用matplotlib的pie图表来制作嵌套的饼图。你也可以删除特定的楔形,如https://stackoverflow.com/a/63881380/1862861所示。使用这些信息,你可以做:

  1. from matplotlib import pyplot as plt
  2. ninner = 2 # number of inner wedge
  3. width = 0.15 # width of inner wedges
  4. radius = 0.5 # radius of inner wedges
  5. gap = 100 # size of missing wedge
  6. ngap = 300 - gap # total size of filled wedges (missing gap will be 1/3 of total)
  7. fig, ax = plt.subplots()
  8. for i in range(8):
  9. if i > 0:
  10. # expand pie chart radius and shrink wedge width
  11. width *= 0.85
  12. radius += width
  13. # create data
  14. data = [gap]
  15. data.extend([ngap / 2**(i+1)] * ninner)
  16. colors = ["lightgrey"] * len(data) # for now they are all grey!
  17. ninner *= 2
  18. # create pie chart
  19. wedges, _ = ax.pie(
  20. data,
  21. radius=radius,
  22. colors=colors,
  23. wedgeprops={"width": width, "edgecolor": "w", "linewidth": 0.25},
  24. startangle=-150, # shift start angle
  25. )
  26. wedges[0].set_visible(False) # make gap invisible
  27. fig.tight_layout()
  28. fig.show()

其产生:

很明显,这并没有给给予颜色,但这是一个开始。

更新

以下是添加了一些颜色的版本:

  1. from matplotlib import pyplot as plt
  2. import numpy as np
  3. def get_colours(N):
  4. # use the tab20c colour map and get an array of colours
  5. # Note: the "16" in this is due to the 16 colours in the tab20c colour map
  6. cmap = plt.colormaps["tab20c"]
  7. cs = cmap(np.arange(16))
  8. if N <= 16:
  9. step = 16 // N
  10. colours = np.array([cs[i] for i in range(0, 16, step)])
  11. else:
  12. s = N // 16
  13. colours = np.array([cs[int(np.floor(i / s))] for i in range(N)])
  14. return colours
  15. ninner = 2 # number of inner wedge
  16. width = 0.15 # width of inner wedges
  17. radius = 0.5 # radius of inner wedges
  18. gap = 100 # size of missing wedge
  19. ngap = 300 - gap # total size of filled wedges (missing gap will be 1/3 of total)
  20. fig, ax = plt.subplots()
  21. for i in range(8):
  22. if i > 0:
  23. # expand pie chart radius and shrink wedge width
  24. width *= 0.85
  25. radius += width
  26. # create data
  27. data = [gap]
  28. data.extend([ngap / 2**(i+1)] * ninner)
  29. colours = np.array([[0.8, 0.8, 0.8, 1.0] for _ in range(len(data))]) # initialise as all grey
  30. wcolours = get_colours(ninner)
  31. # this part will depend on your data!
  32. # let's colour fill all the inner wedges
  33. if i < 5:
  34. colours[1:] = wcolours
  35. else:
  36. # choose some values to fill in
  37. nfill = int(np.sqrt(ninner))
  38. if nfill > 0:
  39. wfill = np.zeros(ninner)
  40. wfill[np.random.choice(np.arange(ninner), nfill, replace=False)] = 1.0
  41. cv = colours[1:]
  42. cv[wfill.astype(bool)] = wcolours[wfill.astype(bool)]
  43. ninner *= 2
  44. # create pie chart
  45. wedges, _ = ax.pie(
  46. data,
  47. radius=radius,
  48. colors=colours,
  49. wedgeprops={"width": width, "edgecolor": "w", "linewidth": 0.25},
  50. startangle=-150, # shift start angle
  51. )
  52. wedges[0].set_visible(False) # make gap invisible
  53. fig.tight_layout()
  54. fig.show()

其产生:

展开查看全部

相关问题