matplotlib 基础练习:画一元二次函数
样例:
1、安装matplotlib模块
Matplotlib 是 Python 的绘图库。
安装方法:
pip install matplotlib -i http://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
C:\Users\Ashley>pip install matplotlib -i http://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality.
Looking in indexes: http://pypi.tuna.tsinghua.edu.cn/simple
Collecting matplotlib
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c9/1e/0cf26e92de5438d0f2118435476665aae843002f4701da296b0b3252a237/matplotlib-3.0.3-cp35-cp35m-win_amd64.whl (9.1 MB)
|████████████████████████████████| 9.1 MB 6.8 MB/s
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in d:\lenovosoftstore\python35\lib\site-packages (from matplotlib) (2.2.0)
Requirement already satisfied: python-dateutil>=2.1 in d:\lenovosoftstore\python35\lib\site-packages (from matplotlib) (2.6.0)
Collecting kiwisolver>=1.0.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/27/74677003aecfc85421f6b70db3e49b52e65f6497a5f2faf4e345588b3c61/kiwisolver-1.1.0-cp35-none-win_amd64.whl (57 kB)
|████████████████████████████████| 57 kB 231 kB/s
Requirement already satisfied: numpy>=1.10.0 in d:\lenovosoftstore\python35\lib\site-packages (from matplotlib) (1.14.2)
Collecting cycler>=0.10
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Requirement already satisfied: six>=1.5 in d:\lenovosoftstore\python35\lib\site-packages (from python-dateutil>=2.1->matplotlib) (1.11.0)
Requirement already satisfied: setuptools in d:\lenovosoftstore\python35\lib\site-packages (from kiwisolver>=1.0.1->matplotlib) (39.0.1)
Installing collected packages: kiwisolver, cycler, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.0.3
2、调用matplotlib模块实现 画一元二次函数
- numpy模块 NumPy(Numerical Python) 是 Python
语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。 - linspace函数 linspace(x1,x2,N)
linspace是Matlab中的均分计算指令,用于产生x1,x2之间的N点行线性的矢量。
代码:
import matplotlib.pyplot as plt # 导入matplotlib包的子模块pyplot,并将其重命名为plt
import numpy as np # 导入numpy,并将其重命名为np
x = np.linspace(-5, 5, 101) # 生成-5到5之间的51个点的一维元组
print(x)
y = 2 * x * x + 1
plt.plot(x, y) # 画图
plt.show()
plt.savefig(r"D:\u_career\i_coder\i_python_course_1026\figure.jpg") # 保存到本地
参考链接:
NumPy Matplotlib
python 画正弦曲线
python之numpy的基本使用
NumPy 教程
linspace