Hi,
While moving from Matlab to Numpy/Scipy/Matplotlib I need sometimes to work
with Matlab figures. It would be nice if we could load Matlab figures in
Python, extract the data and some basic parameters of how it was looking in
Matlab and create its "clone" in matplotlib. At present the Matlab figures are
MAT files and in the near future it will be HDF5, both formats are loadable.
However, I have not found any reference for such attempts to load and parse the
FIG files. As a beginner I find it difficult to write a large piece of code.
However, if there are other interested users that can cooperate on such, I'd
gladly contribute some hours for this task. Meanwhile, to show the
proof-of-concept attempt is attached below. All your useful comments and
suggestions are very welcome.
Thank you,
Alex
#------------ loadfig.py ---------------------- #
""" Loadfig loads simple Matlab figures as MAT files and plots the lines using
matplotlib
"""
import numpy as np
from scipy.io import loadmat
import matplotlib.pyplot as plt
def lowest_order(d):
"""
lowestsc_order(hgstruct) finds the handle of axes (i.e. children of
figure handles, etc.)
"""
while not 'graph2d.lineseries' in d['type']:
d = d['children'][0][0]
return d
def get_data(d):
"""
get_data(hgstruct) extracts XData, YData, Color and Marker of each line
in the
given axis
Parameters
----------
hgstruct : structure
obtained from lowest_order(loadmat('matlab.fig'))
"""
xd,yd,dispname,marker,colors = [],[],[],[],[]
for i in d:
if i['type'] == 'graph2d.lineseries':
xd.append(i['properties'][0][0]['XData'][0][0])
yd.append(i['properties'][0][0]['YData'][0][0])
dispname.append(i['properties'][0][0]['DisplayName'][0][0])
marker.append(i['properties'][0][0]['Marker'][0][0])
colors.append(i['properties'][0][0]['Color'][0][0])
return
np.asarray(xd),np.asarray(yd),dispname,np.asarray(marker).astype('S1'),colors
def plot_data(xd,yd,dispname=None,marker=None,colors=None):
"""
plot_data(xd,yd,dispname=None,marker=None,colors=None)
plots the data sets extracted by
get_data(lowest_order(loadmat('matlab.fig')))
Parameters
----------
xd,yd : array_like
data arrays
dispname : array of strings
to be used in legend, optional
marker : array of characters
markers, e.g. ['o','x'], optional
colors : array of color sets
in RGB, e.g. [[0,0,1],[1,0,0]], optional
"""
for i,n in enumerate(xd):
plt.plot(xd[i].T,yd[i].T,color=tuple(colors[i]),marker=marker[i],linewidth=0)
plt.legend(dispname)
plt.show()
def main(filename):
"""
main(filename)
loads the filename (with the extension .fig) which is Matlab figure. At
the moment only
simple 2D lines are supported.
Examples
-------
>>> loadfig('matlab.fig') # is equivalent to:
>>> d = loadmat(filename)
>>> d = d['hgS_070000']
>>> xd,yd,dispname,marker,colors = get_data(lowest_order(d))
>>> plot_data(xd,yd,dispname,marker,colors)
"""
d = loadmat(filename)
# ver 7.2 or lower:
d = d['hgS_070000']
xd,yd,dispname,marker,colors = get_data(lowest_order(d))
plot_data(xd,yd,dispname,marker,colors)
if __name__ == "__main__":
import sys
import os
try:
filename = sys.argv[1]
main(filename)
except:
print("Wrong file")
# ----------------------------- EOF loadfig.py --------------------------- #
Alex Liberzon
Turbulence Structure Laboratory [http://www.eng.tau.ac.il/efdl]
School of Mechanical Engineering
Tel Aviv University
Ramat Aviv 69978
Israel
Tel: +972-3-640-8928
Lab: +972-3-640-6860 (telefax)
E-mail: [email protected]
------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand
malware threats, the impact they can have on your business, and how you
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users