matplot绘图
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,MatplotlibMatplotlib 是一个 Python 的 2D绘图库,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。http
千家信息网最后更新 2026年10月31日matplot绘图
Matplotlib
Matplotlib 是一个 Python 的 2D绘图库,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。
http://matplotlib.org
- 用于创建出版质量图表的绘图工具库
- 目的是为Python构建一个Matlab式的绘图接口
- import matplotlib.pyplot as plt
- pyplot模块包含了常用的matplotlib API函数
figure
- Matplotlib的图像均位于figure对象中
- 创建figure:fig = plt.figure()
示例代码:
# 引入matplotlib包import matplotlib.pyplot as pltimport numpy as np%matplotlib inline #在jupyter notebook 里需要使用这一句命令# 创建figure对象fig = plt.figure()运行结果:会弹出一个figure窗口,如下图所示
subplot
fig.add_subplot(a, b, c)
- a,b 表示将fig分割成 a*b 的区域
- c 表示当前选中要操作的区域,
- 注意:从1开始编号(不是从0开始)
- plot 绘图的区域是最后一次指定subplot的位置 (jupyter notebook里不能正确显示)
示例代码:
# 指定切分区域的位置ax1 = fig.add_subplot(2,2,1)ax2 = fig.add_subplot(2,2,2)ax3 = fig.add_subplot(2,2,3)ax4 = fig.add_subplot(2,2,4)# 在subplot上作图random_arr = np.random.randn(100)#print random_arr# 默认是在最后一次使用subplot的位置上作图,但是在jupyter notebook 里可能显示有误plt.plot(random_arr)# 可以指定在某个或多个subplot位置上作图# ax1 = fig.plot(random_arr)# ax2 = fig.plot(random_arr)# ax3 = fig.plot(random_arr)# 显示绘图结果plt.show()运行结果:仅右下角有图
直方图:hist
示例代码:
import matplotlib.pyplot as pltimport numpy as npplt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)plt.show()
散点图:scatter
示例代码:
import matplotlib.pyplot as pltimport numpy as np# 绘制散点图x = np.arange(50)y = x + 5 * np.random.rand(50)plt.scatter(x, y)plt.show()柱状图:bar
示例代码:
import matplotlib.pyplot as pltimport numpy as np# 柱状图x = np.arange(5)y1, y2 = np.random.randint(1, 25, size=(2, 5))width = 0.25ax = plt.subplot(1,1,1)ax.bar(x, y1, width, color='r')ax.bar(x+width, y2, width, color='g')ax.set_xticks(x+width)ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])plt.show()矩阵绘图:plt.imshow()
- 混淆矩阵,三个维度的关系
示例代码:
import matplotlib.pyplot as pltimport numpy as np# 矩阵绘图m = np.random.rand(10,10)print(m)plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)plt.colorbar()plt.show()plt.subplots()
- 同时返回新创建的figure和subplot对象数组
- 生成2行2列subplot:fig, subplot_arr = plt.subplots(2,2)
- 在jupyter里可以正常显示,推荐使用这种方式创建多个图表
示例代码:
import matplotlib.pyplot as pltimport numpy as npfig, subplot_arr = plt.subplots(2,2)# bins 为显示个数,一般小于等于数值个数subplot_arr[1,0].hist(np.random.randn(100), bins=10, color='b', alpha=0.3)plt.show()运行结果:左下角绘图
颜色、标记、线型
- ax.plot(x, y, 'r--')
等价于ax.plot(x, y, linestyle='--', color='r')
示例代码:
import matplotlib.pyplot as pltimport numpy as npfig, axes = plt.subplots(2)axes[0].plot(np.random.randint(0, 100, 50), 'ro--')# 等价axes[1].plot(np.random.randint(0, 100, 50), color='r', linestyle='dashed', marker='o')- 常用的颜色、标记、线型
颜色
- b: blue
- g: grean
- r: red
- c: cyan
- m: magenta
- y: yellow
- k: black
- w: white
标记
- .: point
- ,: pixel
- o: circle
- v: triangle_down
- ^: triangle_up
- <: tiiangle_left
线型
- '-' or 'solid': solid lint
- '--' or 'dashed': dashed line
- '-.' or 'dashdot': dash-dotted line
- ':' or 'dotted': dotted line
- 'None': draw nothing
- ' ': draw nothing
- '': draw nothing
刻度、标签、图例
设置刻度范围
plt.xlim(), plt.ylim()
ax.set_xlim(), ax.set_ylim()
设置显示的刻度
plt.xticks(), plt.yticks()
ax.set_xticks(), ax.set_yticks()
设置刻度标签
ax.set_xticklabels(), ax.set_yticklabels()
设置坐标轴标签
ax.set_xlabel(), ax.set_ylabel()
设置标题
ax.set_title()
- 图例
ax.plot(label='legend')
ax.legend(), plt.legend()
loc='best':自动选择放置图例最佳位置
示例代码:
import matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots(1)ax.plot(np.random.randn(1000).cumsum(), label='line0')# 设置刻度#plt.xlim([0,500])ax.set_xlim([0, 800])# 设置显示的刻度#plt.xticks([0,500])ax.set_xticks(range(0,500,100))# 设置刻度标签ax.set_yticklabels(['Jan', 'Feb', 'Mar'])# 设置坐标轴标签ax.set_xlabel('Number')ax.set_ylabel('Month')# 设置标题ax.set_title('Example')# 图例ax.plot(np.random.randn(1000).cumsum(), label='line1')ax.plot(np.random.randn(1000).cumsum(), label='line2')ax.legend()ax.legend(loc='best')#plt.legend()
代码
绘图
示例
刻度
位置
标签
区域
图例
结果
对象
标记
矩阵
线型
颜色
运行
个数
图表
坐标
坐标轴
多个
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技企业
中资互联网科技
麻工互联网科技
礼当家互联网科技
以房互联网科技
鑫亿盛互联网科技
掌上梦互联网科技
互联网科技公司有哪些
深圳期待互联网科技
互联网前沿科技
汉立互联网科技有限公司联系电话
阿里巴巴算科技企业还是互联网
宁夏众和广鑫互联网科技有限公司
厦门千工互联网科技有限公司
皮皮虾互联网科技有限公司
郑州欣桑互联网科技有限公司
银川新华互联网科技学校是国营吗
长沙易易互联网科技有限公司
科技美学互联网 艺术
科技发展第三阶段互联网时代
江苏根荣互联网科技
互联网为科技
互联网科技缩写
赫美联合互联网科技
深圳艾佳互联网科技
亿人帮互联网科技
上海电发互联网科技有限公司
北京大学互联网科技怎么样
彤创互联网科技有限公司
昆明中奇联合互联网科技