Python 绘制显示圆的等式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32092899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot equation showing a circle
提问by Elyakim
The following formula is used to classify points from a 2-dimensional space:
以下公式用于对二维空间中的点进行分类:
f(x1,x2) = np.sign(x1^2+x2^2-.6)
All points are in space X = [-1,1] x [-1,1]
with a uniform probability of picking each x.
所有点都在空间中X = [-1,1] x [-1,1]
,以均匀的概率选择每个 x。
Now I would like to visualize the circle that equals:
现在我想形象化等于的圆圈:
0 = x1^2+x2^2-.6
The values of x1 should be on the x-axis and values of x2 on the y-axis.
x1 的值应该在 x 轴上,x2 的值应该在 y 轴上。
It must be possible but I have difficulty transforming the equation to a plot.
这一定是可能的,但我很难将方程转换为绘图。
采纳答案by Bas Jansen
You can use a contour plot, as follows (based on the examples at http://matplotlib.org/examples/pylab_examples/contour_demo.html):
您可以使用等高线图,如下所示(基于http://matplotlib.org/examples/pylab_examples/contour_demo.html上的示例):
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1.0, 1.0, 100)
y = np.linspace(-1.0, 1.0, 100)
X, Y = np.meshgrid(x,y)
F = X**2 + Y**2 - 0.6
plt.contour(X,Y,F,[0])
plt.show()
This yields the following graph
这产生了下图
Lastly, some general statements:
最后,一些一般性声明:
x^2
does not mean what you thinkit does in python, you have to usex**2
.x1
andx2
are terribly misleading (to me), especially if you state thatx2
has to be on the y-axis.- (Thanks to Dux) You can add
plt.gca().set_aspect('equal')
to make the figure actually look circular, by making the axis equal.
x^2
并不意味着你认为它在 python 中的作用,你必须使用x**2
.x1
并且x2
非常具有误导性(对我而言),特别是如果您声明x2
必须在 y 轴上。- (感谢 Dux)您可以
plt.gca().set_aspect('equal')
通过使轴相等来添加使图形实际上看起来是圆形的。
回答by Dux
How about drawing x-values and calculating the corresponding y-values?
如何绘制 x 值并计算相应的 y 值?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100, endpoint=True)
y = np.sqrt(-x**2. + 0.6)
plt.plot(x, y)
plt.plot(x, -y)
produces
产生
This can obviously be made much nicer, but this is only for demonstration...
这显然可以做得更好,但这仅用于演示......
回答by hitzg
The solution of @BasJansen certainly gets you there, it's either very inefficient (if you use many grid points) or inaccurate (if you use only few grid points).
@BasJansen 的解决方案肯定会让你到达那里,它要么非常低效(如果你使用很多网格点)要么不准确(如果你只使用很少的网格点)。
You can easily draw the circle directly. Given 0 = x1**2 + x**2 - 0.6
it follows that x2 = sqrt(0.6 - x1**2)
(as Dux stated).
您可以轻松地直接绘制圆圈。鉴于0 = x1**2 + x**2 - 0.6
它如下x2 = sqrt(0.6 - x1**2)
(如 Dux 所述)。
But what you really want to do is to transform your cartesian coordinates to polar ones.
但是您真正想做的是将笛卡尔坐标转换为极坐标。
x1 = r*cos(theta)
x2 = r*sin(theta)
if you use these substitions in the circle equation you will see that r=sqrt(0.6)
.
如果你在圆方程中使用这些替换,你会看到r=sqrt(0.6)
。
So now you can use that for your plot:
所以现在你可以将它用于你的情节:
import numpy as np
import matplotlib.pyplot as plt
# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)
# the radius of the circle
r = np.sqrt(0.6)
# compute x1 and x2
x1 = r*np.cos(theta)
x2 = r*np.sin(theta)
# create the figure
fig, ax = plt.subplots(1)
ax.plot(x1, x2)
ax.set_aspect(1)
plt.show()
Result:
结果: