7

I created a function that takes a range of values from a data set and outputs a plot. For instance:

my_plot(location_dataset, min_temperature, max_temperature) will return a plot of precipitation for the range of temperature specified in the function.

Let's say I want to save the plot for the temperature between 60-70F in California. so, I would call my function my_plot(California, 60, 70) and will get a plot of precipitation for California when temperatures are between 60 and 70F.

My question is: how do I save a plot that results from calling a function into a jpeg format?

I know of plt.savefig() when it is not the result of calling a function but in my case how do I do this?

Thanks!

More details: here is my code (heavily simplified):

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()

So then I call this function as follow: my_plot(California, 60, 70) and I get my plot for the 60-70 temperature range. how do I save this plot without having the savefig inside the function definition (and that is because I need to change the min and max temperature parameters.

4
  • because my plot is the result of a function that I call, does that make sense, or should I edit my original post? Commented Jul 16, 2014 at 22:19
  • Share some more code then. What does my_plot return? Or, save the plot within the my_plot function. Commented Jul 16, 2014 at 22:20
  • ok, I edited it. To answer to your question, my_plot returns a plot given the temperature range specified by the min_temperature and mx_temperature parameters Commented Jul 16, 2014 at 22:33
  • It is bad practice to call pyplot with in a function, it is better to pass your function an Axes object and use the OO interface. Commented Jul 17, 2014 at 13:36

1 Answer 1

23

Take the reference to the figure to some variable, and return it from your function:

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
    # N.B. referenca taken to fig
    fig = plt.figure(figsize=(8, 6))
    plt.plot(x)
    plt.show()

    return fig

When you call this function, you can use the reference for saving the figure:

fig = my_plot(...)
fig.savefig("somefile.png")
Sign up to request clarification or add additional context in comments.

1 Comment

Did you try to put fig.save directly into the function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.