matplotlib
1、安装matplotlib
① linux
系统安装
#
安装
matplotlib
模块
$ sudo apt-get install python3-matplotlib#
如果是
python2.7
执行如下命令
$ sudo apt-get install python-matplotlib#
如果你安装较新的
Python
,安装模块一乐的一些库
$ sudo apt-get install python3.5-dev python3.5-tk tk-dev
$ sudo apt-get install libfreetype6-dev g++#
再使用
pip
来安装模块
$ pip install --user matplotlib
② OS
系统中安装
#
安装
matplotlib
模块
$ pip install --user matplotlib
③ windows
系统中安装
#
安装
matplotlib
模块
$pip3 install matplotlib
进入终端执行import matplotlib
不报错表示执行成功
2、绘制简单的折线图
①
创建
mpl_squares.py
文件:
import matplotlib.pyplot
as plt #
导入模块
squares = [1,4,9,16,25] #
指定列表
Y
坐标为列表中的值,
X
坐标为列表下标
plt.plot(squares) #
传入列表
plt.show() #
输出图像
绘图:

②
修改标签文字和线条粗细:
import matplotlib.pyplot
as plt #
导入模块
squares = [1,4,9,16,25] #
指定列表
Y
坐标为列表中的值,
X
坐标为列表下标
plt.plot(squares,linewidth=5) # linewidth
决定绘制线条的粗细
plt.title('Square Numbers',fontsize=24) #
标题
plt.xlabel('Vaule',fontsize=14)
plt.ylabel('Square of Vaule',fontsize=14)
plt.tick_params(axis='both',labelsize=14) #
刻度加粗
plt.show() #
输出图像
绘图:

③
校正图形(设定
X
坐标):
import matplotlib.pyplot
as plt #
导入模块
squares = [1,4,9,16,25] #
指定列表
Y
坐标为列表中的值
input_values = [1,2,3,4,5]
plt.plot(input_values,squares,linewidth=5) # linewidth
决定绘制线条的粗细
plt.title('Square Numbers',fontsize=24) #
标题
plt.xlabel('Vaule',fontsize=14)
plt.ylabel('Square of Vaule',fontsize=14)
plt.tick_params(axis='both',labelsize=14) #
刻度加粗
plt.show() #
输出图像
绘图:

3、绘制散点图
①
创建
scatter_sqares.py:
import matplotlib.pyplot
as plt
plt.scatter(2,4,s=200) #X
坐标
2
,
Y
坐标
4 S=200
点大小
plt.title('Square Numbers',fontsize=24) #
标题
plt.xlabel('Vaule',fontsize=14)
plt.ylabel('Square of Vaule',fontsize=14)
plt.tick_params(axis='both',labelsize=14) #
刻度加粗
plt.show() #
输出图像
绘图:

②
绘制一系列点:
import matplotlib.pyplot
as plt
x_values = [1,2,3,4,5] #
指定
X
轴
y_values = [1,4,9,16,25] #
指定
Y
轴
plt.scatter(x_values,y_values,s=100)
--snip---
plt.show() #
输出图像
绘图

来源:
博客园