这个简单的代码在第 6 行的项目“`('”上产生了一个语法错误 当从命令行以 # ./main.py 运行时
import matplotlib.pyplot as plt
import numpy as np
# create a top-level Figure object
# with some optional customizations
fig = plt.figure('Population density',figsize=(5./2.254,5./2.254),facecolor='red',edgecolor='green')
# creating an Axes object containing axex and the rest
# we plot on the ax object of class Axes
ax = fig.add_subplot(111)
# we plot data with ax.plot
x = np.linspace(-2,2,1000)
line_cosh, = ax.plot(x, np.cosh(x))
line_quad, = ax.plot(x, 1+x**2/2)
plt.show()
除了在 ipython 中使用 %run 运行时,该图出现
通过 %run 运行 ipython
问题:语法错误在哪里?
最佳答案
添加
!/usr/bin/env python
在 linux 中从命令行运行的 python 脚本的顶部
关于Linux, python 3 : syntax error on commandline but not in ipython %run command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51001837/