2016-07-27 202 views
1

我想在python中使用ginput函数从图中获取两个点并获取最接近的int值并保存该数组。的代码如下所示:关于使用ginput和python打印结果的问题

from __future__ import print_function 
from pylab import plot, ginput, show, axis 

import matplotlib.pyplot as plt 
import numpy as np 
t = np.arange(10) 
plt.plot(t, np.sin(t)) 
print("Please click") 
x = ginput(2) 
x=np.ceil(x) 
**print (x)** 
plt.show() 

当我运行的代码,并用括号打印X,输出为:

Please click 
[[ 2. 1.] 
[ 8. -0.]] 

但是,如果我运行代码和打印x没有括号,

from __future__ import print_function 
from pylab import plot, ginput, show, axis 

import matplotlib.pyplot as plt 
import numpy as np 
t = np.arange(10) 
plt.plot(t, np.sin(t)) 
print("Please click") 
x = ginput(2) 
x=np.ceil(x) 
**print x** 
plt.show() 

输出显示错误:

File "<ipython-input-27-1d992b01e790>", line 1 
    print x 
     ^
SyntaxError: invalid syntax 

我对此很困惑。这是什么原因?

回答

1

在Python 3中,print()是一个函数,因此它需要在其输入参数周围使用括号,就像任何其他函数一样。