Note the use of matplotlib drawing in jupyter notebook in

When using jupyter notebook matplotlib library is often used to implement interactive graphics can support data viewing, data location determination, moving images and other functions

% Matplotlib notebook (recommended)

Interactive charts can be formed in a row at the beginning of jupyter notebook add the following code

%matplotlib notebook

Here Insert Picture Description

matplotlib

At the beginning of the following code plus interactive charts can be formed in the mac, i.e., the image window will pop

%matplotlib

%matplotlib inline

Add the following code at the beginning of the display image, without interactive features

%matplotlib inline

If at the beginning without the above mentioned code will form a dry run of pictures, interactive features can not be achieved

A detail: after each operation cell, the chart is reset

Note that there is a detail, in the operation of each cell, the chart is reset, so for complex icons, you have all the names in a single drawing notebook cells (using jupyter notebook %matplotlib notebookand import matplotlib.pyplot as pltthe beginning as long as the most plus one is enough)

Here Insert Picture Description
Here Insert Picture Description

Note: If you want to export to markdown jupyter notebook format, then you should begin with that sentence changed %matplotlib inline, this will be generated by the export picture (automatically placed in the same folder)
Here Insert Picture Description


VScode with plt.show ()

Use% matplotlib VScode will be in error, the correct approach is to use plt.show ()
Here Insert Picture Description
plt.show()to keep the image open, close the image will continue to run down the code
Here Insert Picture Description

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 2*np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

plt.figure(1)
plt.plot(x, y1)
plt.pause(2)  # 在figure(1)上绘制sin曲线,2s后自动关闭窗口

plt.figure(2)
plt.plot(x, y2)
plt.pause(2)  # 在figure(2)上绘制cos曲线,2s后自动关闭窗口

plt.pause(0)  # 重新绘制figure(1)和figure(2),不会自动关闭
print(2)

References:
two modes matplotlib drawing "block" and "interactive"

Published 308 original articles · won praise 149 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_43827595/article/details/104911316
Recommended