|   0 评论   |   23 浏览

  1. Create and activate the virtual environment

    For windows

    py -3 -m venv .venv

    If the activate command generates the message "Activate.ps1 is not digitally signed. You cannot run this script on the current system.", then you need to temporarily change the PowerShell execution policy to allow scripts to run (see About Execution Policies in the PowerShell documentation):

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

    For macOS/Linux

    python3 -m venv .venv
    source .venv/bin/activate
  2. Install the packages

    # Don't use with Anaconda distributions because they include matplotlib already.
    # macOS
    python3 -m pip install matplotlib
    # Windows (may require elevation)
    python -m pip install matplotlib
    # Linux (Debian)
    apt-get install python3-tk
    python3 -m pip install matplotlib
  3. Select your new environment by using the Python: Select Interpreter command from the Command Palette.

  4. Rerun the program now (with or without the debugger) and after a few moments a plot window appears with the output:

    matplotlib output

  5. Once you are finished, type deactivate in the terminal window to deactivate the virtual environment.

  6. 新建文件

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point                   # Display the plot