Python 图形方向字段
有没有办法在python中绘制方向字段 我的尝试是修改给予 然而,我从中得到的最好的印象是。Python 图形方向字段,python,math,Python,Math,有没有办法在python中绘制方向字段 我的尝试是修改给予 然而,我从中得到的最好的印象是。 如何获得更像第一幅图像的输出?另外,如何添加三条实线?尝试将参数值更改为: XSCL = .2 YSCL = .2 这些参数确定轴上采样的点数 根据您的评论,您还需要绘制推导dy_dx(x,y)适用的函数 目前,您仅计算和绘制由函数dy_dx(x,y)计算的坡度线。除了坡度之外,还需要找到(在本例中为3)要打印的函数 首先定义一个函数: def f1_x(x): return x**3-x*
如何获得更像第一幅图像的输出?另外,如何添加三条实线?尝试将参数值更改为:
XSCL = .2
YSCL = .2
这些参数确定轴上采样的点数
根据您的评论,您还需要绘制推导dy_dx(x,y)适用的函数 目前,您仅计算和绘制由函数dy_dx(x,y)计算的坡度线。除了坡度之外,还需要找到(在本例中为3)要打印的函数 首先定义一个函数:
def f1_x(x):
return x**3-x**2-2x;
然后,在循环中,还必须将函数所需的值写入fileobj文件。您可以使用此matplotlib代码作为基础。根据您的需要修改它。 我已经更新了代码以显示相同长度的箭头。重要的选项是设置
quiver
功能的角度
选项,以便箭头从(x,y)正确打印到(x+u,y+v)(而不是默认值,在计算角度时只考虑(u,v)
也可以将轴形式“框”更改为“箭头”。如果您需要更改,请告诉我,我可以添加更改
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
fig = plt.figure()
def vf(x, t):
dx = np.zeros(2)
dx[0] = 1.0
dx[1] = x[0] ** 2 - x[0] - 2.0
return dx
# Solution curves
t0 = 0.0
tEnd = 10.0
# Vector field
X, Y = np.meshgrid(np.linspace(-5, 5, 20), np.linspace(-10, 10, 20))
U = 1.0
V = X ** 2 - X - 2
# Normalize arrows
N = np.sqrt(U ** 2 + V ** 2)
U = U / N
V = V / N
plt.quiver(X, Y, U, V, angles="xy")
t = np.linspace(t0, tEnd, 100)
for y0 in np.linspace(-5.0, 0.0, 10):
y_initial = [y0, -10.0]
y = odeint(vf, y_initial, t)
plt.plot(y[:, 0], y[:, 1], "-")
plt.xlim([-5, 5])
plt.ylim([-10, 10])
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
我用pygame制作了一个这样的游戏作为一个业余爱好项目,玩得很开心。我在每个像素处绘制了坡度,蓝色阴影表示正,红色阴影表示负。黑色代表未定义。这是
dy/dx=log(sin(x/y)+cos(y/x))
:
您可以放大和缩小-此处中上部放大:
并单击一个点以绘制穿过该点的线:
def graphequation(self, sx, sy, stepsize=.01, color=(255, 255, 127)):
"""Graph the differential equation, given the starting point sx and sy, for length
length using stepsize stepsize."""
numstepsf = (self.xrange[1] - sx) / stepsize
numstepsb = (sx - self.xrange[0]) / stepsize
self._grapheqhelp(sx, sy, stepsize, numstepsf, color)
self._grapheqhelp(sx, sy, -stepsize, numstepsb, color)
它只有440行代码,所以。我想我会在这里摘录相关内容
等式本身作为有效的Python表达式输入到字符串中,例如,“log(sin(x/y)+cos(y/x))”
。这就是编译。此函数用于绘制颜色字段,其中self.func.eval()
给出给定点处的dy/dx
。这里的代码有点复杂,因为我让它分阶段呈现——先是32x32块,然后是16x16块,等等——以便让用户更快速
def graphcolorfield(self, sqsizes=[32,16,8,4,2,1]):
su = ScreenUpdater(50)
lastskip = self.xscreensize
quitit = False
for squaresize in sqsizes:
xsquaresize = squaresize
ysquaresize = squaresize
if squaresize == 1:
self.screen.lock()
y = 0
while y <= self.yscreensize:
x = 0
skiprow = y%lastskip == 0
while x <= self.xscreensize:
if skiprow and x%lastskip==0:
x += squaresize
continue
color = (255,255,255)
try:
m = self.func.eval(*self.ct.untranscoord(x, y))
if m >= 0:
if m < 1:
c = 255 * m
color = (0, 0, c)
else:
#c = 255 - 255 * (1.0/m)
#color = (c, c, 255)
c = 255 - 255 * (1.0/m)
color = (c/2.0, c/2.0, 255)
else:
pm = -m
if pm < 1:
c = 255 * pm
color = (c, 0, 0)
else:
c = 255 - 255 * (1.0/pm)
color = (255, c/2.0, c/2.0)
except:
color = (0, 0, 0)
if squaresize > 1:
self.screen.fill(color, (x, y, squaresize, squaresize))
else:
self.screen.set_at((x, y), color)
if su.update():
quitit = True
break
x += xsquaresize
if quitit:
break
y += ysquaresize
if squaresize == 1:
self.screen.unlock()
lastskip = squaresize
if quitit:
break
我从来没有真正画过线,因为像素法看起来太酷了。是的,是的!实际上我做了一个程序来做这个。。。当我到家时,我将尝试查找并上传它。结果得到了一些非常漂亮的照片。坡度场又称方向场。0.5似乎大致正确。在我看来,它仍然不太好(例如没有轴),但在任何情况下,你知道如何添加三条实线吗?如果可能的话,我倾向于在不使用gnuplot的情况下使用python完成整个工作。谢谢。太好了。如何获得这些彩色曲线的参数化?为什么U=1?假设我有dy_dt=-3y+t+e**(-2*t),U仍然是1吗?@RicardoAcuna程序中的U,V公式专门用于箭头,并且完全等于vf(向量场)函数的方程。所以,如果你想画一幅类似的图,比如说为$(x',y')=(f_1(x,y),f_2(x,y))$写$(U,V)=(f_1(x,y),f_2(x,y)$。注意我用的是大$(x,y)$表示网格的坐标。然后,使用$N$对箭头进行归一化处理,使所有箭头具有相同的长度。因此,需要注意的是,您得到的图片是一个归一化向量场,其中只显示方向,而不显示速度本身。希望这会有所帮助。由于
q的方式,此脚本是错误的uiver
绘制箭头的角度。默认设置为angles=“uv”,使角度仅取决于u和v。使用angles=“xy”可以获得正确的图形,使箭头从(x,y)到(x+u,y+u)。
def _grapheqhelp(self, sx, sy, stepsize, numsteps, color):
x = sx
y = sy
i = 0
pygame.draw.line(self.screen, color, (x, y), (x, y), 2)
while i < numsteps:
lastx = x
lasty = y
try:
m = self.func.eval(x, y)
except:
return
x += stepsize
y = y + m * stepsize
screenx1, screeny1 = self.ct.transcoord(lastx, lasty)
screenx2, screeny2 = self.ct.transcoord(x, y)
#print "(%f, %f)-(%f, %f)" % (screenx1, screeny1, screenx2, screeny2)
try:
pygame.draw.line(self.screen, color,
(screenx1, screeny1),
(screenx2, screeny2), 2)
except:
return
i += 1
stx, sty = self.ct.transcoord(sx, sy)
pygame.draw.circle(self.screen, color, (int(stx), int(sty)), 3, 0)
def graphequation(self, sx, sy, stepsize=.01, color=(255, 255, 127)):
"""Graph the differential equation, given the starting point sx and sy, for length
length using stepsize stepsize."""
numstepsf = (self.xrange[1] - sx) / stepsize
numstepsb = (sx - self.xrange[0]) / stepsize
self._grapheqhelp(sx, sy, stepsize, numstepsf, color)
self._grapheqhelp(sx, sy, -stepsize, numstepsb, color)