【matplotlib教程】数据可视化


显示中文和负号

  • matplotlib默认使用英文字库,汉字会乱码,要指定中文字库
matplotlib.rcParams['font.family']='simHei'  #黑体
matplotlib.pyplot.rcParams['axes.unicode_minus'] = False	# 显示负号

1.各种绘图函数

1.1 matplotlib.pyplot.plot

def plot(*args, scalex=True, scaley=True, data=None, **kwargs):...

常用参数:

参数 含义
第一个参数 横坐标数组(list,numpy数组,pandas电子表格)
第二个参数 纵坐标数组(list,numpy数组,pandas电子表格)
label 图例
color 颜色
linestyle 线型
marker 附加点的样式

参数说明:

  • 第一个参数和第二个参数的数组长度必须相同,一一对应
  • 图例、颜色等,会在第2节中详细介绍

1.2 matplotlib.pyplot.scatter

def scatter(
        x, y, s=None, c=None, marker=None, cmap=None, norm=None,
        vmin=None, vmax=None, alpha=None, linewidths=None, *,
        edgecolors=None, plotnonfinite=False, data=None, **kwargs):...

常用参数:

参数 含义
x 横坐标数组(list,numpy数组,pandas电子表格)
y 纵坐标数组(list,numpy数组,pandas电子表格)
s 点的大小
marker 点的样式
label 图例
color 颜色

参数说明:

  • x参数和y参数的数组长度必须相同,一一对应
  • s表示点的大小,默认 20,也可以是个数组,数组每个参数为对应点的大小
  • 图例、颜色等,会在第2节中详细介绍

1.3 matplotlib.pyplot.bar

def bar(
        x, height, width=0.8, bottom=None, *, align='center',
        data=None, **kwargs):...

常用参数:

参数 含义
x 数组,柱形图的 x 轴数据
height 数组,柱形图的 y 轴数据
width 柱形图的宽度
bottom height 坐标的偏移量,默认 0
align 柱形图与 x 坐标的对齐方式
color 颜色
label 图例
tick_label 用来替代 x 轴数据的字符串或字符串数组

参数说明:

  • x参数和height参数的数组长度必须相同,一一对应
  • width 表示柱的宽度,也可以是个数组,对应每条柱的横向宽度
  • bottom表示height坐标的偏移量,也可以是个数组,对应每条柱的y偏移量
  • align可选参数:中心对齐"center",左对齐"edge"
  • tick_label可以替代x轴的数字,也可以是个数组,对应每条柱的x轴位置
  • 图例、颜色等,会在第2节中详细介绍

下面是一个在一张图上画出两幅柱状图的例子:

import numpy as np
import matplotlib.pyplot as plt
# 数据
x = np.arange(4)
Bj = [52, 55, 63, 53]
Sh = [44, 66, 55, 41]
bar_width = 0.3
# 绘图 x 表示 从那里开始
plt.bar(x, Bj, bar_width)
plt.bar(x+bar_width, Sh, bar_width, align="center")
# 展示图片
plt.show()

1.4 matplotlib.pyplot.pie

def pie(
        x, explode=None, labels=None, colors=None, autopct=None,
        pctdistance=0.6, shadow=False, labeldistance=1.1,
        startangle=0, radius=1, counterclock=True, wedgeprops=None,
        textprops=None, center=(0, 0), frame=False,
        rotatelabels=False, *, normalize=True, hatch=None, data=None):

常用参数:

参数 含义
x 数组,用于绘制饼图的数据,表示每个扇形的面积
explode 数组,表示各个扇形之间的间隔
labels 列表,各个扇形的标签
colors 数组,表示各个扇形的颜色
autopct 字符串,设置饼图内各个扇形百分比显示格式
radius 设置饼图的半径

参数说明:

  • x参数,labels参数,colors参数的数组长度必须相同,一一对应
  • explode决定图形显示的方式
  • autopct举例:%d%% 整数百分比,%0.1f 一位小数, %0.1f%% 一位小数百分比, %0.2f%% 两位小数百分比

1.5 matplotlib.pyplot.hist

def hist(
        x, bins=None, range=None, density=False, weights=None,
        cumulative=False, bottom=None, histtype='bar', align='mid',
        orientation='vertical', rwidth=None, log=False, color=None,
        label=None, stacked=False, *, data=None, **kwargs):...

常用参数:

参数 含义
x 数组,表示要绘制直方图的数据
bins 直方图的箱数
colors 颜色
label 图例

参数说明:

  • bins举例说明:如果 bins 参数为 30,这意味着将数据范围分成 30 个等宽的区间,然后统计每个区间内数据的频数。
  • 图例、颜色等,会在第2节中详细介绍

2.绘图样式

2.1 轴标签和标题

轴标签(matplotlib.pyplot.xlabel与matplotlib.pyplot.ylabel)

def xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs):...
def ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs):...
  • loc参数只有"left", "center" , "right "可选

标题

def title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs):
  • loc参数只有"left", "center" , "right "可选

2.2 图例位置(matplotlib.pyplot.legend)

  • 在绘图时指定好label图例后,如果不使用matplotlib.pyplot.legend函数指定图例位置,图例是不会显示的
def legend(*args, **kwargs):...
  • loc参数如下:
可选参数
'upper right'
'upper left'
'lower left'
'lower right'
'right'
'center left'
'center right'
'lower center'
'upper center'
'center'

2.3 可选颜色(color)

常用颜色如下:

颜色标记 描述
'r' 红色
'g' 绿色
'b' 蓝色
'c' 青色
'm' 品红
'y' 黄色
'k' 黑色
'w' 白色

2.4 线型(linestyle)

常用线型如下:

线型标记 描述
'-' 实线
':' 点虚线
'--' 破折线
'-.' 点划线

2.5 点的样式(marker)

常用样式如下:
常用颜色如下:

可选markder 样式 描述
"." 在这里插入图片描述
"," 在这里插入图片描述 像素点
"o" 在这里插入图片描述 实心圆
"v" 在这里插入图片描述 下三角
"^" 在这里插入图片描述 上三角
"<" 在这里插入图片描述 左三角
">" 在这里插入图片描述 右三角
"1" 在这里插入图片描述 下三叉
"2" 在这里插入图片描述 上三叉
"3" 在这里插入图片描述 左三叉
"4" 在这里插入图片描述 右三叉
"8" 在这里插入图片描述 八角形
"s" 在这里插入图片描述 正方形
"p" 在这里插入图片描述 五边形
"P" 在这里插入图片描述 填充的加号
"*" 在这里插入图片描述 星号
"h" 在这里插入图片描述 六边形1
"H" 在这里插入图片描述 六边形2
"+" 在这里插入图片描述 加号
"x" 在这里插入图片描述 乘号
"X" 在这里插入图片描述 填充的乘号
"D" 在这里插入图片描述 菱形
"d" 在这里插入图片描述 瘦菱形
"|" 在这里插入图片描述 竖线
"_" 在这里插入图片描述 横线
4 在这里插入图片描述 左箭头
5 在这里插入图片描述 右箭头
6 在这里插入图片描述 上箭头
7 在这里插入图片描述 下箭头

3.画布管理与多图

3.1 创建一张画布上的多图

def subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True,
             width_ratios=None, height_ratios=None,
             subplot_kw=None, gridspec_kw=None, **fig_kw):...

参数说明:

  • nrows表示子图的行数,ncols表示子图的列数

返回值说明:

  • 返回一个有2个元素的元组,分别为fig和ax。
  • fig为这张画布
  • ax为子图列表,想在第一张图上画折线图:调用ax[0].plot

3.2 清理

  • 清理一张图上所有内容:
plt.cla()
  • 清理一张画布上的所有图:
plt.clf() 

动态图

  1. 先开启交互模式,然后提前展示画布
plt.ion()
plt.show()
  1. 在每轮绘制前先进行清理,然后停留
plt.cla()
plt.plot(...)
plt.pause(0.001)

猜你喜欢

转载自blog.csdn.net/qq_50791664/article/details/131604619