# 科学项目教程

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/idea/llms.txt).

> **Warning:**
> 以下内容仅在   Python   插件 [已安装并启用](managing-plugins.html) 时有效。

在本教程中，您将创建一个项目来运行和调试带有数据可视化的 Python 代码。

在开始之前，请确保已安装 [conda](https://www.anaconda.com/download/)。

## 添加示例代码

创建一个空的 [Python 项目](creating-empty-python-project.html)。 通过右键点击项目根目录并选择  `新建 | Python 文件` ，添加一个新 Python 文件，命名为  `main.py` 。 然后将以下代码添加到  `main.py` 文件中：

```PYTHON
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")

plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$'])

plt.show()



```

处理针对  `numpy`  和  `matplotlib`  的导入显示的警告并启用项目中的包。

## 运行

运行您的项目。 代码执行后会在   图表  工具窗口中显示两个图表。 点击预览缩略图会显示相应的图表：

![预览图表](https://resources.jetbrains.com/help/img/idea/2026.3/py_matplotlib_run.png)

要一次仅绘制一个图表，请通过将项目代码划分为  代码单元 来修改它。 这使您可以独立执行代码的特定部分。

Procedure: 执行代码单元

1. 通过添加 "#%%" 行来修改  `main.py`  文件。

```PYTHON
import numpy as np
import matplotlib.pyplot as plt
#%% generate random values
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii
#%% build a scatter plot
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
#%% plot y versus x as lines
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")

plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$'])

plt.show()

```

![向 Python 代码添加单元](https://resources.jetbrains.com/help/img/idea/2026.3/py_cell_execution.png)

2. 在装订区域中，点击包含导入和变量的行上的图标 ![运行](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)，以便所有名称都能在后续步骤中被识别。

3. 在带有散点图单元标记的行上点击图标 ![运行](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)。 仅构建散点图。

4. 现在点击带有 y 对 x 绘图单元格标记的行上的图标 ![运行](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)。 对应的图形应出现。

> **Note:**
> ![运行](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)  `运行`  按钮可用于在不移动文本光标的情况下执行单元。
>
>
>
> 按  `Shift + Enter`  运行一个单元，并将文本光标移动到下一个单元。

## 调试

让我们在这一行设置一个断点：

```PYTHON
plt.show()

```

此行在我们的示例代码中出现了两次，因此会有两个断点。

右键点击编辑器背景，从上下文菜单中选择   调试 <project_name>  。

您可以看到 [Debug tool window](debug-tool-window.html) 和编辑器中的灰色字符。 这是 [内联调试](examining-suspended-program.html#inline-view)的结果，该功能已启用。

第一个断点所在的行会高亮显示。 这意味着调试器已在带有断点的行停止，但尚未执行它。 如果我们执行此行（例如，通过点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.stepOver.svg)   步过  工具窗口的 [单步执行工具栏](stepping-through-the-program.html)   调试  ），我们将看到图表：

![调试 matplotlib 代码](https://resources.jetbrains.com/help/img/idea/2026.3/py_matplotlib_debug.png)

接下来，请查看 Debug 工具窗口的   变量  选项卡。 如果您点击  `area` 数组旁边的   数组视图  链接，   数据视图  工具窗口将会打开：

![查看为数组](https://resources.jetbrains.com/help/img/idea/2026.3/py_matplotlib_view_as_array.png)

注意表格中唯一的列——这是因为  `area` 数组是一维的。

## 在控制台中运行

右键点击编辑器背景，然后选择   在 Python 控制台中运行文件  命令。

此命令对应于为  `main.py`  文件运行一个运行/调试配置，并选择   使用 Python 控制台运行   选项：

![使用 Python 控制台运行](https://resources.jetbrains.com/help/img/idea/2026.3/ij_run_config_for_command_line.png)

当此命令运行后，  `>>>`  提示会在运行工具窗口的输出后出现，您可以执行自己的命令。

## 总结

那么，借助 IntelliJ IDEA 完成了什么？

* 该文件 `main.py` 已创建并打开供编辑。

* 源代码已输入

* 源代码已运行并调试。

* 最后，我们在控制台中运行了该文件。

## See also

### 外部链接

[Matplotlib](http://matplotlib.org/) [Numpy](http://www.numpy.org/)

### 过程

[运行应用程序](running-applications.html) [调试代码](debugging-code.html)

