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) 的线
plot() :用于画图,它可以绘制点和线。
例: 绘制坐标 (1, 3) 和 (8, 10) 的两个点。
plot():用于画图,它可以绘制点和线。
绘制一条不规则线,坐标为 (1, 3)、(2, 8)、(6, 1)、(8, 10)
如果我们不指定 x 轴上的点,则 x 会根据 y 的值来设置为 0, 1, 2, 3..N-1。
小练习:
Matplotlib 绘图标记:
fmt 参数定义了基本格式,如标记、线条样式和颜色。
fmt = '[marker][line][color]'。
设置标记大小与颜色:
线的类型可以使用 linestyle 参数(可简写为 ls)定义:
线的颜色可以使用 color 参数(可简写为 c)定义:
线的宽度可以使用 linewidth(可简写为 lw)参数定义:
xlabel()、ylabel() :设置 x 轴和 y 轴的标签
title() :设置标题
Matplotlib 网格线
grid():来设置图表中的网格线。
grid() 方法语法格式如下:
matplotlib.pyplot.grid(b=None, which='major', axis='both',)
设置X轴方向网格线:plt.gird(axis = 'x')
颜色,线型,线宽
Matplotlib 绘制多图
subplot():用于绘制多个子图,在绘图时需要指定位置。
subplot(nrows, ncols, index)
scatter():绘制散点图
设置图标大小:
自定义点的颜色:
设置两组散点图:
bar():绘制柱形图
设置柱形图颜色:
自定义各个柱形的颜色:
使用 width 设置柱形图宽度:
barh():绘制垂直方向的柱形图
练习题:
很多人在买一本书的时候,都比较喜欢货比三家,例如《python数据分析实战》在亚马逊、当当网、中国图书网、京东和天猫的最低价格分别为39.5、39.9、45.4、38.9、33.34。针对这个数据,我们也可以通过柱形图来完成,这里使用水平柱形图来显示。
pie():绘制饼图
以上便是我在上课期间学的matplotlib,在下目前小白,刚开始学,开始对编程感兴趣了,感觉挺有意思,若是哪里有不对的地方或者欠缺的,欢迎大家评论。