首页 > 解决方案 > linux,python3:命令行上的语法错误,但在 ipython %run 命令中没有

问题描述

这个简单的代码在命令行中以 # ./main.py 运行时会在第 6 行项目“`('”上生成语法错误


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()

除非使用 %run 在 ipython 中运行,否则会出现绘图


ipython 通过 %run

问题:语法错误在哪里?

标签: linuxpython-3.xmatplotlib

解决方案


添加

!/usr/bin/env 蟒蛇

在 linux 中从命令行运行的 python 脚本的顶部


推荐阅读