首先计算每个值之间的梯度
import numpy as np
import matplotlib.pyplot as plt
rand = np.random.randint(low=1, high=10, size=(100,))
gradient_full = np.gradient(rand)
计算每10个值之间的梯度
rand_10th = rand[::10]
gradient_10th = np.gradient(rand_10th)
创建绘图
x_full = np.arange(100)
x_10th = np.arange(0, 100, 10)
情节
plt.figure(figsize=(12, 6))
plt.plot(x_full, gradient_full, label='Gradient (every value)', alpha=0.7)
plt.plot(x_10th, gradient_10th, 'ro-', label='Gradient (every 10th value)', markersize=8)
plt.xlabel('Index')
plt.ylabel('Gradient')
plt.title('Comparison of Gradients')
plt.legend()
plt.grid(True)
plt.show()
输出