matplotlib画多个子图

想要用matplotlib画多个子图有多种方法,但是如果子图太多,画面就会很混乱,这里给出一种方法,能够使画面在保留必要信息的前提下,尽量简洁,如下图所示。


思路是这样的,在一个fig上先画一张只有坐标轴的空图ax_big:

fig, ax_big = plt.subplots( figsize=(30, 15))

对ax_big 的坐标轴进行偏移、隐藏、以及label设置。

随后再add多个子图,并进行子图的坐标轴设置:

ax = fig.add_subplot(3, 4, i)

这样,最后显示的时候,会把 ax_big 和多个 ax 重叠显示。

代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*

import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl

def draw_picture_11(path_picture):

    # mpl.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
    mpl.rcParams['axes.unicode_minus'] = False      # 解决保存图像是负号'-'显示为方块的问题
    # fig= plt.figure('222', figsize=(15, 15))      # 这是画一张空figure,上面什么都没有
    fig, ax_big = plt.subplots( figsize=(30, 15))   # 这是figure上有一张名叫 ax_big 的空图,上面是有坐标系的
    axis = plt.gca()                                # gca 'get current axes' 获取图像的坐标轴对象
    axis.spines['right'].set_color('none')                  # 隐藏右和上坐标轴
    axis.spines['top'].set_color('none')
    axis.spines['bottom'].set_position(('outward', 30))     # 偏移左和下坐标轴
    axis.spines['left'].set_position(('outward', 30))
    ax_big.set_xticks([])                                   # 隐藏坐标轴刻度
    ax_big.set_yticks([])
    ax_big.set_xlabel('time (s)', fontsize=13, fontstyle='italic')      # 设置字号,斜体
    ax_big.set_ylabel('longitudinal sensor output', fontsize=13, fontstyle='italic')

    for i in range(1,13):               # add_subplot() 不能从0开始
        x = np.linspace(0, 10, 100)
        y = np.sin(x)
        # draw and save the figure
        ax = fig.add_subplot(3, 4, i)
        ax.plot(x, y)
        # ax.set_xticks([0, 5, 10], True)
        ax.set_xticks([0, 5, 10])       # 设置子图的坐标轴刻度
        ax.set_yticks([-2, 0, 2])

    fig.savefig(path_picture + '6666', dpi=300)
    plt.show()

if __name__ == '__main__':
    path_picture = r'G:\TEST\abaqus_text\paper_simulation\picture_file\\'
    draw_picture_11(path_picture)

参考:

https://blog.csdn.net/rumswell/article/details/9862089

https://blog.csdn.net/rumswell/article/details/9862089

https://blog.csdn.net/sinat_36772813/article/details/77160562


猜你喜欢

转载自blog.csdn.net/qintianhaohao/article/details/80607125