matplotlib基础使用

这篇博客介绍了matplotlib的基础使用,包括2D绘图、折线图、散点图、柱形图和饼状图的绘制方法,以及如何设置颜色、标记、线型、刻度范围和标签。还讲解了plt.subplots()在创建多个图表时的优势,以及图例的添加和位置调整。
摘要由CSDN通过智能技术生成

1、matplotlib简介

Python 的 2D绘图库,为Python构建一个Matlab式的绘图接口,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等

2、matplotlib的使用

(1)折线图

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
print(type(fig))
<class 'matplotlib.figure.Figure'>
arr = np.random.randn(100)
plt.plot(arr)
plt.show()
 

Subplot 可以通过add_subplot来分割figure,表示可以在figure的不同位置上作图 fig.

add_subplot(a, b, c) a,b 表示将fig分割成 a*b 的区域 c 表示当前选中要操作的区域,

注意:从1开始编号(不是从0开始)

plot 绘图的区域是最后一次指定subplot的位置 (jupyter notebook里不能正确显示)

fig = plt.figure()
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)
ax1.plot(np.random.randn(100))
ax2.plot(np.random.randn(100))
ax3.plot(np.random.randn(100))
ax4.plot(np.random.randn(100))
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值