pycharm - SyntaxError:无效的语法 Ubuntu 18.04
问题描述
我最近开始使用 Linux(Ubuntu 18.04),我正在尝试运行一个基本的雨滴动画来尝试学习如何创建动画。我在 MatPlotLib 网站上找到了这个教程代码,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
np.random.seed(19680801)
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.set_xlim(0, 1), ax.set_xticks([])
ax.set_ylim(0, 1), ax.set_yticks([])
n_drops = 50
rain_drops = np.zeros(n_drops, dtype=[('position', float, 2),
('size', float, 1),
('growth', float, 1),
('color', float, 4)])
rain_drops['position'] = np.random.uniform(0, 1, (n_drops, 2))
rain_drops['growth'] = np.random.uniform(50, 200, n_drops)
scat = ax.scatter(rain_drops['position'][:, 0], rain_drops['position'][:, 1],s=rain_drops['size'], lw=0.5,edgecolors=rain_drops['color'],
facecolors='none')
def update(frame_number):
current_index = frame_number % n_drops
rain_drops['color'][:, 3] -= 1.0/len(rain_drops)
rain_drops['color'][:, 3] = np.clip(rain_drops['color'][:, 3], 0, 1)
rain_drops['size'] += rain_drops['growth']
rain_drops['position'][current_index] = np.random.uniform(0, 1, 2)
rain_drops['size'][current_index] = 5
rain_drops['color'][current_index] = (0, 0, 0, 1)
rain_drops['growth'][current_index] = np.random.uniform(50, 200)
scat.set_edgecolors(rain_drops['color'])
scat.set_sizes(rain_drops['size'])
scat.set_offsets(rain_drops['position'])
animation = FuncAnimation(fig, update, interval=10)
plt.show()
当我尝试在 Ubuntu 上运行它时,我收到 SyntaxError: invalid syntax for line 18
rain_drops['position'] = np.random.uniform(0, 1, (n_drops, 2))
但是当我用 Pycharm 运行它时,它会编译并正确运行。是否有一些特殊的方式需要在 Ubuntu 上运行 python 文件?我尝试使用 shebang 线,但这也不起作用。
解决方案
when i copy that code, i don't get a syntax error, so i assume that a windows line ending snuck in there. you can try to correct that with dos2unix.
推荐阅读
- python - 如何加入过滤的重复文件列表及其相应的路径?
- html - 单击 Img 标记时出现 VBA HTML 脚本错误
- django - TypeError: __init__() 得到了一个意外的关键字参数“必需”
- laravel - laravel 7中带有forelse循环的未定义变量
- sql - 通过 ID 连接时如何避免在字段层次结构中重复行值
- eclipse - java.lang.NoClassDefFoundError:无法初始化类 org.springframework.mail.javamail.SmartMimeMessage
- python.net - Python.Net 在 Linux 上使用 .NET Core 运行时而不是 Mono
- svg - 有没有办法使用 SVG 作为 ImageData 创建 Stamp 注释
- highcharts - 在柱形图中使用最近邻搜索悬停
- azure-active-directory - MSal.js --> ClientAuthError: no_tokens_found: 未找到给定范围的令牌