python: using Jupyter notebook (testing matplotlib and opencv)

environment:

  • window1
  • python 3.10.6

reference:

1. Create a virtual environment

This step can be skipped (because the author doesn't like to install anything in the global environment, so make a new environment).

First select a directory: D:\jackletter\test-pyenv(This directory has not been created yet)
Enter powershell:
insert image description here
Execute the command:py -m venv test-pyenv
insert image description here

Activate the environment:.\test-pyenv\Scripts\Activate.ps1
insert image description here

2. Install Jupyter notebook

implementpip install notebook

insert image description here
After the installation is complete, you will see the following (pip list shows that there are many more packages):
insert image description here

3. Use Jupyter notebook

First select an empty directory, as follows:

insert image description here

The command line is positioned here, and then executed: jupyter notebook
insert image description here
At this point we can create a new test (ctrl+enter to execute the code):
insert image description here

4. Test matplotlib

First install: pip install matplotlib, check the version after installation (pip list):
insert image description here

Then test in Jupyter notebook as follows:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 2, 6]
y1 = [e+1 for e in y]
y2 = [e+2 for e in y]
plt.plot(x, y, "b.")                  # b:蓝色,.:点
plt.plot(x, y1, "ro")                  # r:红色,o:圆圈
plt.plot(x, y2, "kx")                 # k:黑色,x:x字符(小叉)
plt.show()                            # 在窗口显示该图片

insert image description here

5. Test opencv

Or install first, pip install opencv-pythonand check the version after installation (pip list):

insert image description here
Then, prepare an image in the Jupyter notebook directory:
insert image description here

Then test in Jupyter notebook as follows:

import cv2

img = cv2.imread('cat.webp')

cv2.imshow('cat',img)

cv2.waitKey(0)

insert image description here

Guess you like

Origin blog.csdn.net/u010476739/article/details/129350635