Matplotlib轮廓不起作用

时间:2011-07-29 09:53:49

标签: python matplotlib sympy

我正在尝试绘制蝙蝠侠等式。在sympy或matplotlib的解决方案将是伟大的(圣人不酷,因为我正在使用Windows)。问题在于,如果我注释掉某些部分,那么图中的部分会显示,但所有F *=部分都会出现,我会得到一个空白的图。

import matplotlib.pyplot
from numpy import arange
from numpy import meshgrid
from numpy import sqrt
from numpy import real

delta = 0.01
xrange = arange(-7.0, 7.0, delta)
yrange = arange(-3.0, 3.0, delta)
x, y = meshgrid(xrange,yrange)

F = 1
F *= (((x/7) ** 2) * sqrt(abs(abs(x) - 3)/(abs(x) - 3)) + ((y / 3) ** 2) * sqrt(abs(y + (3 * sqrt(33)) / 7)/(y + (3 * sqrt(33)) / 7)) - 1)
F *= (abs(x/2) - ((3 * sqrt(33) - 7)/112) * x**2 - 3 + sqrt(1 - (abs(abs(x) - 2) - 1) ** 2 ) - y)
F *= (9 * sqrt(abs((abs(x) - 1) * (abs(x) - 3/4))/((1 - abs(x)) * (abs(x) - 3/4))) - 8 * abs(x) - y)
F *= (3 * abs(x) + 0.75 * sqrt(abs((abs(x) - 3/4) * (abs(x) - 1/2))/((3/4 - abs(x)) * (abs(x) - 1/2))) - y)
F *= ((9/4) * sqrt(abs((x - 1/2) * (x + 1/2))/((1/2 - x) * (1/2 + x))) - y)
F *= ((6 * sqrt(10)) / 7 + (3/2 - abs(x)/2) * sqrt(abs(abs(x) - 1)/(abs(x) - 1)) - ((6 * sqrt(10))/ 14) * sqrt(4 - (abs(x) - 1) ** 2 ) - y)
G = 0

matplotlib.pyplot.contour(x, y, (F - G), [0])
matplotlib.pyplot.show()

这里发生了什么?如果一个被乘数的图形为零,那么无论我扔哪个其他被乘数,它都应该如此。

蝙蝠侠等式的来源:http://www.reddit.com/r/pics/comments/j2qjc/do_you_like_batman_do_you_like_math_my_math/

3 个答案:

答案 0 :(得分:7)

sqrt的参数对于很多点是负的,所以最终的产品都是NaN。您可以将每个因素绘制如下:

from __future__ import division  # this is important, otherwise 1/2 will be 0
import matplotlib.pyplot
from numpy import arange
from numpy import meshgrid
from numpy import sqrt
from numpy import real


delta = 0.01
xrange = arange(-7.0, 7.0, delta)
yrange = arange(-3.0, 3.0, delta)
x, y = meshgrid(xrange,yrange)

F1 = (((x/7) ** 2) * sqrt(abs(abs(x) - 3)/(abs(x) - 3)) + ((y / 3) ** 2) * sqrt(abs(y + (3 * sqrt(33)) / 7)/(y + (3 * sqrt(33)) / 7)) - 1)
F2 = (abs(x/2) - ((3 * sqrt(33) - 7)/112) * x**2 - 3 + sqrt(1 - (abs(abs(x) - 2) - 1) ** 2 ) - y)
F3 = (9 * sqrt(abs((abs(x) - 1) * (abs(x) - 3/4))/((1 - abs(x)) * (abs(x) - 3/4))) - 8 * abs(x) - y)
F4 = (3 * abs(x) + 0.75 * sqrt(abs((abs(x) - 3/4) * (abs(x) - 1/2))/((3/4 - abs(x)) * (abs(x) - 1/2))) - y)
F5 = ((9/4) * sqrt(abs((x - 1/2) * (x + 1/2))/((1/2 - x) * (1/2 + x))) - y)
F6 = ((6 * sqrt(10)) / 7 + (3/2 - abs(x)/2) * sqrt(abs(abs(x) - 1)/(abs(x) - 1)) - ((6 * sqrt(10))/ 14) * sqrt(4 - (abs(x) - 1) ** 2 ) - y)


for f in [F1,F2,F3,F4,F5,F6]:
    matplotlib.pyplot.contour(x, y, f, [0])
matplotlib.pyplot.show()

结果图: enter image description here

答案 1 :(得分:0)

我知道这可能看起来很蹩脚,但如何创建一个x值列表,然后在每个位置计算“batman”的值,并存储在另一个列表中。您可以定义一个函数“batman”来计算传入的每个x值的y值。

然后用matplotlib绘制这些列表。

编辑:由于您已经将numpy数组存储到结果中,因此您可以在计算y值时使用它们。

答案 2 :(得分:0)

我甚至不确定这个等式是如何工作的,因为我看到在第一项中出现的除零(在第一个平方根下,当abs(x)= 3时),并且虚数出现在最后一个术语(在最后的平方根下,当{abs(x)-1} ^ 2> 4时,即x> 3或x <-3)。
我在这里错过了什么?只使用结果的真实部分,并将零除以零或近似?

运行这个,我确实看到了很多RunTimeWarnings,并且matplotlib不太可能完全混淆使用哪些数字(NaNs,Infs;最后尝试打印F)。当只有相对较少的NaN或Infs时,它看起来仍然可以管理,这可以解释你是否看到了这个数字的一​​部分。
我认为matplotlib的轮廓很好,只是输入混淆了。