带图的函数循环

时间:2021-04-13 23:30:31

标签: python matplotlib

我有一个函数可以返回一些变量并绘制图形(直方图)。我想在循环中运行该函数并在单独的图表上绘制每个结果。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def func_graph(a, b):
   plt.hist(a, density=True, bins=10)
   return sum(a), b


a = [np.random.randn(1000), np.random.randn(1000), np.random.randn(1000)]
b = 5
List_of_results = [func_graph(x,b) for x in a]

所以在打印 List_of_results[0] 之后,我会得到第一个 (sum(a), b) 项的 a 及其绘图(现在它绘制了所有图形)

我怎样才能实现它?

我在 Jupyter 中做的

1 个答案:

答案 0 :(得分:0)

您需要存储和返回直方图。

def func_graph(a, b):
   histogram = plt.hist(a, density=True, bins=10)
   return sum(a), b, histogram