Matplotlib 简明教程

Matplotlib - Sankey Class

Sankey Class 在 matplotlib 中用于创建 Sankey diagrams ,在深入了解 Sankey 类之前,了解 Sankey 图的基本知识非常重要。

Sankey Class in matplotlib is used to create Sankey diagrams, Before diving into the Sankey Class, it’s essential to understand the basics of Sankey diagrams.

Sankey diagram 是一种强大的可视化工具,它表示不同实体或进程之间的资源、能量或信息的流动。它使用不同宽度的箭头来描述流动的数量,并且宽度与所表示的数量成正比。请参阅下图以了解简单的 Sankey 图的参考

A Sankey diagram is a powerful visualization tool that represents the flow of resources, energy, or information between different entities or processes. It uses arrows of varying widths to depict the quantity of flow, with the width being proportional to the quantity being represented. See the below image for the reference of a simple Sankey Diagram

sankey class intro

Key Components of a Sankey Diagram

  1. Nodes − Entities or processes between which the flow occurs.

  2. Flows − Arrows connecting nodes, representing the quantity of the flow.

  3. Labels − Descriptions associated with nodes or flows.

Sankey Class in Matplotlib

在 Matplotlib 中,Sankey() 类提供了一种创建这些图表的便捷方法。以下是该类的语法 −

In Matplotlib, the Sankey() class provides a convenient way to create these diagrams. Below is the syntax of the class −

Syntax

class matplotlib.sankey.Sankey(ax=None, scale=1.0, unit='', format='%G', gap=0.25, radius=0.1, shoulder=0.03, offset=0.15, head_angle=100, margin=0.4, tolerance=1e-06, **kwargs)

以下是使用 matplotlib Sankey 类创建 Sankey 图的步骤 −

Following are the steps to create a Sankey diagram using matplotlib Sankey Class −

  1. Creating a Sankey Object − This can be done by using the Sankey() class which initializes an instance of the Sankey class, which will be used to build the diagram.

  2. Adding the Flows and Labels − The Sankey.add() method is used to specify the flows and labels.

  3. Finish and Display − The Sankey.finish() method finalizes the diagram, and plt.show() displays it.

Drawing a Basic Sankey Diagram

现在,让我们使用默认设置构建一个简单的 Sankey 图。以下示例创建一个输入和输出流动图。流动使用 flows 参数指定,而标签使用 labels 参数提供。

Now, let’s build a simple Sankey diagram with The default settings. The below example creates a diagram of one input and output flow. The flows are specified with the flows argument, while labels are provided using labels argument.

Example 1

这里是创建具有一个输入和输出流的 Sankey 图的示例。

Here is the example that crates the Sankey diagram with one input and output flows.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
sankey = Sankey()
sankey.add(flows=[1, -1],
   labels=['input', 'output'])
plt.title("Sankey Diagram")
sankey.finish()
plt.show()

在执行上述程序时,将生成以下 Sankey 图 −

On executing the above program, will generate the following Sankey diagram −

sankey class ex1

Example 2

import matplotlib.pyplot as plt

from matplotlib.sankey import Sankey
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.")
plt.show()

在执行上述程序时,将生成以下 Sankey 图 −

On executing the above program, will generate the following Sankey diagram −

sankey class ex2

Customized Sankey Diagram

Matplotlib Sankey class 提供了更多自定义选项,包括将多个 Sankey 图连接在一起、在图表的中间放置标签、隐式将关键字参数传递给 PathPatch()、更改箭头头的角度、在创建图表后更改贴片和标签的外观等。

The Matplotlib Sankey class provides more customization options, including connecting multiple Sankey diagrams together, placing a label in the middle of the diagram, implicitly passing keyword arguments to PathPatch(), changing the angle of the arrowheads, changing the appearance of the patch and the labels after the figure is created, and more.

Example

这是一个使用 matplotlib sankey() 类创建复杂自定义 Sankey 图的示例。

Here is an example that creates a complex customized Sankey Diagram using matplotlib sankey() class.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey

# Create a subplot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Customized Sankey Diagram with Two Systems")

# Create a Sankey diagram
sankey = Sankey(ax=ax, scale=0.01, offset=0.2, format='%.0f', unit='%')

# Add flows and labels for the first system
sankey.add(flows=[25, 0, 60, -10, -20, -5, -15, -10, -40], label='one',
   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")

# Add flows and labels for the second system, connected to the first
sankey.add(flows=[-25, 15, 10], label='two',
   orientations=[-1, -1, -1], prior=0, connect=(0, 0))

# Finish the Sankey diagram and apply customizations
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
diagrams[0].texts[-1].set_color('r')
diagrams[0].text.set_fontweight('bold')

# Display the diagram
plt.legend()
plt.show()

在执行上述程序时,将生成以下 Sankey 图 −

On executing the above program, will generate the following Sankey diagram −

sankey class ex3