Any way to get a figure from Python's matplotlib into Matlab? - python

I'm processing some data for a research project, and I'm writing all my scripts in python. I've been using matplotlib to create graphs to present to my supervisor. However, he is a die-hard MATLAB user and he wants me to send him MATLAB .fig files rather than SVG images.
I've looked all over but can't find anything to do the job. Is there any way to either export .fig files from matplotlib, convert .svg files to .fig, or import .svg files into MATLAB?

Without access to (or experience with matlab) this is going to be a bit tricky. As Amro stated, .fig files store the underlying data, and not just an image, and you're going to have a hard time saving .fig files from python. There are however a couple of things which might work in your favour, these are:
numpy/scipy can read and write matlab .mat files
the matplotlib plotting commands are very similar to/ based on the matlab ones, so the code to generate plots from the data is going to be nearly identical (modulo round/square brackets and 0/1 based indexing).
My approach would be to write your data out as .mat files, and then just put your plotting commands in a script and give that to your supervisor - with any luck it shouldn't be too hard for him to recreate the plots based on that information.
If you had access to Matlab to test/debug, I'm sure it would be possible to create some code which automagically created .mat files and a matlab .m file which would recreate the figures.
There's a neat list of matlab/scipy equivalent commands on the scipy web site.
good luck!

Related

Can I translate/duplicate an existing graph in Excel into Python?

I have many graphs in Excel that I would like to convert to Python but am struggling with how to do so using Matplotlib. Is there a package or method that would essentially convert/translate all the formatting and data series selection into python?
Once I could see a few examples of the correct code I think I could start doing this directly in python but I do not have much experience manually creating graph code (I use Excel insert graphs mostly) so am looking for a bridge.

Making 3d lines with pycollada

I am using QGIS and there is a plugin Qgis2threejs that can accept collada files along with the current map layer and produce three.js output. I am not familiar with collada, but I want to use pycollada to build a set of 3d lines that I can import into the plugin.
I am having a lot of trouble understanding how to do this with pycollada, I don't see many tutorials or examples on the internet, the ones I see are mostly for cubes.
I basically want to know how to build the simplest python script that will create lines if I know the x,y,z coordinates of each point and then write them to the file.
Does anyone know of a tutorial that does this, or something similar.
This lineset test in the repo should give you a good example.
You should also read the Creating A Collada Object section of the docs.

Move data from a MATLAB program to python

I am fairly new to programming, and started programming in python 3. The data I want to analyze have already been processed in a matlab program, and I need to export them. I don't know anything about matlab, and after searching the web I came up with this:
fileID = fopen('VarA.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,1));
fclose(fileID);
fileID = fopen('varB.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,2));
fclose(fileID);
This gives me 2 .txt files with x and y coordinates respectively. I have about 1000 strings (which each contains about 10k datapoints), so this seems like an awful way to do this.
My question is then; how can I export these datasets with an efficient code? For instance: I am writing a dataset to 2 different .txt files which separates the 2 variables, instead of a hash saved in 1 file.
If you are interested in exporting a lot of structured numerical data, i recommend you use the HDF5 functions in matlab to write these, and the corresponding python library to read these.
To simplify the code you showed there, read about dlmwrite in the matlab help.
The way you choose (or via dlmwrite) may sound awful in the beginning, but very often will not really have an impact on the performance.

How to build a file to save different Python objects as a project file?

I'm creating a GUI using PyQt4 and Python 2.7, I have to process different images with advanced techniques. The images are processed as NumPy arrays, however, in the program there are more than one image and other data (dictionaries, tuples, lists, NumPy arrays) that should be stored preferably in just one file. It would be great that all this data could be stored in a file as file_name.project (with a custom extension). I don't know how to do this. There is an easy way to do this stuff? What do you recommend me?

How to extract data from Matlab .fig files in Python?

If I have some X vs Y data saved in a Matlab .fig file, is there a way to extract that data in Python? I've tried using the method shown in a previous discussion, but this does not work for me. I have also tried to open the files using h5py and PyTables, since .mat files are actually HDF5 files now, but this results in an error where a valid file signature can't be found.
Currently I'm trying to do this with the Anaconda distribution of Python 3.4.
EDIT: I managed to figure out something that works, but I don't know why. This has me worried something might break in the future and I won't be able to debug it. If anyone can explain why this works, but the method in the old discussion doesn't I'd really appreciate it.
from scipy.io import loadmat
d = loadmat('linear.fig', squeeze_me=True, struct_as_record=False)
x = d['hgS_070000'].children.children.properties.XData
y = d['hgS_070000'].children.children.properties.YData
The best way I can think of is using any of the Matlab-Python bridge (such as pymatbridge).
You can call Matlab code directly on python files and transform the data from one to the other. You could use some Matlab code to load the fig and extract the data and then convert the numerical variables to python arrays (or numpy arrays) easily.

Categories

Resources