经常有人问起,如何制作出这些动画视频,制作难不难的问题?比如下面这样的物理和数学方面的直观演示,如果自己能够制作出来,那么对于学生了解概念和定义就变得简单易懂了。
import numpy as np# 原始数据data = [1, 2, 3]# 创建行向量 (1×3)row_vec = np.array(data)print(row_vec)# 创建列向量 (3×1)col_vec = np.array(data).reshape(-1, 1)#col_vec = row_vec.reshape(3,1)#col_vec = row_vec[:, np.newaxis]print(col_vec)
[1 2 3][[1][2][3]]

鞍形向量场 F = (y, x)

import numpy as npimport matplotlib.pyplot as plt# 创建网格点x = np.linspace(-2, 2, 20)y = np.linspace(-2, 2, 20)X, Y = np.meshgrid(x, y)U = YV = X# 创建图形plt.figure(figsize=(8, 6))plt.quiver(X, Y, U, V, scale=25, pivot='middle', color='red', alpha=0.7)plt.title('Vector Field: $\mathbf{F} = (y, x)$')plt.xlabel('x')plt.ylabel('y')plt.grid(True, linestyle='--', alpha=0.7)plt.axhline(y=0, color='k', linewidth=0.5)plt.axvline(x=0, color='k', linewidth=0.5)plt.xlim(-2.5, 2.5)plt.ylim(-2.5, 2.5)plt.show()
print(x)[-2. -0.66666667 0.66666667 2. ]x:之前创建的一维数组 [-2. -0.66666667 0.66666667 2.]
y:同样的一维数组 [-2. -0.66666667 0.66666667 2.]
输出结果:
X:一个4×4的二维数组,每一行都是x数组的重复
Y:一个4×4的二维数组,每一列都是y数组的重复
print(X.shape,X)(4, 4) [[-2. -0.66666667 0.66666667 2. ][-2. -0.66666667 0.66666667 2. ][-2. -0.66666667 0.66666667 2. ][-2. -0.66666667 0.66666667 2. ]]
quiver函数的前四个参数分别控制箭头的起点和方向分量:
X, Y:箭头的起点坐标网格
U, V:箭头的向量分量(x方向和y方向的位移)
具体来说: 每个箭头的起点是 (X[i,j], Y[i,j])
每个箭头的方向由 (U[i,j], V[i,j])决定
每个箭头的终点是 (X[i,j] + U[i,j], Y[i,j] + V[i,j])
empty_list = []# 包含不同类型元素的列表mixed_list = [1, "Hello", 3.14, True]# 使用 list() 构造函数numbers = list(range(5)) # [0, 1, 2, 3, 4]print(f"空列表: {empty_list}")print(f"混合列表: {mixed_list}")print(f"数字列表: {numbers}")
空列表: []混合列表: [1, 'Hello', 3.14, True]数字列表: [0, 1, 2, 3, 4]
fruits = ["apple", "banana", "cherry", "date", "elderberry"]print(f"\n2. 完整列表: {fruits}")print(f"第一个元素: {fruits[0]}")print(f"第二个元素: {fruits[1]}")print(f"最后一个元素: {fruits[-1]}")print(f"倒数第二个元素: {fruits[-2]}")
第一个元素: apple第二个元素: banana最后一个元素: elderberry倒数第二个元素: date
print(f"前三个: {fruits[:3]}")print(f"索引1到3: {fruits[1:4]}")print(f"最后两个: {fruits[-2:]}")print(f"每隔一个取一个: {fruits[::2]}")
前三个: ['apple', 'banana', 'cherry']索引1到3: ['banana', 'cherry', 'date']最后两个: ['date', 'elderberry']每隔一个取一个: ['apple', 'cherry', 'elderberry']
fruits.append("fig")fruits.insert(2, "cantaloupe")removed = fruits.pop() # 移除最后一个fruits.remove("cantaloupe")index = fruits.index("cherry")print(f" index('cherry'): {index}")#index('cherry'): 2
fruits = ["apple", "banana", "cherry", "date", "elderberry"]print(fruits)arr_fruits = np.array(fruits)print(arr_fruits)
['apple', 'banana', 'cherry', 'date', 'elderberry']['apple''banana''cherry''date''elderberry']
a = np.arange(0, 3, 0.2)print(a)
[0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. 2.2 2.4 2.6 2.8]import numpy as np# 二维行向量row_vector = np.array([[1,2,3,4,5,6,7,8,9,10]])# 索引print(row_vector[0, 1])print(row_vector[0, -1])# 切片print(row_vector[0, 1:])print(row_vector[0, :2])print(row_vector[0, :-1])
210[ 2 3 4 5 6 7 8 9 10][1 2][1 2 3 4 5 6 7 8 9]
col_vector = row_vector.Tprint(col_vector)# 索引print(col_vector[1, 0])print(col_vector[-1, 0])# 切片print(col_vector[1:, 0])print(col_vector[:2, 0])print(col_vector[:-1, 0])
[[ 1][ 2][ 3][ 4][ 5][ 6][ 7][ 8][ 9][10]]210[ 2 3 4 5 6 7 8 9 10][1 2][1 2 3 4 5 6 7 8 9]