Python全系列 教程
3567个小节阅读:5930.3k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
条形图是一种常见的数据可视化工具,它主要用于比较不同类别之间的数据。以下是条形图的主要作用和适用场景:
比较类别之间的大小: 条形图适用于展示不同类别的数据量或大小,通过条形的长度直观地比较它们之间的差异
展示排名和排序: 条形图可以用于显示数据的排名顺序,使观察者能够很容易地识别出最大值、最小值以及中间的排名
展示分布情况: 当你想了解不同类别在整体中所占的比例或分布情况时,条形图是一种有效的选择。每个条形的长度代表相应类别的数据量,总和即为整体的总量
呈现时间趋势: 虽然折线图更适合显示时间趋势,但条形图也可用于呈现时间点上的数据,例如某个特定时间内的销售额或收益
对比多组数据: 类似于折线图,条形图也能用于对比多组数据,但通常是在不同类别上进行比较,而非随时间变化
突出异常值: 条形图可以突出显示数据的异常值,观察者可以很容易地识别出高于或低于平均水平的类别
bar(x,y,color,width) 纵向条形图
barh(x,y,color,height) 横向条形图
xxxxxxxxxx
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fruits = ['苹果', '蓝莓', '草莓', '橘子']
counts = [40, 100, 30, 55]
bar_labels = ['青红色', '蓝色', '红色', '橙色']
bar_colors = ['tab:red', 'tab:blue', 'orangered', 'tab:orange']
container = ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.bar_label(container)
ax.set_ylabel('水果数量')
ax.set_title('按种类和颜色划分的水果供应')
ax.legend(title='水果颜色')
xxxxxxxxxx
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fruits = ['苹果', '蓝莓', '草莓', '橘子']
counts = [40, 100, 30, 55]
bar_labels = ['青红色', '蓝色', '红色', '橙色']
bar_colors = ['tab:red', 'tab:blue', 'orangered', 'tab:orange']
container = ax.barh(fruits, counts, label=bar_labels, color=bar_colors)
ax.bar_label(container)
ax.set_xlabel('水果数量')
ax.set_title('按种类和颜色划分的水果供应')
ax.legend(title='水果颜色')
xxxxxxxxxx
import matplotlib.pyplot as plt
species = ("苹果","蓝莓","橘子")
weight_counts = {
"第1季度": [70, 39, 58],
"第2季度": [82, 37, 66],
}
x = range(len(species))
x1 =[_+width for _ in x]
width = 0.2
container = plt.bar(x, weight_counts["第1季度"],color='tab:red',label='第1季度',width=width)
plt.bar_label(container)
container2 = plt.bar(x1, weight_counts["第2季度"],color='tab:blue',label='第2季度',width=width)
plt.bar_label(container2)
plt.xticks([_+width/2 for _ in x], species)
plt.legend(loc=1)
xxxxxxxxxx
import matplotlib.pyplot as plt
species = ("苹果","蓝莓","橘子")
weight_counts = {
"第1季度": [70, 39, 58],
"第2季度": [82, 37, 66],
}
x = range(len(species))
container = plt.bar(x, weight_counts["第1季度"],color='tab:red',label='第1季度')
plt.bar_label(container,label_type='center')
container2 = plt.bar(x, weight_counts["第2季度"],color='tab:blue',label='第2季度',bottom=weight_counts["第1季度"])
plt.bar_label(container2,label_type='center')
plt.xticks([_+width/2 for _ in x], species)
plt.legend(loc="upper right")
实时学习反馈
1. 条形图最适合用于展示以下哪种情况?
A 时间趋势的变化
B 不同类别之间的大小比较
C 单一类别在不同时间点的数据对比
D 呈现数据的周期性变化
答案
1=>B