我创建了一个函数,它接受来自数据集的一系列值并输出一个图像。例如:
假设我想要保存60-70华氏度在加利福尼亚的温度下的降水图像。那么,我可以调用
我的问题是:如何将调用函数后生成的图像保存为jpeg格式?
我知道
谢谢!
更多细节:以下是代码(大幅简化):
my_plot(location_dataset, min_temperature, max_temperature)
将返回指定温度范围内的降水量图像。假设我想要保存60-70华氏度在加利福尼亚的温度下的降水图像。那么,我可以调用
my_plot(California, 60, 70)
,并得到当温度在60到70华氏度之间时加利福尼亚的降水图像。我的问题是:如何将调用函数后生成的图像保存为jpeg格式?
我知道
plt.savefig()
适用于非函数调用结果的情况,但在我的情况下,我该怎么做呢?谢谢!
更多细节:以下是代码(大幅简化):
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
,该如何保存这个图表呢?
my_plot
函数返回什么?或者将图表保存在my_plot
函数中。 - Andymin_temperature
е’Ңmx_temperature
еҸӮж•°жҢҮе®ҡзҡ„жё©еәҰиҢғеӣҙз»ҳеҲ¶зҡ„еӣҫиЎЁгҖӮ - user3495042pyplot
,而是最好传递一个Axes
对象给函数,并使用面向对象的接口。 - tacaswell