# Matplotlib数据可视化进阶（二）

+关注继续查看

## 1.4 直方图

🚩绘制的直方图其实就是一个概率分布，直方图可以看成很多个柱子的柱状图

import numpy as np
import matplotlib.pyplot as plt

mu = 100 # 平均值
sigma = 15 # 标准差
x = np.random.normal(loc = mu, scale = 15, size = 10000)
fig, ax = plt.subplots()

# 直方图一般用于描述统计性的数据
# 数据量比较大,通过绘制直方图,看出数据内部的关系
# density = True 统计的是概率
# density = False 统计数字在某个范围内的次数
n, bins, patches = ax.hist(x, 200, density = True) # 直方图

# 概率密度函数
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
np.exp(-0.5 * (1 / sigma * (bins - mu)) ** 2))
plt.plot(bins, y, '--')
plt.xlabel('Smarts')
plt.ylabel('Probability density')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

# 紧凑布局
fig.tight_layout()

## 1.5 箱形图

import numpy as np
import matplotlib.pyplot as plt

# 正态分布
data = np.random.normal(size = (500, 4))
lables = ['A', 'B', 'C', 'D']
# 用Matplotlib画箱线图
# 黄色的线就是中位数,红色的圆点是异常值
_ = plt.boxplot(data, 1, 'ro', labels = lables) 

## 1.6 散点图

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100, 2)
s = np.random.randint(100, 300, size = 100)
color = np.random.randn(100)

plt.scatter(data[:, 0], # 横坐标
data[:, 1], # 纵坐标
s = s, # 尺寸
c = color, # 颜色
alpha = 0.5) # 透明度

## 1.7 饼图

### 1.7.1 一般饼图

import numpy as np
import matplotlib.pyplot as plt

# 解决中文字体乱码的问题
plt.rcParams['font.sans-serif'] = 'KaiTi'

labels = ["五星", "四星", "三星", "二星", "一星"] # 标签
percent = [95, 261, 105, 30, 9] # 某市星级酒店数量
# 设置图片大小和分辨率
fig = plt.figure(figsize = (5, 5), dpi = 120)
# 偏移中心量，突出某一部分
# 0.1 表示 10%,自身高度的10%,是一个相对值
explode = (0, 0.1, 0, 0, 0)

_ = plt.pie(x = percent,         # 数据
explode = explode,   # 偏移中心量
labels = labels,     # 显示标签
autopct = '%0.1f%%', # 显示百分比
shadow = True)      # 阴影，3D效果

### 1.7.2 甜甜圈

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize = (6, 6))
# 甜甜圈原料
recipe = ["225g flour",
"90g sugar",
"1 egg",
"60g butter",
"100ml milk",
"1/2package of yeast"]
# 原料比例
data = [225, 90, 50, 60, 100, 5]
wedges, texts = plt.pie(data, startangle = 40)
bbox_props = dict(boxstyle = "square,pad=0.3", fc = "w", ec = "k", lw = 0.72)
kw = dict(arrowprops = dict(arrowstyle = "-"),
bbox = bbox_props, va = "center")

for i, p in enumerate(wedges):
ang = (p.theta2 - p.theta1) / 2. + p.theta1 # 角度计算
# 角度转弧度----->弧度转坐标
ha = {-1 :  "right", 1 : "left"}[int(np.sign(x))] # 水平对齐方式
connectionstyle = "angle,angleA=0,angleB={}".format(ang) # 箭头连接样式
kw["arrowprops"].update({"connectionstyle" : connectionstyle}) # 更新箭头连接方式
plt.annotate(recipe[i], xy=(x, y), xytext = (1.35 * np.sign(x), 1.4 * y),
ha = ha, **kw, fontsize = 18, weight = 'bold')
plt.title("Matplotlib bakery: A donut", fontsize = 18, pad = 25)
plt.tight_layout()

## 1.8 热力图

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# 标签
vegetables = ["cucumber", "tomato", "lettuce", "asparagus", "potato", "wheat", "barley"]
farmers = list('ABCDEFG')

# 创建数据,随机数
harvest = np.random.randn(7, 7) * 5 # 农民丰收数据

plt.rcParams['font.size'] = 18
plt.rcParams['font.weight'] = 'heavy'
plt.figure(figsize = (9, 9))
# imshow() 显示图片,因为数值不同,所以图片颜色不同
im = plt.imshow(harvest)

plt.xticks(np.arange(len(farmers)), farmers, rotation = 45, ha = 'right')
plt.yticks(np.arange(len(vegetables)), vegetables)

# 绘制文本
for i in range(len(vegetables)):
for j in range(len(farmers)):
text = plt.text(j, i, round(harvest[i, j], 1),
ha = "center", va = "center", color = 'r')

plt.title("Harvest of local farmers (in tons/year)", pad = 20)

## 1.9 面积图

import matplotlib.pyplot as plt

plt.figure(figsize = (9, 6))
days = [1, 2, 3, 4, 5]
sleeping = [7, 8, 6, 11, 7]
eating = [2, 3, 4, 3, 2]
working = [7, 8, 7, 2, 2]
playing = [8, 5, 7, 8, 13]
plt.stackplot(days, sleeping, eating, working, playing)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Stack Plot', fontsize = 18)
plt.legend(['Sleeping', 'Eating', 'Working', 'Playing'],
fontsize = 18)

## 1.10 蜘蛛图

import numpy as np
import matplotlib.pyplot as plt

# 画图数据
plt.rcParams['font.family'] = 'KaiTi'
labels = np.array(["个人能力", "IQ", "服务意识",
"团队精神", "解决问题能力", "持续学习"])
y = [83, 61, 95, 67, 76, 88]
x = np.linspace(0, 2 * np.pi, len(labels), endpoint = False)

# 首位相接,我们要把 "持续学习" 和 "个人能力相连起来"
y = np.concatenate((y, [y[0]]))  # 首尾相接
x = np.concatenate((x, [x[0]]))  # 首尾相接

# 用Matplotlib画蜘蛛图
fig = plt.figure(figsize = (6, 6))
ax = fig.add_subplot(111, polar = True)
# 连线
# o:表示形状，圆形
# -:表示实线
# o-:属性连用
ax.plot(x, y, 'o-', linewidth = 2)
ax.fill(x, y, alpha = 0.25) # 填充颜色
# 设置角度
ax.set_thetagrids(x[:-1] * 180 / np.pi,# 角度值
# 由于首位相接时候相当于给x增加了一个元素,现在需要切片去掉这个元素
labels,
fontsize = 18)
_ = ax.set_rgrids([20, 40, 60, 80], fontsize = 18)

8 0

18 0
Matplotlib数据可视化入门（一）

50 0
Matplotlib数据可视化高级（二）

39 0
Matplotlib数据可视化高级（四）

47 0

45 0
Matplotlib进阶

1048 0
Python3入门机器学习 - 数据可视化基础matplotlib

719 0

2273 0
+关注

CSDN人工智能领域优质创作者，华为云&middot;云享专家，CCF-TYUT President-designate

WebGL在数据可视化中的实践