Python 数据分析——Matplotlib相关知识

Python 数据分析——Matplotlib相关知识

第三章 Matplotlib相关知识 —— 布局格式



前言

这次内容主要是设计Matplotlib的布局格式绘制,了解布局可以更好的绘制所需要的可视化图。

  • 导入
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']   
plt.rcParams['axes.unicode_minus'] = False  

一、子图

1. 使用 plt.subplots 绘制均匀状态下的子图

  • 主要参数
figsize指定整个画布的大小
sharex表示是否共享横轴(x轴)
sharey 表示是否共享纵轴(y轴)
tight_layout 函数调整子图的相对大小使字符不会重叠

具体例子如下:

fig, axs = plt.subplots(3, 4, figsize=(12, 8), sharex=False, sharey=False) # "3"和"4"分别代表行和列,figsize为子图大小。

# 随机生成变量 
# "i"和"j"的范围应与行和列的值一致
for i in range(3):
    for j in range(4):
        axs[i][j].scatter(np.random.randn(15), np.random.randn(15))
        axs[i][j].set_title('第%d行,第%d列'%(i+1,j+1))
        # 坐标轴的范围
        axs[i][j].set_xlim(-5,5)
        axs[i][j].set_ylim(-5,5)
        if i in [0,1,2]: axs[i][j].set_xlabel('横坐标')
        if j==0: axs[i][j].set_ylabel('纵坐标')
fig.tight_layout()

在这里插入图片描述

注:这类图我个人觉得横纵坐标不共享会比较容易观察数据


  • 另一种表示方式

基于pyplot模式的写法,每次在指定位置新建一个子图,并且之后的绘图操作都会指向当前子图

plt.figure()
# 子图1
plt.subplot(2,2,1) 
plt.plot([1,2], 'r')
# 子图2
plt.subplot(2,2,2)
plt.plot([1,2], 'b')

# 当三位数都小于10时,可以省略中间的逗号
# 子图3
plt.subplot(223) # (223) 等价于 (2,2,3)
plt.plot([1,2], 'y')
# 子图4
plt.subplot(224)
plt.plot([1,2], 'g');

在这里插入图片描述


  • 除了常规的直角坐标系,也可以通过projection方法创建极坐标系下的图表
N = 50
r = 2 * np.random.rand(N) # r半径对应高度
theta = 2 * np.pi * np.random.rand(N) # theta角度对应位置
area = 100 * r**2
colors = theta # 随机产生颜色


plt.subplot(projection='polar')
plt.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75);

在这里插入图片描述


2. 使用 GridSpec 绘制非均匀子图

所谓非均匀包含两层含义,第一是指图的比例大小不同但没有跨行或跨列,第二是指图为跨列或跨行状态
利用 add_gridspec 可以指定相对宽度比例 width_ratios 和相对高度比例参数 height_ratios

  • 比如: fig.add_gridspec(nrows=2, ncols=5, width_ratios=[1,2,3,4,5], height_ratios=[1,3])
  • width_ratios=[1,2,3,4,5]:宽度比例。列表里的元素数应当与列数相同,代表各列的宽度比例。例如,ncols=5,那么list中应当有5个元素。
  • height_ratios=[1,3]:高度比例。列表里的元素数应当与行数相等,代表各行的高度比例。例如nrows=2,那么list里应该有两个元素。
fig = plt.figure(figsize=(10,8))
spec = fig.add_gridspec(nrows=3, ncols=4, width_ratios=[1,2,3,4,], height_ratios=[1,3,5])
for i in range(3):
    for j in range(4):
        ax = fig.add_subplot(spec[i, j])
        ax.scatter(np.random.randn(10), np.random.randn(10))
        ax.set_title('第%d行,第%d列'%(i+1,j+1))
        if i in [0,1,2]: ax.set_xlabel('横坐标')
        if j==0: ax.set_ylabel('纵坐标')
fig.tight_layout()

在这里插入图片描述


  • 在上面的例子中出现了 spec[i, j] 的用法,事实上通过切片就可以实现子图的合并而达到跨图的共能
fig = plt.figure(figsize=(10, 6))
spec = fig.add_gridspec(nrows=3, ncols=5, width_ratios=[2,2.5,3,1,2], height_ratios=[1,3,4]) # 3行5列 
# sub1
ax = fig.add_subplot(spec[0,0]) # 第1行第1列
ax.scatter(np.random.randn(10), np.random.randn(10))
# sub2
ax = fig.add_subplot(spec[0, 2:4]) # 第1行 第3、4列
ax.scatter(np.random.randn(10), np.random.randn(10))
# sub3
ax = fig.add_subplot(spec[:2, 4]) # 前2行第5列
ax.scatter(np.random.randn(10), np.random.randn(10))
# sub4
ax = fig.add_subplot(spec[1, :4]) # 第2行前4列
ax.scatter(np.random.randn(10), np.random.randn(10))
# sub5
ax = fig.add_subplot(spec[2, :5]) # 第3行
ax.scatter(np.random.randn(10), np.random.randn(10))
fig.tight_layout()

在这里插入图片描述


二、子图上的方法

补充介绍一些子图上的方法

常用直线的画法为: axhline, axvline, axline (水平、垂直、任意方向)

fig, ax = plt.subplots(figsize=(4,3))
ax.axhline(0.5,0.2,0.8)
ax.axvline(0.5,0.2,0.8)
ax.axline([0.3,0.3],[0.7,0.7]);

在这里插入图片描述


  • 使用 grid 可以加灰色网格
fig, ax = plt.subplots(figsize=(5,3))
ax.grid() # 默认为True

在这里插入图片描述

fig, ax = plt.subplots(figsize=(5,3))
ax.grid(False) # False的情况

在这里插入图片描述


  • 使用 set_xscale 可以设置坐标轴的规度(指对数坐标等)
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
for j in range(2):
    axs[j].plot(list('abcd'), [10**i for i in range(4)])
    if j == 0:
        axs[j].set_yscale('log')
        axs[j].set_title('对数')
        axs[j].set_ylabel('对数坐标')
    else:
        axs[j].set_title('原函数')
        axs[j].set_ylabel('原函数坐标')
fig.tight_layout()

在这里插入图片描述


参考

Datawhale🐳数据可视化小组开源项目:《Fantastic-Matplotlib》

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值