SyntaxError:无效语法Ubuntu 18.04

问题描述 投票:-1回答:1

我最近开始使用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上运行它时,我收到第18行的SyntaxError:invalid语法

rain_drops['position'] = np.random.uniform(0, 1, (n_drops, 2))

但是当我用Pycharm运行它时,它会编译并正确运行。是否有一些特殊的方式需要在Ubuntu上运行python文件?我尝试使用shebang线,但这也不起作用。

pycharm windows-subsystem-for-linux
1个回答
0
投票

当我复制该代码时,我没有得到语法错误,所以我假设一个窗口行结束在那里偷偷摸摸。你可以试着用dos2unix纠正这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.