# Scientific project tutorial

> **Warning:**
> The following is only valid when the Python plugin is [installed and enabled](managing-plugins.html).

In this tutorial, you will create a project to run and debug Python code with data visualization.

Before you start, make sure that [conda](https://www.anaconda.com/download/) is installed.

## Add sample code

Create an empty [Python project](creating-empty-python-project.html). Add a new Python file named `main.py` by right-clicking the project root and selecting `New | Python File`. Then add the following code to the `main.py` file:

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


```

Process warnings shown for the `numpy` and `matplotlib` imports and enable the packages in the project.

## Run

Run your project. The code is executed and shows two graphs in the Plots  tool window. Clicking the preview thumbnail displays the respective graph:

![Preview the graph](https://resources.jetbrains.com.cn/help/img/idea/2026.2/py_matplotlib_run.png)

To plot only one graph at a time, modify the project code by dividing it into code cells. This allows you to execute specific sections of your code independently.

Procedure: Execute code cells

1. Modify the `main.py` file by adding the "#%%" lines.

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

![Adding cells to the Python code](https://resources.jetbrains.com.cn/help/img/idea/2026.2/py_cell_execution.png)

2. In the gutter, click the icon ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) on the lines with imports and variables so that all the names would be recognized for the following steps.

3. Click the icon ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) on the line with the scatter plot cell mark. Only the scatter graph will be built.

4. Now click the icon ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) on the line with the y versus x plot cell mark. The corresponding graph should appear.

> **Note:**
> The ![Run](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) `Run` button allows you to execute a cell without moving the caret.
>
>
>
> Press `Shift + Enter` to run a cell and move the caret to the next one.

## Debug

Let's put a breakpoint at the line:

```PYTHON
plt.show()
```

This line appears twice in our example code, and so there will be two breakpoints.

Right-click the editor background and from the context menu choose Debug <project_name>.

You see the [Debug tool window](debug-tool-window.html) and the grey characters in the editor. This is the result of the  [inline debugging](examining-suspended-program.html#inline-view) , which is enabled.

The line with the first breakpoint is highlighted. It means that the debugger has stopped at the line with the breakpoint but has not yet executed it. If we execute this line (for example, by clicking the ![](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.stepOver.svg) Step Over on the  [stepping toolbar](stepping-through-the-program.html) of the Debug tool window), we'll see the graph:

![Debugging the matplotlib code](https://resources.jetbrains.com.cn/help/img/idea/2026.2/py_matplotlib_debug.png)

Next, look at the Variables tab of the Debug tool window. If you click the View as Array link next to the `area` array, the Data View tool window opens:

![View as array](https://resources.jetbrains.com.cn/help/img/idea/2026.2/py_matplotlib_view_as_array.png)

Mind the only column in the table — it is explained by the fact that the `area` array is one-dimensional.

## Run in console

Right-click the editor background and choose the Run File in Python Console command.

This command corresponds to running a run/debug configuration for the `main.py` file with the Run with Python console option selected:

![Run with Python console](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ij_run_config_for_command_line.png)

When this command is run, the `>>>` prompt appears after the output in the Run tool window, and you can execute your own commands.

## Summary

So, what has been done with the help of IntelliJ IDEA?

* The file `main.py` was created and opened for editing.

* The source code has been entered

* The source code has been run and debugged.

* Finally, we ran the file in console.

## See also

### External Links

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

### Procedures

[Run applications](running-applications.html) [Debug code](debugging-code.html)

