Matplotlib 简明教程

Matplotlib - Path Effects

路径效果是指在计算机图形中操作路径(路线)的方式。“路径”是使用绘图工具创建的线或形状,“路径效果”允许您对该线或形状应用各种修改。

A path effects refer to the ways in which a path (route) can be manipulated in computer graphics. A "path" is a line or shape that you create using a drawing tool, and "path effects" allow you to apply various modifications to that line or shape.

想象一下,你画了一条简单直线,借助路径效果,你可以让这条直线看起来波浪形、点状,或者应用其他视觉改变,而无需手动重新绘制。这就像为创建的路径添加特殊效果,让你的绘画更有趣、更动态。

Imagine you draw a simple line, and with path effects, you can make that line look wavy, dotted, or apply other visual changes without having to redraw it manually. It is like adding special effects to the path you have created, making your drawings more interesting and dynamic.

path effects1

Path Effects in Matplotlib

你可以在 matplotlib 中使用“path_effects”模块,通过创建路径效果提升你绘制图形的视觉效果。首先,可以在“path_effects”模块中考虑“withStroke()”函数。这个函数允许你为线条和标记添加笔触或轮廓。

You can use the "path_effects" module in matplotlib to enhance the visual representation of your plots by creating path effects. To get started, consider the "withStroke()" function within the "path_effects" module. This function allows you to add a stroke, or outline, to lines and markers.

Matplotlib 中的路径效果允许你通过向线条和图形应用特殊视觉效果,提升它们的外观。

Path effects in Matplotlib allow you to enhance the appearance of lines and shapes in your plots by applying special visual effects to them.

Simple Line with Shadow Path Effect

在 Matplotlib 中,创建具有阴影效果的简单线条包括在绘制的基本线条上进行绘制,然后增强它,采用阴影效果,让它在视觉上更有趣。这个效果在线条下方表现为阴影,就像在绘制表面上创建微妙阴影一样。

In Matplotlib, creating a simple line with a shadow involves drawing a basic line on a plot and then enhancing it with a shadow effect to make it visually interesting. This effect gives the appearance of a shadow under the line, as if it were creating a subtle shade on the plotting surface.

Example

在以下示例中,我们绘制一条波浪线,然后使用 path_effects 模块,为线条添加阴影效果。阴影效果由“灰色”笔触组成,在线条后面创建阴影的外观。

In the following example, we are drawing a wavy line, and then adding shadow effect to the line using the path_effects module. The shadow effect consists of a "gray" stroke, creating the appearance of a shadow behind the line.

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Creating a simple line plot
fig, ax = plt.subplots()
line, = ax.plot(x, y, label='Simple Line')

# Adding a shadow effect to the line
shadow_effect = path_effects.withStroke(linewidth=5, foreground='gray')
line.set_path_effects([shadow_effect])

ax.set_title('Simple Line with Shadow Effect')
plt.legend()
plt.show()

以下是上面代码的输出: -

Following is the output of the above code −

path effects2

Dashed Line with Outline Path Effect

在 Matplotlib 中创建带轮廓路径效果的虚线包括在绘制上绘制虚线,然后通过添加粗体外框增强它。要创建一个轮廓,你可以在虚线的顶部绘制另一条线,用更粗的线宽和实线线型。

Creating a dashed line with an outline path effect in Matplotlib involves drawing a dotted line on a plot and then enhancing it by adding a bold outline. To create an outline, you can draw another line on top of the dashed line with a thicker linewidth and a solid linestyle.

Example

这里,我们创建一条虚线,并用“黑色”笔触轮廓增强它,为线条提供粗体边框,并让它脱颖而出。

In here, we are creating a dashed line and enhancing it with a "black" stroke outline to give the line a bold border and make it stand out.

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y = np.cos(x)

# Creating a dashed line plot with outline
fig, ax = plt.subplots()
line, = ax.plot(x, y, linestyle='dashed', label='Dashed Line')

# Adding an outline effect to the line
outline_effect = path_effects.withStroke(linewidth=3, foreground='black')
line.set_path_effects([outline_effect])

ax.set_title('Dashed Line with Outline Effect')
plt.legend()
plt.show()

执行上述代码,我们将得到以下输出 −

On executing the above code we will get the following output −

path effects3

Bold Outlined Scatter Plot Path Effect

在 Matplotlib 中创建带路径效果的高亮散点图包括在图表上绘制一组点,并通过添加粗轮廓增强每个点。

Creating a bold outlined scatter plot with path effects in Matplotlib involves plotting a set of points on a graph and enhancing each point by adding a strong outline.

Example

在下方的示例中,我们生成带有随机数据点的散点图。为提升这些点的可视性,我们通过在每个散点周围添加黑色笔触(带有增加的线宽)向每个点应用粗轮廓效果。

In the example below, we generate a scatter with random data points. To enhance the visibility of these points, we apply a bold outline effect to each one by adding a "black" stroke with an increased "linewidth" around each scatter point.

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import numpy as np

# Generating data
x = np.random.rand(50)
y = np.random.rand(50)

# Creating a scatter plot with bold outline effect
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, label='Scatter Plot')

# Adding a bold outline effect to the scatter points
outline_effect = path_effects.withStroke(linewidth=3, foreground='black')
scatter.set_path_effects([outline_effect])

ax.set_title('Scatter Plot with Bold Outline')
plt.legend()
plt.show()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

path effects4

Combined Path Effects

在 Matplotlib 中创建复合路径效果绘制允许我们向线条应用多重艺术增强,如线型、标记、颜色渐变和透明度。

Creating a combined path effects plot in Matplotlib allows us to apply multiple artistic enhancements to a line such as linestyle, markers, color gradients, and transparency.

  1. Linestyle − You can choose various linestyle options like solid lines ('-'), dashed lines ('--'), dotted lines (':'), and more.

  2. Markers − Markers can be added to highlight specific data points. Common markers include circles ('o'), squares ('s'), triangles ('^'), etc.

  3. Color Gradients − You can use color gradients to create visually appealing lines. You can achieve this by specifying a "colormap" and using it to color the line based on a variable.

  4. Transparency − Adding transparency to the line can make overlapping lines more distinguishable. You can adjust transparency using the "alpha" parameter.

Example

现在,我们向线条应用三个路径效果:微妙阴影效果、高亮黑色外框和轻微模糊效果。这个结果显示一条结合了这些效果的线条绘制。

Now, we are applying three path effects to the line: a subtle shadow effect, a bold black outline, and a slight blur effect. The result displays a line plot with a combination of these effects.

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Creating a line plot
fig, ax = plt.subplots()
line, = ax.plot(x, y, label='Combined Effects Line')

# Combine multiple path effects: Shadow, Bold Outline, and Blur
shadow_effect = path_effects.withStroke(linewidth=5, foreground='cyan')
outline_effect = path_effects.withStroke(linewidth=3, foreground='red')
blur_effect = path_effects.withStroke(linewidth=5, foreground='magenta', alpha=0.4)

# Applying the combined effects to the line
line.set_path_effects([shadow_effect, outline_effect, blur_effect])

ax.set_title('Combined Path Effects Line')
plt.legend()
plt.show()

执行上述代码,我们将得到以下输出 −

On executing the above code we will get the following output −

path effects5