python?matplotlib各種畫圖
1.引入matpltlib庫
matplotlib
是一種優(yōu)秀的python
數(shù)據(jù)可視化第三方庫
使用matpltlib
庫畫圖時(shí),先將它引入,加載里面的pyplot,并命名為plt,然后使用plot
函數(shù)畫圖
import matplotlib.pyplot as plt #plt是引入模塊的別名
2.pyplot基礎(chǔ)圖標(biāo)函數(shù)總結(jié)
3.plot函數(shù)畫圖語法規(guī)則
plot函數(shù)參數(shù):plot([x],y,[format],**kwargs)
各類語法太多啦,偷幾張MOOC的圖放上來~
4.折線圖
from matplotlib import pyplot as plt
#生成數(shù)據(jù) #橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(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()
4.散點(diǎn)圖
from matplotlib import pyplot as plt import numpy as np # 生成數(shù)據(jù) # 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(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') # 點(diǎn)的大小 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()
5.直方圖
from matplotlib import pyplot as plt import numpy as np # 生成數(shù)據(jù) # 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(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') # 點(diǎn)的大小 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:需要計(jì)算直方圖的一維數(shù)組 bins:直方圖的柱數(shù),可選項(xiàng),默認(rèn)為10 normed:是否將得到的直方圖向量歸一化,默認(rèn)為0 facecolor:直方圖顏色 alpha:透明度 '''
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()
橫向
多條
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()
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()
到此這篇關(guān)于python matplotlib各種畫圖的文章就介紹到這了,更多相關(guān)python matplotlib畫圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python Matplotlib畫圖之調(diào)整字體大小的示例
- Python matplotlib畫圖時(shí)圖例說明(legend)放到圖像外側(cè)詳解
- python matplotlib畫圖實(shí)例代碼分享
- Python matplotlib實(shí)時(shí)畫圖案例
- Python畫柱狀統(tǒng)計(jì)圖操作示例【基于matplotlib庫】
- python matplotlib畫圖庫學(xué)習(xí)繪制常用的圖
- python使用Matplotlib畫條形圖
- python使用Matplotlib畫餅圖
- Python中使用Matplotlib進(jìn)行多圖繪制的詳細(xì)教程
相關(guān)文章
在Windows系統(tǒng)上搭建Nginx+Python+MySQL環(huán)境的教程
這篇文章主要介紹了在Windows系統(tǒng)上搭建Nginx+Python+MySQL環(huán)境的教程,文中使用flup中間件及FastCGI方式連接,需要的朋友可以參考下2015-12-12Python+tkinter實(shí)現(xiàn)音樂下載軟件的制作
平常我們下載的歌曲,都是各種妖魔鬼怪的格式橫行,想下載下來用一下都不行,還只能在它的播放器內(nèi)聽,這誰受得了~本文就來用Python制作個(gè)音樂下載軟件,需要的可以參考一下2022-09-09獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例
這篇文章主要介紹了獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python自動化測試框架pytest的詳解安裝與運(yùn)行
這篇文章主要為大家介紹了Python自動化測試框架pytest的簡介以及安裝與運(yùn)行,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException)
這篇文章主要介紹了Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03公眾號接入chatGPT的詳細(xì)教程 附Python源碼
這篇文章主要介紹了公眾號接入chatGPT教程附Python源碼,這里需要大家準(zhǔn)備一個(gè)域名,一臺服務(wù)器和一個(gè)公眾號,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02基于Python數(shù)據(jù)結(jié)構(gòu)之遞歸與回溯搜索
今天小編就為大家分享一篇基于Python數(shù)據(jù)結(jié)構(gòu)之遞歸與回溯搜索,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Pytorch中.detach()與.data的用法小結(jié)
這篇文章主要介紹了Pytorch中.detach()與.data的用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07