使用Sankey的长链连接

通过建立长链连接来演示/测试Sankey类。

  1. import matplotlib.pyplot as plt
  2. from matplotlib.sankey import Sankey
  3. links_per_side = 6
  4. def side(sankey, n=1):
  5. """Generate a side chain."""
  6. prior = len(sankey.diagrams)
  7. for i in range(0, 2*n, 2):
  8. sankey.add(flows=[1, -1], orientations=[-1, -1],
  9. patchlabel=str(prior + i),
  10. prior=prior + i - 1, connect=(1, 0), alpha=0.5)
  11. sankey.add(flows=[1, -1], orientations=[1, 1],
  12. patchlabel=str(prior + i + 1),
  13. prior=prior + i, connect=(1, 0), alpha=0.5)
  14. def corner(sankey):
  15. """Generate a corner link."""
  16. prior = len(sankey.diagrams)
  17. sankey.add(flows=[1, -1], orientations=[0, 1],
  18. patchlabel=str(prior), facecolor='k',
  19. prior=prior - 1, connect=(1, 0), alpha=0.5)
  20. fig = plt.figure()
  21. ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
  22. title="Why would you want to do this?\n(But you could.)")
  23. sankey = Sankey(ax=ax, unit=None)
  24. sankey.add(flows=[1, -1], orientations=[0, 1],
  25. patchlabel="0", facecolor='k',
  26. rotation=45)
  27. side(sankey, n=links_per_side)
  28. corner(sankey)
  29. side(sankey, n=links_per_side)
  30. corner(sankey)
  31. side(sankey, n=links_per_side)
  32. corner(sankey)
  33. side(sankey, n=links_per_side)
  34. sankey.finish()
  35. # Notice:
  36. # 1. The alignment doesn't drift significantly (if at all; with 16007
  37. # subdiagrams there is still closure).
  38. # 2. The first diagram is rotated 45 deg, so all other diagrams are rotated
  39. # accordingly.
  40. plt.show()

使用Sankey的长链连接示例

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.sankey
  3. matplotlib.sankey.Sankey
  4. matplotlib.sankey.Sankey.add
  5. matplotlib.sankey.Sankey.finish

下载这个示例