0

I have a function wrapper for making a plot in matplotlib. i want to know how best we return the figure handle from inside the function. I want to use the figure handle to update the plot by putting more points on it. The size of the points should depend on it's value of the data point. The bigger the data point, the bigger the size of the point.

1 Answer 1

1

One common way is to return an Axes object from your function. You can do additional plotting directly from the Axes.

You don't say whether your function is using the pyplot state machine or bare-bones Matplotlib, but here's an example of the former:

import matplotlib.pyplot as plt

x = range(3)
y1 = [2, 1, 3]
y2 = [3, 2, 1]

def plot_data(x, y):
    """Plots x, y. Returns the Axes."""
    plt.plot(x, y, '-.k')
    return plt.gca()

ax = plot_data(x, y1)
ax.scatter(x, y2, s=y2)

Here we also use the s= argument to specify the size of each point. Matplotlib assumes certain units for these values so you may end up having to multiply by some constant to scale them to meet your aesthetics.

Note that in addition to returning the Axes, sometimes it's useful to also have your plotting function also take an existing Axes as the argument.

Sign up to request clarification or add additional context in comments.

Comments

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.