Sankey类

通过生成三个基本图来演示sankey类。

import matplotlib.pyplot as plt

from matplotlib.sankey import Sankey

示例1——主要是默认值

这演示了如何通过隐式调用sankey.add()方法和向类的调用追加finish()来创建简单的关系图。

Sankey(flows=[0.25, 0.15, 0.60, -0.20, -0.15, -0.05, -0.50, -0.10],
       labels=['', '', '', 'First', 'Second', 'Third', 'Fourth', 'Fifth'],
       orientations=[-1, 1, 0, 1, 1, 1, 0, -1]).finish()
plt.title("The default settings produce a diagram like this.")
The default settings produce a diagram like this.

出:

Text(0.5, 1.0, 'The default settings produce a diagram like this.')

注意事项:

  1. 在sankey()实例化时没有提供轴,因此它们是自动创建的。
  2. 因为数据已经被规范化,所以不需要scale参数。
  3. 默认情况下,路径的长度是合理的。

例2

这表明:

  1. 设置一条路径比其他路径长
  2. 在图表中间放置标签
  3. 使用scale参数规范化流
  4. 将关键字参数隐式传递给PathPatch()。
  5. 更改箭头的角度
  6. 更改路径尖端及其标签之间的偏移量
  7. 格式化路径标签和关联单元中的数字
  8. 创建图形后更改修补程序和标签的外观
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
                     title="Flow Diagram of a Widget")
sankey = Sankey(ax=ax, scale=0.01, offset=0.2, head_angle=180,
                format='%.0f', unit='%')
sankey.add(flows=[25, 0, 60, -10, -20, -5, -15, -10, -40],
           labels=['', '', '', 'First', 'Second', 'Third', 'Fourth',
                   'Fifth', 'Hurray!'],
           orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0],
           pathlengths=[0.25, 0.25, 0.25, 0.25, 0.25, 0.6, 0.25, 0.25,
                        0.25],
           patchlabel="Widget\nA")  # Arguments to matplotlib.patches.PathPatch
diagrams = sankey.finish()
diagrams[0].texts[-1].set_color('r')
diagrams[0].text.set_fontweight('bold')
Flow Diagram of a Widget

注意事项:

  1. 由于流的总和不为零,所以主干的宽度不均匀。Matplotlib日志记录系统在调试级别记录这一点。
  2. 第二个流不会出现,因为它的值为零。同样,这是在调试级别记录的。

例3

这表明:

  1. 连接两个系统
  2. 关闭数量标签
  3. 添加图例
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='one',
           orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0])
sankey.add(flows=[-0.25, 0.15, 0.1], label='two',
           orientations=[-1, -1, -1], prior=0, connect=(0, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend()
Two Systems

出:

<matplotlib.legend.Legend object at 0x7fa9bfe8d860>

注意,只指定了一个连接,但是系统形成了一个回路,因为:(1)路径的长度是合理的,(2)流的方向和顺序是镜像的。


工具书类

以下函数、方法、类和模块的使用如本例所示:

出:

<function Sankey.finish at 0x7faa00650488>

脚本的总运行时间: (0分1.050秒)

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery