9

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 format is matlab figure format.

With .fig format, we can adjust/modify the figures, that is why I want to save figures into .fig format.

So are there any ways to save figures in .fig format with python matplotlib?

lily
  • 308
  • 3
  • 10
  • [What kind of modification do you want to make using Matlab, that you cannot do with matplotlib?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Schorsch Apr 15 '14 at 15:37
  • 1
    @Schorsch after saving the figures into a .png format, I can't modify the .png files like modifying .fig files – lily Apr 15 '14 at 15:50
  • 1
    @lily I believe Schorsch was asking why you need to make modifications to a figure generated with matplotlib in matlab; why not make those modifications directly in python before you generate the figure? **It sounds like the real problem is that you do not know how to do something in matplotlib that you can do in matlab.** What is that something? – wflynny Apr 15 '14 at 15:53
  • 1
    @Bill For example, Originally, I plot the graph for a paper. But later I notice I need to put it in a PTT and thus need to adjust the width:length rate or the title, or I need to add some dashed lines as benchmark, blabla. Just an example – lily Apr 15 '14 at 16:01
  • 5
    matplotlib does not have an easy way to do this. The preferred method to deal with this is to write a python script/function that takes in your (raw?) data and returns the graph. You can then tweak and re-generate your graph when needed. – tacaswell Apr 15 '14 at 16:08
  • 1
    Matplotlib has an (experimental?) feature which allows you to [pickle](http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object) objects. This will allow you to save an object which could then be opened in Python at a later date for modification. – Ffisegydd Apr 16 '14 at 08:37
  • 1
    You can save to figure as SVG. This can be then modified in any vector graphics program at a later point. – numentar Apr 16 '14 at 11:49
  • Did you manage to find a solution to this? – Charlie Parker Oct 10 '16 at 05:52
  • Possible duplicate: [Saving interactive Matplotlib figures](https://stackoverflow.com/questions/4348733/saving-interactive-matplotlib-figures) – Bas Swinckels Jun 02 '20 at 14:05

4 Answers4

7

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()
GLaDOS
  • 620
  • 6
  • 17
sefira32
  • 91
  • 1
  • 5
5

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
1

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")
Sumit
  • 51
  • 2
0

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")
Raab70
  • 721
  • 3
  • 11
  • Have you found a way now 2 years later? – Charlie Parker Oct 10 '16 at 05:58
  • Haven't really tried TBH, eps serves my needs just fine. Free tools like Gimp allow for any modifications necessary. For any 3d visualization I usually make an animated spinning gif easily like this: https://stackoverflow.com/questions/26876885/how-to-make-gif-images-from-a-set-of-images-in-matlab – Raab70 Oct 10 '16 at 16:30