Matplotlib的安装:

1.pip install matplotlib

2.conda install matplotlib(清华源:https://pypi.tuna.tsinghua.edu.cn/simple)

Matplotlib的导入:

import matplotlib.pyplot as plt

一.plot():绘制点和线

格式;plot([x],y,[fmt]................,**kwars)       

x:横坐标   y:纵坐标    fmt:定义基本格式(如颜色、标记和线条样式)

kwag:可选,用在二维平面图上,设置指定属性,如标签,线的宽度

示例:绘制坐标 (1, 3) 到 (8, 10) 的线

python matlibplot画竖线 matplotlib 画横线_matplotlib

python matlibplot画竖线 matplotlib 画横线_自定义_02

plot() :用于画图,它可以绘制点和线。

例: 绘制坐标 (1, 3) 和 (8, 10) 的两个点。

python matlibplot画竖线 matplotlib 画横线_自定义_03

 

python matlibplot画竖线 matplotlib 画横线_python_04

plot():用于画图,它可以绘制点和线。

绘制一条不规则线,坐标为 (1, 3)、(2, 8)、(6, 1)、(8, 10)

 

python matlibplot画竖线 matplotlib 画横线_python_05

 

python matlibplot画竖线 matplotlib 画横线_自定义_06

如果我们不指定 x 轴上的点,则 x 会根据 y 的值来设置为 0, 1, 2, 3..N-1。

 

python matlibplot画竖线 matplotlib 画横线_开发语言_07

python matlibplot画竖线 matplotlib 画横线_指定位置_08

小练习:

python matlibplot画竖线 matplotlib 画横线_开发语言_09

 

python matlibplot画竖线 matplotlib 画横线_自定义_10

 

python matlibplot画竖线 matplotlib 画横线_指定位置_11

Matplotlib 绘图标记:

python matlibplot画竖线 matplotlib 画横线_自定义_12

 

python matlibplot画竖线 matplotlib 画横线_自定义_13

 

python matlibplot画竖线 matplotlib 画横线_指定位置_14

 

python matlibplot画竖线 matplotlib 画横线_matplotlib_15

 

python matlibplot画竖线 matplotlib 画横线_python_16

fmt 参数定义了基本格式,如标记、线条样式和颜色。

fmt = '[marker][line][color]'。

python matlibplot画竖线 matplotlib 画横线_matplotlib_17

python matlibplot画竖线 matplotlib 画横线_自定义_18

python matlibplot画竖线 matplotlib 画横线_python_19

 

 

设置标记大小与颜色:

python matlibplot画竖线 matplotlib 画横线_指定位置_20

 

 

python matlibplot画竖线 matplotlib 画横线_指定位置_21

 

 

python matlibplot画竖线 matplotlib 画横线_开发语言_22

线的类型可以使用 linestyle 参数(可简写为 ls)定义:

 

python matlibplot画竖线 matplotlib 画横线_matplotlib_23

 

python matlibplot画竖线 matplotlib 画横线_自定义_24

 

 

python matlibplot画竖线 matplotlib 画横线_开发语言_25

线的颜色可以使用 color 参数(可简写为 c)定义:

python matlibplot画竖线 matplotlib 画横线_自定义_26

 

线的宽度可以使用 linewidth(可简写为  lw)参数定义:

python matlibplot画竖线 matplotlib 画横线_python_27

 

python matlibplot画竖线 matplotlib 画横线_自定义_28

xlabel()、ylabel() :设置 x 轴和 y 轴的标签

python matlibplot画竖线 matplotlib 画横线_python_29

title() :设置标题

python matlibplot画竖线 matplotlib 画横线_开发语言_30

 

python matlibplot画竖线 matplotlib 画横线_自定义_31

Matplotlib 网格线

grid():来设置图表中的网格线。

grid() 方法语法格式如下:

matplotlib.pyplot.grid(b=None, which='major', axis='both',)

python matlibplot画竖线 matplotlib 画横线_开发语言_32

 

python matlibplot画竖线 matplotlib 画横线_自定义_33

 

python matlibplot画竖线 matplotlib 画横线_matplotlib_34

 设置X轴方向网格线:plt.gird(axis = 'x')

python matlibplot画竖线 matplotlib 画横线_开发语言_35

python matlibplot画竖线 matplotlib 画横线_matplotlib_36

 颜色,线型,线宽

python matlibplot画竖线 matplotlib 画横线_指定位置_37

 

 

python matlibplot画竖线 matplotlib 画横线_自定义_38

Matplotlib 绘制多图

subplot():用于绘制多个子图,在绘图时需要指定位置。

subplot(nrows, ncols, index)

python matlibplot画竖线 matplotlib 画横线_自定义_39

python matlibplot画竖线 matplotlib 画横线_自定义_40

python matlibplot画竖线 matplotlib 画横线_自定义_41

 scatter():绘制散点图

python matlibplot画竖线 matplotlib 画横线_自定义_42

 

python matlibplot画竖线 matplotlib 画横线_指定位置_43

设置图标大小:

python matlibplot画竖线 matplotlib 画横线_开发语言_44

 

python matlibplot画竖线 matplotlib 画横线_matplotlib_45

自定义点的颜色:

python matlibplot画竖线 matplotlib 画横线_开发语言_46

 

 

python matlibplot画竖线 matplotlib 画横线_python_47

设置两组散点图:

python matlibplot画竖线 matplotlib 画横线_自定义_48

 

 

python matlibplot画竖线 matplotlib 画横线_自定义_49

bar():绘制柱形图

python matlibplot画竖线 matplotlib 画横线_开发语言_50

python matlibplot画竖线 matplotlib 画横线_指定位置_51

设置柱形图颜色:

 

python matlibplot画竖线 matplotlib 画横线_自定义_52

python matlibplot画竖线 matplotlib 画横线_python_53

自定义各个柱形的颜色:

python matlibplot画竖线 matplotlib 画横线_自定义_54

 

python matlibplot画竖线 matplotlib 画横线_matplotlib_55

使用 width 设置柱形图宽度:

 

python matlibplot画竖线 matplotlib 画横线_python_56

python matlibplot画竖线 matplotlib 画横线_matplotlib_57

barh():绘制垂直方向的柱形图

 

python matlibplot画竖线 matplotlib 画横线_python_58

python matlibplot画竖线 matplotlib 画横线_指定位置_59

 练习题:

很多人在买一本书的时候,都比较喜欢货比三家,例如《python数据分析实战》在亚马逊、当当网、中国图书网、京东和天猫的最低价格分别为39.5、39.9、45.4、38.9、33.34。针对这个数据,我们也可以通过柱形图来完成,这里使用水平柱形图来显示。

python matlibplot画竖线 matplotlib 画横线_开发语言_60

 

python matlibplot画竖线 matplotlib 画横线_指定位置_61

 

pie():绘制饼图

python matlibplot画竖线 matplotlib 画横线_自定义_62

 

python matlibplot画竖线 matplotlib 画横线_开发语言_63

 

 

 以上便是我在上课期间学的matplotlib,在下目前小白,刚开始学,开始对编程感兴趣了,感觉挺有意思,若是哪里有不对的地方或者欠缺的,欢迎大家评论。