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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:58:47  来源:igfitidea点击:

Plot equation showing a circle

pythonnumpymatplotlibplotequation

提问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

这产生了下图

enter image description here

在此处输入图片说明

Lastly, some general statements:

最后,一些一般性声明:

  1. x^2does not mean what you thinkit does in python, you have to use x**2.
  2. x1and x2are terribly misleading (to me), especially if you state that x2has to be on the y-axis.
  3. (Thanks to Dux) You can add plt.gca().set_aspect('equal')to make the figure actually look circular, by making the axis equal.
  1. x^2并不意味着你认为它在 python 中的作用,你必须使用x**2.
  2. x1并且x2非常具有误导性(对我而言),特别是如果您声明x2必须在 y 轴上。
  3. (感谢 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

产生

enter image description here

在此处输入图片说明

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.6it 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:

结果:

enter image description here

在此处输入图片说明

回答by chen_767

# x**2  + y**2 = r**2
r = 6
x = np.linspace(-r,r,1000)
y = np.sqrt(-x**2+r**2)
plt.plot(x, y,'b')
plt.plot(x,-y,'b')
plt.gca().set_aspect('equal')
plt.show()

produces:

产生:

enter image description here

在此处输入图片说明