Python: matplotlib.pyplot探究
在学习matplotlib时,好奇pyplot作为一个module,实质是一个pyplot.py文件,那么在下面的代码中,没有生成实例对象,数据存在哪里呢?
import matplotlib.pyplot as plt import random # 画出温度变化图 # 0.准备x, y坐标的数据 x = range(60) y = [random.uniform(15, 18) for i in x] # 1.创建画布 plt.figure(figsize=(20, 8), dpi=80) # 2.绘制折线图 plt.plot(x, y) # 3.显示图像 plt.show()
其实只需要在module中定义一个全局变量,供module中的函数调用即可,数据就存在全局变量中。
例子:
文件a.py:
res = None def get_sum(a, b): global res res = a + b def result(): print(res)
文件b.py:
import a a.get_sum(1, 2) a.result() 运行结果: 3