Matplotlib常用绘图指令大全

本文详细介绍了Matplotlib库在Python中的各种绘图指令,包括直方图、条形图、折线图、饼图、散点图、箱线图、极线图和阶梯图等基本图形,以及图表参数配置、堆积图、分块图和气泡图等高级图形的绘制方法。通过这些指令,可以有效地可视化数据并理解其分布、关系和趋势。
摘要由CSDN通过智能技术生成

常用配置: matplotlib.rcParams

本文首发于本csdn博主私人博客:Timing is Fun

  • A dictionary object including validation
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    mpl.rcParams['font.sans-serifs'] = ['SimHei'] # 中文支持
    mpl.rcParams['axes.unicode_minus'] = False # 正常显示负号
    
    mpl.rcParams['lines.linewidth'] = 5 # 线条宽度
    mpl.rcParams['lines.color'] = 'red' # 线条颜色
    mpl.rcParams['lines.linestyle'] = '-' # 线条样式

基本图形

直方图

  • 查看分布规律
  • plt.hist(x, bins)
    • x - array or sequence of (n,) arrays,数据data
    • bins - int or sequence or str, optional, 横坐标区间
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    height = [168, 155, 182, 170, 173, 161]
    bins = range(150,191,5)
    
    plt.hist(height, bins=bins)
    plt.show()

在这里插入图片描述

条形图

  • 同类数据进行对比
  • plt.bar(x, y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    classes = ['class 1', 'class 2', 'class 3']
    scores = [70, 80, 60]
    
    plt.bar(classes, scores)
    plt.show()

在这里插入图片描述

折线图

  • 展示数据随某一指标变化的关系
  • plt.plot(x, y)
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    plt.plot(year, height)
    plt.title('身高随时间变化图')
    plt.show()

在这里插入图片描述

饼图

  • 展示不同类在整体中所占的比重
  • plt.pie(data, labels, autopct)
    • data - array or sequence of (n,) arrays,数据data
    • labels - list, optional, default: None,数据标签
    • autopct - None (default), string, or function, optional,占比计算
    import matplotlib.pyplot as plt 
    %matplotlib inline
    
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    labels = ['衣', '食', '住', '行' ]
    data &#
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值