数据可视化---柱状图

import matplotlib.pyplot as plt
import numpy as np

def plot_bar_chart(data, labels, colors=None, title="Bar Chart", xlabel="X-Axis", ylabel="Y-Axis"):
    """
    绘制柱状图,并在柱子上显示数量和比例。
    
    :param data: 包含数值的列表。
    :param labels: 与数据相对应的标签列表。
    :param colors: 柱子的颜色列表。
    :param title: 图表的标题。
    :param xlabel: X轴的标签。
    :param ylabel: Y轴的标签。
    """
    fig, ax = plt.subplots()
    bars = plt.bar(labels, data, color=colors)
    
    total = sum(data)
    for bar in bars:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width()/2, yval, f'{yval}\n({yval/total:.1%})', ha='center', va='bottom')
    
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.show()

# 示例数据 
simple_data = [15, 30, 45, 10] 
simple_labels = ['A', 'B', 'C', 'D'] 
simple_colors = ['red', 'green', 'blue', 'orange'] 
grouped_data = [[12, 30, 1, 8], [28, 6, 16, 5]] 
group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4'] 
category_labels = ['Category A', 'Category B'] 
# 绘制图表 
plot_bar_chart(simple_data, simple_labels, simple_colors, title="Simple Bar Chart", xlabel="Category",ylabel="Values")

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

def plot_grouped_bar_chart(data, group_labels, category_labels, colors=None, title="Grouped Bar Chart", xlabel="X-Axis", ylabel="Y-Axis"):
    """
    绘制分组柱状图,并在柱子上显示数量和比例。
    
    :param data: 一个二维列表,其中每个内部列表包含同一组的数据。
    :param group_labels: 每组的标签列表。
    :param category_labels: 类别标签列表。
    :param colors: 每个类别的柱子颜色列表。
    :param title: 图表的标题。
    :param xlabel: X轴的标签。
    :param ylabel: Y轴的标签。
    """
    n_groups = len(data[0])
    n_categories = len(data)

    fig, ax = plt.subplots()
    index = np.arange(n_groups)
    bar_width = 0.35
    opacity = 0.8

    if not colors or len(colors) != n_categories:
        colors = plt.cm.viridis(np.linspace(0, 1, n_categories))

    total = np.sum(data, axis=0)
    for i in range(n_categories):
        bars = plt.bar(index + i * bar_width, data[i], bar_width, alpha=opacity, color=colors[i], label=category_labels[i])
        
        for bar, total_val in zip(bars, total):
            yval = bar.get_height()
            plt.text(bar.get_x() + bar.get_width()/2, yval, f'{yval/total_val:.1%}', ha='center', va='bottom')

    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.xticks(index + bar_width / 2, group_labels)
    plt.legend()

    plt.tight_layout()
    plt.show()

# 示例数据 
grouped_data = [[12, 30, 1, 8], [28, 6, 16, 5]] 
group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4'] 
category_labels = ['Category A', 'Category B'] 

# 绘制图表 
plot_grouped_bar_chart(grouped_data, group_labels, category_labels, title="Grouped Bar Chart", xlabel="Groups",ylabel="Values")

在这里插入图片描述


http://www.niftyadmin.cn/n/5267822.html

相关文章

KL散度、CrossEntropy详解

文章目录 0. 概述1. 信息量1.1 定义1.2 性质1.3 例子2. 熵 Entropy2.1 定义2.2 公式2.3 例子3. 交叉熵 Cross Entropy3.1 定义3.2 公式3.3 例子4. KL 散度(相对熵)4.1 公式

【JavaScript 】JavaScript 数组对象操作数组的方法(很全带示例)

JavaScript 数组对象操作数组的方法 1. push() - 向数组的末尾添加一个或多个元素,并返回新的长度。2. pop() - 删除并返回数组的最后一个元素。3. shift() - 删除并返回数组的第一个元素。4. unshift() - 向数组的开头添加一个或多个元素,并返回新的长度…

ETLCloud的应用策略——实时数据处理是关键

一、ETLCloud是什么? ETLCloud又称数据集成(DataOps),是RestCloud旗下的一款数据仓库管理工具,通过自动化数据转换和集成来实现企业内部和外部数据的无缝对接,从而帮助企业快速获取准确的数据信息&#xff…

显示器件是什么

显示器件 电子元器件百科 文章目录 显示器件前言一、显示器件是什么二、显示器件的类别三、显示器件的应用实例四、显示器件的作用原理总结前言 显示器件根据不同的技术原理和应用领域,具有不同的特点和优势,可适用于电子产品、电视、计算机显示器、手持设备、汽车仪表盘等…

C语言写的 mini版的 http 服务器 , 很详细

文章目录 效果展示整体架构流程技术细节完整代码 效果展示 例如:htpp://192.168.23.140/home.html -> 正确的请求格式 home.html 这个资源是放在我们服务器里面的 , 并不是随便访问的资源,当然我们可以放很多的资源进去. 整体架构流程 整个实现的流…

TrustGeo代码理解(一)main.py

代码链接:https://github.com/ICDM-UESTC/TrustGeo 一、导入各种模块和数据库 # -*- coding: utf-8 -*- import torch.nnfrom lib.utils import * import argparse, os import numpy as np import random from lib.model import * import copy from thop import profile imp…

企业计算机服务器中了halo勒索病毒怎么解密,勒索病毒解密数据恢复

在网络技术飞速发展的今天,越来越多的企业开始意识到企业数据安全的重要性,很多企业都会依赖数字化办公系统软件,并且通过系统软件将企业的重要数据存储在数据库中,为企业的生产运营提供了极大便利,但网络威胁一直存在…

华为OD机试真题-字符串变换最小字符串-2023年OD统一考试(C卷)

题目描述: 给定一个字符串s,最多只能进行一次变换,返回变换后能得到的最小字符串(按照字典序进行比较)。变换规则:交换字符串中任意两个不同位置的字符。 输入描述:一串小写字母组成的字符串s 输出描述:按照要求进行变换得到的最小字符串 补充说明:s是都是小写字符组成…