【matplotlib教程】数据可视化
@TOC显示中文和负号
[*]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):...常用参数:
参数含义args1横坐标数组(list,numpy数组,pandas电子表格)args2纵坐标数组(list,numpy数组,pandas电子表格)label图例名字,给一个字符串即可color颜色linestyle线型marker附加点的样式参数说明:
[*]若输入两个数组作为args,那么第一个参数和第二个参数的数组长度必须雷同,它们是一一对应关系
[*]若只输入一个数组作为args,那么matplolib会主动根据数组长度进行横坐标的索引
[*]颜色、线性、点样式会在第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柱形图的宽度bottomheight坐标的偏移量,默认 0align柱形图与 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 =
Sh =
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样式形貌"." https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133452507-442041607.png点 ","https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133453196-850042398.png像素点"o" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133453912-1607998402.png实心圆"v" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133454731-1693305230.png下三角 "^"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133455659-393891845.png上三角""https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133456988-1977875234.png右三角"1" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133457666-2098629170.png下三叉 "2"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133458271-345656290.png上三叉 "3"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133458917-1160635323.png左三叉"4"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133459615-316989053.png右三叉"8"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133500230-1084019851.png八角形 "s"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133500830-1426853834.png正方形"p" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133501472-2056168927.png五边形"P" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133502085-1249086844.png填充的加号"*" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133502707-1484866236.png星号 "h" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133503321-1536714865.png六边形1"H" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133503958-193824117.png六边形2"+" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133504561-2133636738.png加号 "x" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133505482-847120284.png乘号 "X" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133506126-2016891943.png填充的乘号"D" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133506779-1837935256.png菱形"d"https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133507408-137987930.png瘦菱形 "|" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133508040-1106975541.png竖线 "_" https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133508603-798320069.png横线 4 https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133509218-151814904.png左箭头 5 https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133509941-1322293605.png右箭头 6https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133510646-688025176.png上箭头7 https://img2024.cnblogs.com/other/3132880/202408/3132880-20240823133511739-1507538454.png下箭头3.画布管理与多图
3.1 subplots创建一张画布上的多图
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.plot
3.2 subplot创建一张画布上的多图
def subplot(*args, **kwargs):利用说明
[*]输入plt.subplot(2,2,1)后,表明此时选定2行2列的一共4张子图中的第1张子图
[*]此时调用plt.plt,就可以在第1张子图上绘图
[*]切换子图时,输入plt.subplot(2,2,2)后,表明切换到第2张子图
3.3 整理
[*]整理一张图上所有内容:
plt.cla()
[*]整理一张画布上的所有图:
plt.clf() 3.4 画布尺寸设置
fig = plt.figure(figsize=(16, 6),dpi=100)以上设置了一张宽度16英寸,高度6英寸的画布,且每英寸的像素个数为100,也就是说此图片的宽高是1600x600。大家可以自行修改想要的数值。
fig.tight_layout()以上这行指令用于在所有子图都绘制完毕后主动调解子图之间的位置,防止子图的轴标签被遮挡。
动态图
[*]先开启交互模式,然后提前展示画布
plt.ion()
plt.show()
[*]在每轮绘制前先进行整理,然后停留
plt.cla()
plt.plot(...)
plt.pause(0.001)本文由博客一文多发平台 OpenWrite 发布!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]