在 Python 中使用 Matplotlib 绘制抛物线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30553585/
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
Graphing a Parabola using Matplotlib in Python
提问by kman29
I am trying to graph a simple parabola in matplotlib
and I am confused as to how I am supposed to plot points on the parabola. So far, this is what I have:
我正在尝试绘制一个简单的抛物线matplotlib
,但我对如何在抛物线上绘制点感到困惑。到目前为止,这就是我所拥有的:
import matplotlib.pyplot as plt
a=[]
b=[]
y=0
x=-50
while x in range(-50,50,1):
y=x^2+2*x+2
a=[x]
b=[y]
fig= plt.figure()
axes=fig.add_subplot(111)
axes.plot(a,b)
plt.show()
x= x+1
回答by cel
This should do:
这应该做:
import matplotlib.pyplot as plt
import numpy as np
# create 1000 equally spaced points between -10 and 10
x = np.linspace(-10, 10, 1000)
# calculate the y value for each element of the x vector
y = x**2 + 2*x + 2
fig, ax = plt.subplots()
ax.plot(x, y)
回答by tom10
This is your approach with as few changes as possible to make it work (because it's clear that you're a beginner and this is a learning exercise). The changes I made were:
这是您的方法,只需进行尽可能少的更改即可使其发挥作用(因为很明显您是初学者,这是一项学习练习)。我所做的改变是:
Moved the
plt.figure
, and other plotting statements out of the loop. The loop now gives you the data to plot, and then you plot it once the loop is finished.Changed
x^2
tox**2
.Changed
while
tofor
in your main loop control statement.Commented out a few lines that weren't doing anything. They all had the same source of error (or non-utility, really): in the for loop,
x
is set in the loop control line and theny
is calculated directly, so you don't need to give them initial values or incrementx
, though you would have had to do these steps for a while loop.
将
plt.figure
, 和其他绘图语句移出循环。循环现在为您提供要绘制的数据,然后在循环完成后绘制它。改
x^2
到x**2
。在主循环控制语句中更改
while
为for
。注释掉几行没有做任何事情。它们都有相同的错误来源(或非实用程序,真的):在 for 循环中,
x
在循环控制行中设置,然后y
直接计算,因此您不需要给它们初始值或 incrementx
,但是您将不得不在一段时间循环中执行这些步骤。
Here the code:
这里的代码:
import matplotlib.pyplot as plt
a=[]
b=[]
# y=0
# x=-50
for x in range(-50,50,1):
y=x**2+2*x+2
a.append(x)
b.append(y)
#x= x+1
fig= plt.figure()
axes=fig.add_subplot(111)
axes.plot(a,b)
plt.show()
回答by heykay47
Adjust your third to last line to:
将倒数第三行调整为:
axes.plot(a,b, 'r-^')
Adding the 'r-^
' will add red triangular points to the graph. Alternatively, you can use 'b-o
'.
添加“ r-^
”将向图形添加红色三角形点。或者,您可以使用“ b-o
”。
Note:You should include the quotes.
注意:您应该包括引号。
For different colors you can use 'b' - blue; 'g' - green; 'r' - red; 'c' - cyan; 'm' - magenta; 'y' - yellow; 'k' - black; 'w' - white
对于不同的颜色,您可以使用 'b' - 蓝色;'g' - 绿色;'r' - 红色;'c' - 青色;'m' - 洋红色;'y' - 黄色;'k' - 黑色;'w' - 白色
The - in the 'r-^
' or 'b-o
' will create the line joining the triangles or circles respectively. That is, without the dash, you will end up with a scatter plot.
' r-^
' 或 ' b-o
' 中的 - 将分别创建连接三角形或圆形的线。也就是说,如果没有破折号,您将得到一个散点图。
Alternatively, there is command ....scatter(x,y)
which would be equivalent to 'r ^
' and 'b o
'
或者,有命令 ....scatter(x,y)
相当于 ' r ^
' 和 ' b o
'