如何在matplotlib等高线图中添加点?

2024-05-16 01:53:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在二维相空间中绘制一个常微分方程组的相图,并用matplotlib的streamplot函数来实现。我想在图中标记一些点(固定点),但我不知道如何在同一个图上做,因为使用scatter只在另一个图上绘制点。我该怎么做?以下是我目前的情况:

import numpy as np
import matplotlib.pyplot as plt

"""
The system of ODE is given by:
dx/dt = alpha*(1-x)*x - c*x*y
dy/dt = -b*y + d*x*y
"""

# Set the parameters
alpha, b, c, d = 4, 3, 2, 1

# The fixed points
fp = [(0, 0), (1,0), (b/d, alpha/c*(1-b/d))]

# The phase portrait lies in {(x,y)|-l<x<l, -l<y<l} with n arrows in each dimension
l, n = 2, 41

# The 'velocity' of a point in phase space = (dx/dt, dy/dt)
def velocity(x, y):
    return alpha*(1-x)*x - c*x*y, -b*y + d*x*y

# Mesh the grids in x-y space
x, y = np.linspace(-l, l, n), np.linspace(-1, 1, n)
X, Y = np.meshgrid(x, y)
U, V = velocity(X, Y)
plt.figure()
plt.streamplot(X, Y, U, V)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()

Tags: oftheinimportalphamatplotlibasnp