Matplotlib 简明教程

Matplotlib - Fill Spiral

一般来说, spiral 是发源自中心点并随着围绕该点旋转而越移越开的几何曲线。螺旋呈现出涡旋模式,并有各种形式,包括阿基米德螺旋和对数螺旋。请参阅下图以供参考-

In general definition, a spiral is a geometric curve that emanates from a central point and moves farther away as it revolves around that point. Spirals exhibit a whorled pattern and come in various forms, including Archimedean spirals, and logarithmic spirals. See the below image for reference −

fill spiral intro

另一方面, Fill Spiral 是指螺旋曲线的视觉表示,其中螺旋所包围的空间填充有颜色或图案。

On the other hand, a Fill Spiral refers to the visual representation of a spiral curve in which the space enclosed by the spiral is filled with a color or pattern.

在本教程中,我们将看到使用 Matplotlib 创建和填充螺旋的两种不同方法。过程涉及定义表示螺旋的数学方程,然后使用 pyplot.fill() 等函数对螺旋包围的区域进行着色。

In this tutorial, we’ll see two different ways of creating and filling spirals using Matplotlib. the process involves defining the mathematical equations that represent the spiral and then using a function like pyplot.fill() to color the region enclosed by the spiral.

Creating a Basic Fill Spiral

可以使用极坐标的参数方程定义一个基本的填充螺旋。然后, pyplot.fill() 函数被用来填充螺旋包围的区域。

A basic fill spiral can be defined using parametric equations in polar coordinates. The pyplot.fill() function is tthen used to fill the region enclosed by the spiral with a color.

Example

这里是一个使用 pyplot.fill()np.concatenate() 函数创建基本填充螺旋的示例。

Here is an example that creates the basic fill spiral using the pyplot.fill() and np.concatenate() functions.

import matplotlib.pyplot as plt
import numpy as np

# Define parameters
theta = np.radians(np.linspace(0,360*5,1000))
a = 1
b = 0.2

fig, axes = plt.subplots(figsize=(7, 4))

# Create a spiral
for dt in np.arange(0, 2 * np.pi, np.pi / 2.0):
   x = a * np.cos(theta + dt) * np.exp(b * theta)
   y = a * np.sin(theta + dt) * np.exp(b * theta)

   dt = dt + np.pi / 4.0

   x2 = a * np.cos(theta + dt) * np.exp(b * theta)
   y2 = a * np.sin(theta + dt) * np.exp(b * theta)

   # Concatenate points for filling
   xf = np.concatenate((x, x2[::-1]))
   yf = np.concatenate((y, y2[::-1]))

   # Fill the spiral
   plt.fill(xf, yf)

# Display the plot
plt.show()

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

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

fill spiral ex1

Creating the Logarithmic Fill Spiral

对数螺旋是一种特定的螺旋类型,其中半径随角度呈指数增长。

A logarithmic spiral is a specific type of spiral where the radius grows exponentially with the angle.

Example

该示例将对数螺旋分段构建,结合具有不同参数的线段。

The example constructs the logarithmic spiral in pieces, combining segments with different parameters.

import matplotlib.pyplot as plt
import numpy as np

# Define parameters for the logarithmic spiral
a = 2
b = 0.2

# Generate theta and radius values for different pieces
theta1 = np.linspace(0, np.pi * 3.0, 1000, endpoint=True)
r1 = np.exp(b * theta1) * a

theta2 = np.linspace(np.pi, np.pi * 4.0, 1000, endpoint=True)
r2 = np.exp(b * theta1) * a

theta3 = np.linspace(np.pi, 0, 1000)
r3 = r1[-1] * np.ones_like(theta3)

theta4 = np.linspace(np.pi, 2 * np.pi, 1000)
r4 = a * np.ones_like(theta4)

theta5 = np.linspace(np.pi, 2 * np.pi, 1000)
r5 = r1[-1] * np.ones_like(theta5)

theta6 = np.linspace(0, np.pi, 1000)
r6 = a * np.ones_like(theta6)

# Concatenate pieces for filling
theta_final_red = np.concatenate([theta1, theta3, np.flip(theta2), theta4])
radius_red = np.concatenate([r1, r3, np.flip(r2), r4])

theta_final_blue = np.concatenate([theta1, theta5, np.flip(theta2), theta6])
radius_blue = np.concatenate([r1, r5, np.flip(r2), r6])

# Plot the filled spirals
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(111, projection='polar')
ax.set_rmax(r1[-1])
ax.fill(theta_final_red, radius_red, "g")
ax.fill(theta_final_blue, radius_blue, "r")

# Plot the individual pieces
ax.plot(theta1, r1)
ax.plot(theta2, r2)

# Black inner circle
theta_inner = np.linspace(0, np.pi * 2.0, 1000, endpoint=True)
r_inner = [a] * len(theta_inner)
ax.fill(theta_inner, r_inner, c='black')

ax.axis(False)
ax.grid(False)

# Display the plot
plt.show()

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

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

fill spiral ex2