我创建了一个函数,它从数据集中获取一系列值并输出一个图。例如:
my_plot(location_dataset, min_temperature, max_temperature)
将返回函数中指定温度范围内的降水图。
假设我想保存加利福尼亚州 60-70F 之间温度的绘图。所以,我会调用我的函数my_plot(California, 60, 70)
并将获得加利福尼亚州温度在 60 到 70°F 之间时的降水量图。
我的问题是:如何将调用函数产生的绘图保存为 jpeg 格式?
我知道plt.savefig()
当它不是调用函数的结果时,但在我的情况下我该怎么做?
Thanks!
更多细节:这是我的代码(大大简化):
import matplotlib.pyplot as plt
def my_plot(location_dataset, min_temperature, max_temperature):
condition = (location_dataset['temperature'] > min_temperature) & (dataset['temperature'] <= max_temperature)
subset = location_dataset[condition] # subset the data based on the temperature range
x = subset['precipitation'] # takes the precipitation column only
plt.figure(figsize=(8, 6))
plt.plot(x)
plt.show()
那么我调用这个函数如下:my_plot(California, 60, 70)
我得到了 60-70 温度范围的图。我如何在没有savefig
在函数定义内(这是因为我需要更改最小和最大温度参数。