1

I expect the answer is "not possible". I am compiling some matplotlib plots that take a long time to produce. The plots are generated based on data that also takes a long time to produce, but even when this data is saved to file, it takes a long time to read in the data and generate the plot. If I do this many times, and then suddenly decide I'd rather have a different set of axes titles on the plot, there is nothing I can do to change the title or the axes or the plot colour or whatnot.

I understand that without access to the raw data in the system I would never be able to change how the plot itself looks, but is there some way of processing these images such that it's still possible to edit the axis titles quickly? Somehow I'd like to save a half-compiled version of the plot?

1 Answer 1

1

It depends on what aspects of the plot you would like to change. A general solution would be using pickle, based on this related question.

For example, at one point, you could serialize a Figure instance with pickle.dump():

import pickle
import matplotlib.pyplot as plt
import numpy as np

# Plot sine wave as demo
fig = plt.figure()
x = np.linspace(0, 2 * np.pi)
plt.plot(x, np.sin(x))
plt.title("before pickling")

# Save figure to disk
with open("sine.pickle", "wb") as f:
    pickle.dump(fig, f)

Then, at a later point, you could deserialize it using pickle.load() and adjust it as necessary (here, I change the title):

import pickle

with open("sine.pickle", "rb") as f:
    fig_loaded = pickle.load(f)
    
fig_loaded.get_axes()[0].set_title("after pickling")
fig_loaded.show()

Changing other aspects of the Figure is also possible, but will be cumbersome and will imply that you pretty much know the Figure's content. With the given plot, you could for example also change the color of the sine wave after deserialization/loading:

fig_loaded.get_axes()[0].get_lines()[0].set_color("red")

If you need more concrete answers, a more concrete question would be necessary.

In any case, as a caveat, you would need to make sure that the matplotlib version of your Python environment is still the same or a compatible one (on the code level, that is) at the point of deserialization/loading, or otherwise you might see problems like in this question regarding the (de)serialization of plotly instances.

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.