当前位置: 首页 > article >正文

plt.bar函数介绍及实战

目录

  • plt.bar() 函数
  • 实战

plt.bar() 函数

plt.bar() 函数是 Matplotlib 中用于创建柱状图的函数。它用于在图形中绘制一个或多个柱状图,通常用于展示类别型数据的数量或大小的比较。

基本语法:

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

参数说明:

  1. x:柱状图的 x 坐标位置,通常是类别型数据的位置
  2. height:柱状图的高度,即对应类别的数值
  3. width:柱状的宽度,默认为 0.8
  4. bottom:柱状图的底部位置,默认为 None,表示从 0 开始
  5. align:柱状图的对齐方式,可选值有’center’(默认)、‘edge’
  6. data:用于绘图的数据,可以是 DataFrame 或类似的结构
  7. **kwargs:其他可选参数,例如颜色、标签等

示例用法:

import matplotlib.pyplot as plt

categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]

plt.bar(categories, values, color='skyblue')  # 创建柱状图
plt.xlabel('Category')  # 设置 x 轴标签
plt.ylabel('Value')  # 设置 y 轴标签
plt.title('Bar Chart')  # 设置标题
plt.show()

在这里插入图片描述

实战

import matplotlib.pyplot as plt
import numpy as np

n=12
X=np.arange(n)
Y1=(1-X/float(n)) *np.random.uniform(0.5,1.0,n)
Y2=(1-X/float(n)) *np.random.uniform(0.5,1.0,n)

plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')

for x,y in zip(X,Y1):
    plt.text(x+0.4,y+0.05,'%.2f'% y,ha='center',va='bottom')

for x,y in zip(X,Y2):
    plt.text(x+0.4,-y-0.05,'%.2f'% y,ha='center',va='top')

plt.xlim((-.5,n))
plt.ylim((-1.25,1.25))

plt.xticks(())
plt.yticks(())

plt.show()

在这里插入图片描述


http://www.kler.cn/news/325825.html

相关文章:

  • linux服务器部署filebeat
  • 【30天玩转python】自动化与脚本编写
  • 各种 JIT(Just-In-Time) 编译器
  • Python | Leetcode Python题解之第446题等差数列划分II-子序列
  • 鸿蒙harmonyos next flutter通信之MethodChannel获取设备信息
  • 【Python】Curdling:Python 包管理的高效工具
  • 基于STM32与OpenCV的物料搬运机械臂设计流程
  • 卷积神经网络 循环神经网络
  • 定时任务上云改造方案
  • 【MySQL】常见的SQL优化方式(二)
  • 设计模式 命令模式(Command Pattern)
  • MySQL 索引最左匹配原则详解
  • Element-plus安装及其基础组件使用
  • Python轴承故障诊断 (四)基于EMD-CNN的故障分类
  • 用Python集成免费IP归属地查询API
  • 语言的重定向
  • python select interpreter vscode 配置
  • 深度学习模型可视化工具 Netron 使用教程
  • 主流的消息队列
  • ACM第三次考核题解
  • Docker命令---查看所有创建的容器
  • [Linux]僵尸进程,孤儿进程,环境变量
  • 代码随想录算法训练营Day16
  • Threejs绘制圆锥体
  • 1.8 软件业务测试
  • 试填+组合数学,CF 1648C - Tyler and Strings
  • 【Linux】Linux内核结构基础
  • 缓存池和数据库连接池的使用(Java)
  • Vue 中自定义指令的探索与实践
  • ZYNQ:点亮LED灯