본문 바로가기
Programming Languages/Python

Chapter 17. Matplotlib

by 더 이프 2023. 4. 5.
728x90

목차

    1. matplotlib

    Python 프로그래밍 언어 및 Numpy 라이브러리를 활용한 플로팅 라이브러리입니다. 범용 GUI 툴킷을 사용하여 애플리케이션에 플롯을 포함하기 위한 객체 지향 API를 제공합니다.

    a. linear equation

    _, axe = plt.subplots()
    x = np.arange(10)
    y = x*5+10
    axe.plot(x,y)

    b. quadratic equation

    _, axe = plt.subplots()
    x = np.linspace(2,4,10)
    y = 2*x*x+5
    axe.plot(x,y)

    c. normal distribution

    _, axe = plt.subplots()
    x = np.linspace(-5,5,100)
    mu = 0
    sig = 1
    pi = np.pi
    y = (1/(sig*np.sqrt(2*pi)))*(np.exp(-((x-mu)**2)/(2*sig**2)))
    axe.plot(x,y)

    d. error function

    _, axe = plt.subplots()
    x = np.linspace(-5,5,100)
    mu = 0
    sig = 1
    y=[]
    for v in x:
        a = (1 + math.erf((v-mu)/(sig*np.sqrt(2))))*(1/2)
        y.append(a)
    axe.plot(x,y)

     

    2. Example

    a. iris

    import pickle
    import numpy as np
    import matplotlib.pyplot as plt
    
    with open('iris_ndarray.pickle','rb') as f:
        iris_np = pickle.load(f)
    print(iris_np)
    sepal_length = iris_np[:,0]
    sepal_width = iris_np[:,1]
    petal_length = iris_np[:,2]
    petal_width = iris_np[:,3]
    
    # 결과값
    [[5.1 3.5 1.4 0.2 0. ]
     [4.9 3.  1.4 0.2 0. ]
     [4.7 3.2 1.3 0.2 0. ]
     [4.6 3.1 1.5 0.2 0. ]
     [5.  3.6 1.4 0.2 0. ]
     [5.4 3.9 1.7 0.4 0. ]
     [4.6 3.4 1.4 0.3 0. ]
     [5.  3.4 1.5 0.2 0. ]
     [4.4 2.9 1.4 0.2 0. ]
     [4.9 3.1 1.5 0.1 0. ]
     [5.4 3.7 1.5 0.2 0. ]
     [4.8 3.4 1.6 0.2 0. ]
     [4.8 3.  1.4 0.1 0. ]
     [4.3 3.  1.1 0.1 0. ]
     [5.8 4.  1.2 0.2 0. ]
     [5.7 4.4 1.5 0.4 0. ]
     [5.4 3.9 1.3 0.4 0. ]
     [5.1 3.5 1.4 0.3 0. ]
     [5.7 3.8 1.7 0.3 0. ]
     [5.1 3.8 1.5 0.3 0. ]
     [5.4 3.4 1.7 0.2 0. ]
     [5.1 3.7 1.5 0.4 0. ]
     [4.6 3.6 1.  0.2 0. ]
     [5.1 3.3 1.7 0.5 0. ]
     [4.8 3.4 1.9 0.2 0. ]
     [5.  3.  1.6 0.2 0. ]
     [5.  3.4 1.6 0.4 0. ]
     [5.2 3.5 1.5 0.2 0. ]
     [5.2 3.4 1.4 0.2 0. ]
     [4.7 3.2 1.6 0.2 0. ]
     [4.8 3.1 1.6 0.2 0. ]
     [5.4 3.4 1.5 0.4 0. ]
     [5.2 4.1 1.5 0.1 0. ]
     [5.5 4.2 1.4 0.2 0. ]
     [4.9 3.1 1.5 0.1 0. ]
     [5.  3.2 1.2 0.2 0. ]
     [5.5 3.5 1.3 0.2 0. ]
     [4.9 3.1 1.5 0.1 0. ]
     [4.4 3.  1.3 0.2 0. ]
     [5.1 3.4 1.5 0.2 0. ]
     [5.  3.5 1.3 0.3 0. ]
     [4.5 2.3 1.3 0.3 0. ]
     [4.4 3.2 1.3 0.2 0. ]
     [5.  3.5 1.6 0.6 0. ]
     [5.1 3.8 1.9 0.4 0. ]
     [4.8 3.  1.4 0.3 0. ]
     [5.1 3.8 1.6 0.2 0. ]
     [4.6 3.2 1.4 0.2 0. ]
     [5.3 3.7 1.5 0.2 0. ]
     [5.  3.3 1.4 0.2 0. ]
     [7.  3.2 4.7 1.4 1. ]
     [6.4 3.2 4.5 1.5 1. ]
     [6.9 3.1 4.9 1.5 1. ]
     [5.5 2.3 4.  1.3 1. ]
     [6.5 2.8 4.6 1.5 1. ]
     [5.7 2.8 4.5 1.3 1. ]
     [6.3 3.3 4.7 1.6 1. ]
     [4.9 2.4 3.3 1.  1. ]
     [6.6 2.9 4.6 1.3 1. ]
     [5.2 2.7 3.9 1.4 1. ]
     [5.  2.  3.5 1.  1. ]
     [5.9 3.  4.2 1.5 1. ]
     [6.  2.2 4.  1.  1. ]
     [6.1 2.9 4.7 1.4 1. ]
     [5.6 2.9 3.6 1.3 1. ]
     [6.7 3.1 4.4 1.4 1. ]
     [5.6 3.  4.5 1.5 1. ]
     [5.8 2.7 4.1 1.  1. ]
     [6.2 2.2 4.5 1.5 1. ]
     [5.6 2.5 3.9 1.1 1. ]
     [5.9 3.2 4.8 1.8 1. ]
     [6.1 2.8 4.  1.3 1. ]
     [6.3 2.5 4.9 1.5 1. ]
     [6.1 2.8 4.7 1.2 1. ]
     [6.4 2.9 4.3 1.3 1. ]
     [6.6 3.  4.4 1.4 1. ]
     [6.8 2.8 4.8 1.4 1. ]
     [6.7 3.  5.  1.7 1. ]
     [6.  2.9 4.5 1.5 1. ]
     [5.7 2.6 3.5 1.  1. ]
     [5.5 2.4 3.8 1.1 1. ]
     [5.5 2.4 3.7 1.  1. ]
     [5.8 2.7 3.9 1.2 1. ]
     [6.  2.7 5.1 1.6 1. ]
     [5.4 3.  4.5 1.5 1. ]
     [6.  3.4 4.5 1.6 1. ]
     [6.7 3.1 4.7 1.5 1. ]
     [6.3 2.3 4.4 1.3 1. ]
     [5.6 3.  4.1 1.3 1. ]
     [5.5 2.5 4.  1.3 1. ]
     [5.5 2.6 4.4 1.2 1. ]
     [6.1 3.  4.6 1.4 1. ]
     [5.8 2.6 4.  1.2 1. ]
     [5.  2.3 3.3 1.  1. ]
     [5.6 2.7 4.2 1.3 1. ]
     [5.7 3.  4.2 1.2 1. ]
     [5.7 2.9 4.2 1.3 1. ]
     [6.2 2.9 4.3 1.3 1. ]
     [5.1 2.5 3.  1.1 1. ]
     [5.7 2.8 4.1 1.3 1. ]
     [6.3 3.3 6.  2.5 2. ]
     [5.8 2.7 5.1 1.9 2. ]
     [7.1 3.  5.9 2.1 2. ]
     [6.3 2.9 5.6 1.8 2. ]
     [6.5 3.  5.8 2.2 2. ]
     [7.6 3.  6.6 2.1 2. ]
     [4.9 2.5 4.5 1.7 2. ]
     [7.3 2.9 6.3 1.8 2. ]
     [6.7 2.5 5.8 1.8 2. ]
     [7.2 3.6 6.1 2.5 2. ]
     [6.5 3.2 5.1 2.  2. ]
     [6.4 2.7 5.3 1.9 2. ]
     [6.8 3.  5.5 2.1 2. ]
     [5.7 2.5 5.  2.  2. ]
     [5.8 2.8 5.1 2.4 2. ]
     [6.4 3.2 5.3 2.3 2. ]
     [6.5 3.  5.5 1.8 2. ]
     [7.7 3.8 6.7 2.2 2. ]
     [7.7 2.6 6.9 2.3 2. ]
     [6.  2.2 5.  1.5 2. ]
     [6.9 3.2 5.7 2.3 2. ]
     [5.6 2.8 4.9 2.  2. ]
     [7.7 2.8 6.7 2.  2. ]
     [6.3 2.7 4.9 1.8 2. ]
     [6.7 3.3 5.7 2.1 2. ]
     [7.2 3.2 6.  1.8 2. ]
     [6.2 2.8 4.8 1.8 2. ]
     [6.1 3.  4.9 1.8 2. ]
     [6.4 2.8 5.6 2.1 2. ]
     [7.2 3.  5.8 1.6 2. ]
     [7.4 2.8 6.1 1.9 2. ]
     [7.9 3.8 6.4 2.  2. ]
     [6.4 2.8 5.6 2.2 2. ]
     [6.3 2.8 5.1 1.5 2. ]
     [6.1 2.6 5.6 1.4 2. ]
     [7.7 3.  6.1 2.3 2. ]
     [6.3 3.4 5.6 2.4 2. ]
     [6.4 3.1 5.5 1.8 2. ]
     [6.  3.  4.8 1.8 2. ]
     [6.9 3.1 5.4 2.1 2. ]
     [6.7 3.1 5.6 2.4 2. ]
     [6.9 3.1 5.1 2.3 2. ]
     [5.8 2.7 5.1 1.9 2. ]
     [6.8 3.2 5.9 2.3 2. ]
     [6.7 3.3 5.7 2.5 2. ]
     [6.7 3.  5.2 2.3 2. ]
     [6.3 2.5 5.  1.9 2. ]
     [6.5 3.  5.2 2.  2. ]
     [6.2 3.4 5.4 2.3 2. ]
     [5.9 3.  5.1 1.8 2. ]]
    sepal_ratio = np.divide(sepal_length,sepal_width)
    petal_ratio = np.divide(petal_length,petal_width)
    
    print(sepal_ratio.shape)
    sepal_ratio_2d = np.expand_dims(sepal_ratio,1)
    print(sepal_ratio_2d.shape)
    
    print(petal_ratio.shape)
    petal_ratio_2d = petal_ratio[:,np.newaxis]
    print(petal_ratio_2d.shape)
    
    concat_result = np.concatenate((sepal_ratio_2d,petal_ratio_2d),axis=1)
    print(concat_result)
    
    # 결과값
    (150,)
    (150, 1)
    (150,)
    (150, 1)
    [[ 1.45714286  7.        ]
     [ 1.63333333  7.        ]
     [ 1.46875     6.5       ]
     [ 1.48387097  7.5       ]
     [ 1.38888889  7.        ]
     [ 1.38461538  4.25      ]
     [ 1.35294118  4.66666667]
     [ 1.47058824  7.5       ]
     [ 1.51724138  7.        ]
     [ 1.58064516 15.        ]
     [ 1.45945946  7.5       ]
     [ 1.41176471  8.        ]
     [ 1.6        14.        ]
     [ 1.43333333 11.        ]
     [ 1.45        6.        ]
     [ 1.29545455  3.75      ]
     [ 1.38461538  3.25      ]
     [ 1.45714286  4.66666667]
     [ 1.5         5.66666667]
     [ 1.34210526  5.        ]
     [ 1.58823529  8.5       ]
     [ 1.37837838  3.75      ]
     [ 1.27777778  5.        ]
     [ 1.54545455  3.4       ]
     [ 1.41176471  9.5       ]
     [ 1.66666667  8.        ]
     [ 1.47058824  4.        ]
     [ 1.48571429  7.5       ]
     [ 1.52941176  7.        ]
     [ 1.46875     8.        ]
     [ 1.5483871   8.        ]
     [ 1.58823529  3.75      ]
     [ 1.26829268 15.        ]
     [ 1.30952381  7.        ]
     [ 1.58064516 15.        ]
     [ 1.5625      6.        ]
     [ 1.57142857  6.5       ]
     [ 1.58064516 15.        ]
     [ 1.46666667  6.5       ]
     [ 1.5         7.5       ]
     [ 1.42857143  4.33333333]
     [ 1.95652174  4.33333333]
     [ 1.375       6.5       ]
     [ 1.42857143  2.66666667]
     [ 1.34210526  4.75      ]
     [ 1.6         4.66666667]
     [ 1.34210526  8.        ]
     [ 1.4375      7.        ]
     [ 1.43243243  7.5       ]
     [ 1.51515152  7.        ]
     [ 2.1875      3.35714286]
     [ 2.          3.        ]
     [ 2.22580645  3.26666667]
     [ 2.39130435  3.07692308]
     [ 2.32142857  3.06666667]
     [ 2.03571429  3.46153846]
     [ 1.90909091  2.9375    ]
     [ 2.04166667  3.3       ]
     [ 2.27586207  3.53846154]
     [ 1.92592593  2.78571429]
     [ 2.5         3.5       ]
     [ 1.96666667  2.8       ]
     [ 2.72727273  4.        ]
     [ 2.10344828  3.35714286]
     [ 1.93103448  2.76923077]
     [ 2.16129032  3.14285714]
     [ 1.86666667  3.        ]
     [ 2.14814815  4.1       ]
     [ 2.81818182  3.        ]
     [ 2.24        3.54545455]
     [ 1.84375     2.66666667]
     [ 2.17857143  3.07692308]
     [ 2.52        3.26666667]
     [ 2.17857143  3.91666667]
     [ 2.20689655  3.30769231]
     [ 2.2         3.14285714]
     [ 2.42857143  3.42857143]
     [ 2.23333333  2.94117647]
     [ 2.06896552  3.        ]
     [ 2.19230769  3.5       ]
     [ 2.29166667  3.45454545]
     [ 2.29166667  3.7       ]
     [ 2.14814815  3.25      ]
     [ 2.22222222  3.1875    ]
     [ 1.8         3.        ]
     [ 1.76470588  2.8125    ]
     [ 2.16129032  3.13333333]
     [ 2.73913043  3.38461538]
     [ 1.86666667  3.15384615]
     [ 2.2         3.07692308]
     [ 2.11538462  3.66666667]
     [ 2.03333333  3.28571429]
     [ 2.23076923  3.33333333]
     [ 2.17391304  3.3       ]
     [ 2.07407407  3.23076923]
     [ 1.9         3.5       ]
     [ 1.96551724  3.23076923]
     [ 2.13793103  3.30769231]
     [ 2.04        2.72727273]
     [ 2.03571429  3.15384615]
     [ 1.90909091  2.4       ]
     [ 2.14814815  2.68421053]
     [ 2.36666667  2.80952381]
     [ 2.17241379  3.11111111]
     [ 2.16666667  2.63636364]
     [ 2.53333333  3.14285714]
     [ 1.96        2.64705882]
     [ 2.51724138  3.5       ]
     [ 2.68        3.22222222]
     [ 2.          2.44      ]
     [ 2.03125     2.55      ]
     [ 2.37037037  2.78947368]
     [ 2.26666667  2.61904762]
     [ 2.28        2.5       ]
     [ 2.07142857  2.125     ]
     [ 2.          2.30434783]
     [ 2.16666667  3.05555556]
     [ 2.02631579  3.04545455]
     [ 2.96153846  3.        ]
     [ 2.72727273  3.33333333]
     [ 2.15625     2.47826087]
     [ 2.          2.45      ]
     [ 2.75        3.35      ]
     [ 2.33333333  2.72222222]
     [ 2.03030303  2.71428571]
     [ 2.25        3.33333333]
     [ 2.21428571  2.66666667]
     [ 2.03333333  2.72222222]
     [ 2.28571429  2.66666667]
     [ 2.4         3.625     ]
     [ 2.64285714  3.21052632]
     [ 2.07894737  3.2       ]
     [ 2.28571429  2.54545455]
     [ 2.25        3.4       ]
     [ 2.34615385  4.        ]
     [ 2.56666667  2.65217391]
     [ 1.85294118  2.33333333]
     [ 2.06451613  3.05555556]
     [ 2.          2.66666667]
     [ 2.22580645  2.57142857]
     [ 2.16129032  2.33333333]
     [ 2.22580645  2.2173913 ]
     [ 2.14814815  2.68421053]
     [ 2.125       2.56521739]
     [ 2.03030303  2.28      ]
     [ 2.23333333  2.26086957]
     [ 2.52        2.63157895]
     [ 2.16666667  2.6       ]
     [ 1.82352941  2.34782609]
     [ 1.96666667  2.83333333]]
    filter_class0 = iris_np[:,-1]==0.0
    iris_np_class0 = iris_np[filter_class0]
    
    filter_class1 = iris_np[:,-1]==1.0
    iris_np_class1 = iris_np[filter_class1]
    
    filter_class2 = iris_np[:,-1]==2.0
    iris_np_class2 = iris_np[filter_class2]
    
    _, axe = plt.subplots()
    axe.set_xlim(0,8)
    axe.set_ylim(0,3.0)
    axe.scatter(iris_np_class0[:,2],iris_np_class0[:,3])
    axe.scatter(iris_np_class1[:,2],iris_np_class1[:,3])
    axe.scatter(iris_np_class2[:,2],iris_np_class2[:,3])