Matplotlib入門シリーズ4_一度に複数のサブマップを作成

3247 ワード

2019 7 15 14:45:27

環境
Python3.6

前言
Matplotlib       

1、  
2、   plt  
3、       
4、        
5、    
6、  Matplotlib       

       ,      ,            。
         ,            ,         ,     。

                   。
	          ,              ,             。
  :
        https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

一度に複数のサブマップを作成
主に柱状図、折れ線図、散点図、餅状図の生成に用いられる
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   @File:          4、         .py
   @Author :        hyh
   @Ide:            Pycharm
   @Contact:        [email protected]
   @Date:          19-7-15   11:11
   
-------------------------------------------------
   Description :

-------------------------------------------------
"""

import matplotlib.pyplot as plt
import numpy as np

"""
   :
    plt.bar(names, values)
   :
    plt.scatter(names, values)
   :
    plt.plot(names, values)
   :
    

"""


def plt_sub_one():
    names = ['group_a', 'group_b', 'group_c']
    values = [1, 10, 100]

    plt.figure(figsize=(9, 3))

    plt.subplot(131)
    plt.bar(names, values)  #    
    plt.subplot(132)
    plt.scatter(names, values)  #    
    plt.subplot(133)
    plt.plot(names, values)  #    
    plt.suptitle('Categorical Plotting')
    plt.show()


"""
       :
        :https://matplotlib.org/gallery/pie_and_polar_charts/pie_features.html
"""


def ply_pie():
    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

    plt.show()


def f(t):
    return np.exp(-t) * np.cos(2 * np.pi * t)


"""
    plt     

"""


def plt_sub_two():
    t1 = np.arange(0.0, 5.0, 0.1)
    t2 = np.arange(0.0, 5.0, 0.02)

    plt.figure()
    plt.subplot(211)
    plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

    plt.subplot(212)
    plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')
    plt.show()


"""
           ,     
"""


def plt_sub_three():
    plt.figure(1)  # the first figure
    plt.subplot(211)  # the first subplot in the first figure
    plt.plot([1, 2, 3])
    plt.subplot(212)  # the second subplot in the first figure
    plt.plot([4, 5, 6])

    plt.figure(2)  # a second figure
    plt.plot([4, 5, 6])  # creates a subplot(111) by default

    plt.figure(1)  # figure 1 current; subplot(212) still current
    plt.subplot(211)  # make subplot(211) in figure1 current
    plt.title('Easy as 1, 2, 3')  # subplot 211 title


if __name__ == '__main__':
    plt_sub_one()
    ply_pie()

    plt_sub_two()
    # plt_sub_three()