Python+matplotlib實現(xiàn)循環(huán)作圖的方法詳解
大家好,我是皮皮。
一、前言
前幾天在Python白銀交流群【在 途中要勤奮的熏肉肉】問了一道Python可視化處理的問題,如下圖所示。

原始代碼,如下所示:
import?pandas?as?pd
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?scipy.stats?as?st
result_parameter_peak?=?pd.read_csv("result_parameter_peak.csv",?encoding="utf_8_sig")
#?設(shè)置畫布
fig?=?plt.figure(figsize=(20,?8))??#?figsize是常用的參數(shù).(寬,高)
axl?=?fig.add_subplot(1,?1,?1)
for?i?in?range(len(result_parameter_peak)):
????x?=?np.arange(0,?400,?1)
????#?繪制gamma曲線
????y661?=?st.gamma.pdf(x,?result_parameter_peak.iloc[i,?1],?scale=result_parameter_peak.iloc[i,?2])
????axl.plot(x,?y661,?'r-.',?label="α=?9.9028,β=10.4205")
????#?設(shè)置坐標(biāo)軸標(biāo)題
????axl.set_xlabel('Time')
????axl.set_ylabel('Probility')
????axl.set_title('分布')
????#?可視化
????plt.show()
得到的只是單個的圖。

二、實現(xiàn)過程
這里【月神】給了一個思路和一份示例代碼,如下所示:

import?pandas?as?pd
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?scipy.stats?as?st
result_parameter_peak?=?pd.read_csv("result_parameter_peak.csv",?encoding="utf_8_sig")
plt.figure()
for?i,?alpha,?beta?in?result_parameter_peak.itertuples():
????x?=?np.arange(0,?300,?1)
????#?繪制gamma曲線
????y661?=?st.gamma.pdf(x,?alpha,?scale=beta)
????plt.plot(x,?y661,?'-.')
????#?設(shè)置坐標(biāo)軸標(biāo)題
????plt.xlabel('Time')
????plt.ylabel('Probility')
????plt.title('分布')
#?可視化
plt.show()
運行之后,結(jié)果如下圖所示:

順利地解決了粉絲的問題!

后來【小趴菜】又給圖加了圖注,看上去高大上一些,代碼如下所示:
import?pandas?as?pd
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?scipy.stats?as?st
result_parameter_peak?=?pd.read_csv("result_parameter_peak.csv",?encoding="utf_8_sig")
plt.figure()
for?i,?alpha,?beta?in?result_parameter_peak.itertuples():
????x?=?np.arange(0,?300,?1)
????#?繪制gamma曲線
????y661?=?st.gamma.pdf(x,?alpha,?scale=beta)
????#?plt.plot(x,?y661,?'-.')
????plt.plot(x,?y661,?'-.',?label="α:"?+?str(alpha)?+?"β:"?+?str(beta))
????#?設(shè)置坐標(biāo)軸標(biāo)題
????plt.xlabel('Time')
????plt.ylabel('Probility')
????plt.title('fenbu')
????
#?可視化
plt.legend()
plt.show()
得到的效果圖如下所示:

三、總結(jié)
大家好,我是皮皮。這篇文章主要盤點了一道matplotlib作圖的問題,文中針對該問題給出了具體的解析和代碼實現(xiàn),幫助粉絲順利解決了問題。
到此這篇關(guān)于Python+matplotlib實現(xiàn)循環(huán)作圖的方法詳解的文章就介紹到這了,更多相關(guān)Python matplotlib循環(huán)作圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用PyAudio制作錄音工具的實現(xiàn)代碼
這篇文章主要介紹了Python使用PyAudio制作錄音工具,音頻錄制與視頻錄制相似,也是以數(shù)據(jù)幀的方式錄制保存,這次使用強大的第三方包PyAudio和內(nèi)置的wave模塊編寫,需要的朋友可以參考下2022-04-04
使用Python創(chuàng)建簡單的HTTP服務(wù)器的方法步驟
這篇文章主要介紹了使用Python創(chuàng)建簡單的HTTP服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python的爬蟲程序編寫框架Scrapy入門學(xué)習(xí)教程
Python的一大優(yōu)勢就是可以輕松制作Web爬蟲,而超高人氣的Scrapy則是名副其實的Python編寫爬蟲的利器,這里我們就來看一下Python的爬蟲程序編寫框架Scrapy入門學(xué)習(xí)教程:2016-07-07

