0

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

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 做

4

1 回答 1

0

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

def func_graph(a, b):
   histogram = plt.hist(a, density=True, bins=10)
   return sum(a), b, histogram
于 2021-04-13T23:39:16.080 回答