好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python+matplotlib实现循环作图的方法详解

大家好,我是皮皮。

一、前言

前几天在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")

#?设置画布
fig?=?plt.figure(figsize=(20,?8))??#?figsize是常用的参数.(宽,高)
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")
????#?设置坐标轴标题
????axl.set_xlabel('Time')
????axl.set_ylabel('Probility')
????axl.set_title('分布')
????#?可视化
????plt.show()

得到的只是单个的图。

二、实现过程

这里【月神】给了一个思路和一份示例代码,如下所示:

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.xlabel('Time')
????plt.ylabel('Probility')
????plt.title('分布')

#?可视化
plt.show()

运行之后,结果如下图所示:

顺利地解决了粉丝的问题!

后来【小趴菜】又给图加了图注,看上去高大上一些,代码如下所示:

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))
????#?设置坐标轴标题
????plt.xlabel('Time')
????plt.ylabel('Probility')
????plt.title('fenbu')
????
#?可视化
plt.legend()
plt.show()

得到的效果图如下所示:

三、总结

大家好,我是皮皮。这篇文章主要盘点了一道 matplotlib 作图的问题,文中针对该问题给出了具体的解析和代码实现,帮助粉丝顺利解决了问题。

到此这篇关于Python+matplotlib实现循环作图的方法详解的文章就介绍到这了,更多相关Python matplotlib循环作图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于Python+matplotlib实现循环作图的方法详解的详细内容...

  阅读:51次