溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python?matplotlib是如何畫圖的

發(fā)布時間:2021-12-27 18:23:35 來源:億速云 閱讀:162 作者:柒染 欄目:開發(fā)技術(shù)

python matplotlib是如何畫圖的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

    1.引入matpltlib庫

    matplotlib是一種優(yōu)秀的python數(shù)據(jù)可視化第三方庫
    使用matpltlib庫畫圖時,先將它引入,加載里面的pyplot,并命名為plt,然后使用plot函數(shù)畫圖

    import matplotlib.pyplot as plt #plt是引入模塊的別名

    2.pyplot基礎(chǔ)圖標(biāo)函數(shù)總結(jié)

    python?matplotlib是如何畫圖的

    3.plot函數(shù)畫圖語法規(guī)則

    plot函數(shù)參數(shù):plot([x],y,[format],**kwargs)

    python?matplotlib是如何畫圖的

    各類語法太多啦,偷幾張MOOC的圖放上來~

    python?matplotlib是如何畫圖的

    python?matplotlib是如何畫圖的

    python?matplotlib是如何畫圖的

    python?matplotlib是如何畫圖的

    python?matplotlib是如何畫圖的

    4.折線圖

    from matplotlib import pyplot as plt

    #生成數(shù)據(jù)
    #橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個參數(shù)可控制步長,可寫可不寫
    x = range(2017, 2022)
    #y對應(yīng)縱坐標(biāo)的值
    y1 = [49, 48, 45, 52, 50]
    y2 = [60, 62, 61, 65, 63]
    #生成圖形
    plt.title("LMY and her mother's weight")
    plt.xlabel('year')
    plt.ylabel('kg')
    plt.plot(x, y1, color='green', label='LMY')
    plt.plot(x, y2, color='purple', label='mother')
    plt.grid(alpha=0.5)
    plt.legend(loc='upper right')
    #顯示圖形
    plt.show()

    python?matplotlib是如何畫圖的

    4.散點圖

    from matplotlib import pyplot as plt
    import numpy as np
    
    # 生成數(shù)據(jù)
    # 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個參數(shù)可控制步長,可寫可不寫
    x = range(2017, 2022)
    # y對應(yīng)縱坐標(biāo)的值
    y1 = [49, 48, 45, 52, 50]
    y2 = [60, 62, 61, 65, 63]
    # 生成圖形
    plt.title("LMY and her mother's weight")
    plt.xlabel('year')
    plt.ylabel('kg')
    # 點的大小
    area = np.pi*4**2
    plt.scatter(x, y1, s=area, c='yellow', alpha=1)
    plt.scatter(x, y2, s=area, c='blue', alpha=1)
    plt.legend()
    plt.yticks(())
    plt.show()

    python?matplotlib是如何畫圖的

    5.直方圖

    from matplotlib import pyplot as plt
    import numpy as np
    
    # 生成數(shù)據(jù)
    # 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個參數(shù)可控制步長,可寫可不寫
    x = [2017, 2018, 2019, 2020, 2021]
    # y對應(yīng)縱坐標(biāo)的值
    y1 = [49, 48, 45, 52, 50]
    y2 = [60, 62, 61, 65, 63]
    # 生成圖形
    plt.title("LMY and her mother's weight")
    plt.ylabel('frequency')
    plt.xlabel('kg')
    # 點的大小
    plt.hist(y1, bottom=None, color='purple')
    plt.hist(y2, bottom=None, color='pink')
    plt.show()
    
    # n, bins, patches = plt.hist(arr, bins=50, normed=1, facecolor='green', alpha=0.75)
    '''
    arr:需要計算直方圖的一維數(shù)組
    bins:直方圖的柱數(shù),可選項,默認(rèn)為10
    normed:是否將得到的直方圖向量歸一化,默認(rèn)為0
    facecolor:直方圖顏色
    alpha:透明度
    '''

    python?matplotlib是如何畫圖的

    6.條形圖

    縱向

    from matplotlib import pyplot as plt
    import numpy as np
    
    arr = np.arange(2017, 2022)
    x = [49, 48, 45, 52, 50]  # x軸
    y = [2017, 2018, 2019, 2020, 2021]
    rect = plt.bar(arr, x, width=0.5)
    plt.title('LMY')
    plt.xlabel('weight')
    plt.ylabel('year')
    plt.legend()
    
    plt.show()

    python?matplotlib是如何畫圖的

    橫向

    python?matplotlib是如何畫圖的

    多條

    from matplotlib import pyplot as plt
    import numpy as np
    
    arr = np.arange(2017, 2022)
    x1 = [49, 48, 45, 52, 50]  # x軸
    x2 = [60, 62, 61, 65, 63]
    y = [2017, 2018, 2019, 2020, 2021]
    rects1 = plt.bar(arr, x1, 0.5, color='purple', label='LMY')
    rects2 = plt.bar(arr, x2, 0.5, color='yellow', label='Mother', alpha=0.3)
    plt.title("LMY and her mother's weight")
    plt.xlabel('weight')
    plt.ylabel('year')
    plt.legend()
    
    plt.show()

    python?matplotlib是如何畫圖的

    7.餅圖

    from matplotlib import patches, pyplot as plt
    import numpy as np
    
    label_list = ['49', '48', '45', '52', '50']
    size = [20, 20, 20, 20, 20]
    # 各部分的顏色
    color = ['red', 'pink', 'blue', 'green', 'purple']
    explode = [0, 0, 0.15, 0, 0]
    
    patches, l_text, p_text = plt.pie(size, explode=explode, colors=color, labels=label_list,
                                      labeldistance=1.2, autopct="%1.2f%%", shadow=False, startangle=90, pctdistance=0.6)
    plt.axis('equal')
    plt.title("LMY's weight")
    plt.legend(loc='upper left')
    plt.show()

    python?matplotlib是如何畫圖的

    看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI