Skip to content Skip to sidebar Skip to footer

Python Matplotlib: How To Automatically Save Figures In .fig Format?

With python matplotlib module, we can use pylab.savefig() function to save figures. However it seems that this function can't be used to save figures in .fig format. The .fig forma

Solution 1:

You can pickle a figure to disk like this

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

# Plot
fig_object = plt.figure()
x = np.linspace(0,3*np.pi)
y = np.sin(x)
plt.plot(x,y)
# Save to disk
pickle.dump(fig_object,open('sinus.pickle','wb'))

And then load it from disk and display:

fig_object = pickle.load(open('sinus.pickle','rb'))
fig_object.show()

Solution 2:

If you are looking to save python plots as an interactive figure to modify and share with others like MATLAB .fig file then you can try to following code. Here z_data.values is just a numpy ndarray and so you can use the same code to plot and save your own data. No need of using pandas then.

The file generated here can be opened and interactively modified by anyone with or without python just by clicking on it and opening in browsers like Chrome/Firefox/Edge etc.

import plotly.graph_objects as go
import pandas as pd

z_data=pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')

fig = go.Figure(data=[go.Surface(z=z_data.values)])

fig.update_layout(title='Mt Bruno Elevation', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))

fig.show()
fig.write_html("testfile.html")

Solution 3:

The MATLAB .fig file is nothing more than a MAT-file that contains each of the handle graphics objects in the figure, complete with all their properties. MATLAB handle graphics objects are class objects whose type determines what graphic to draw, and whose properties determines how to draw it.

For example a line plot will have a figure object, containing an axes object, containing a line object. The line object will have xdata and ydata properties containing the data plotted. But each of these objects has many, many properties that need to be set. For example, the line also has properties for color and width, which markers to use and how to draw those, etc.

We can write a MAT-file in Python using scipy.io.savemat. But generating the data to be saved will be a lot of work. We would have to take each of the Matplotlib objects in the figure we wish to save (which have a similar hierarchy to MATLAB’s), and translate their properties to how MATLAB represents them in their objects. This will involve research for each graphics object type, since they all have a different set of properties.

Writing a function to create a .fig file from a Matplotlib plot would be possible, but not worth the effort, IMO. It would be easier to save the data and translate the Python script that generates the plot to a MATLAB script to generate the equivalent plot in MATLAB.

Solution 4:

I have never found a way to output figures using the .fig format but one alternative would be to output your plots as encapsulated post-scripts (eps). This allows for manipulation of the individual plot elements after the fact with a program such as adobe illustrator.

import matplotlib.pyplot as plt
.
.
.
plt.savefig("MyFigure.eps", format="eps")

Post a Comment for "Python Matplotlib: How To Automatically Save Figures In .fig Format?"