Data from a MATLAB .fig file using Python? - python

Does anyone know of any methods of extracting the data from a MATLAB fig file using Python? I know these are binary files but the methods in the Python Cookbook for .mat files http://www.scipy.org/Cookbook/Reading_mat_files don't seem to work for .fig files...
Thanks in advance for any help,
Dan

.fig files are .mat files (containing a struct), see
http://undocumentedmatlab.com/blog/fig-files-format/
As the reference you give states, structs are only supported up to v7.1:
http://www.scipy.org/Cookbook/Reading_mat_files
So, in MATLAB I save using -v7:
plot([1 2],[3 4])
hgsave(gcf,'c','-v7');
Then in Python 2.6.4 I use:
>>> from scipy.io import loadmat
>>> x = loadmat('c.fig')
>>> x
{'hgS_070000': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1500e70>]], dtype=object), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Nov 18 12:02:31 2011', '__globals__': []}
>>> x['hgS_070000'][0,0].__dict__
{'handle': array([[1]], dtype=uint8), 'children': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1516030>]], dtype=object), '_fieldnames': ['type', 'handle', 'properties', 'children', 'special'], 'type': array([u'figure'], dtype='<U6'), 'properties': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1500fb0>]], dtype=object), 'special': array([], shape=(1, 0), dtype=float64)}
Where I used .__dict__ to see how to traverse the structure. E.g. to get XData and YData I can use:
>>> x['hgS_070000'][0,0].children[0,0].children[0,0].properties[0,0].XData
array([[1, 2]], dtype=uint8)
>>> x['hgS_070000'][0,0].children[0,0].children[0,0].properties[0,0].YData
array([[3, 4]], dtype=uint8)
Showing that I'd used plot([1 2],[3 4]) in MATLAB (the child is the axis and the grandchild is the lineseries).

Here is my update from Sascha's post. Now it can:
display rotated, tex labels
display xticks and yticks
better handling of markers
grid on/off
better axes and legend enumeration handling
maintain figure size
The code is below:
from scipy.io import loadmat
import numpy as np
import matplotlib.pyplot as plt
def plotFig(filename,fignr=1):
d = loadmat(filename,squeeze_me=True, struct_as_record=False)
matfig = d['hgS_070000']
childs = matfig.children
ax1 = [c for c in childs if c.type == 'axes']
if(len(ax1) > 0):
ax1 = ax1[0]
legs = [c for c in childs if c.type == 'scribe.legend']
if(len(legs) > 0):
legs = legs[0]
else:
legs=0
pos = matfig.properties.Position
size = np.array([pos[2]-pos[0],pos[3]-pos[1]])/96
plt.figure(fignr,figsize=size)
plt.clf()
plt.hold(True)
counter = 0
for line in ax1.children:
if line.type == 'graph2d.lineseries':
if hasattr(line.properties,'Marker'):
mark = "%s" % line.properties.Marker
if(mark != "none"):
mark = mark[0]
else:
mark = '.'
if hasattr(line.properties,'LineStyle'):
linestyle = "%s" % line.properties.LineStyle
else:
linestyle = '-'
if hasattr(line.properties,'Color'):
r,g,b = line.properties.Color
else:
r = 0
g = 0
b = 1
if hasattr(line.properties,'MarkerSize'):
marker_size = line.properties.MarkerSize
else:
marker_size = -1
x = line.properties.XData
y = line.properties.YData
if(mark == "none"):
plt.plot(x,y,linestyle=linestyle,color=[r,g,b])
elif(marker_size==-1):
plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b])
else:
plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b],ms=marker_size)
elif line.type == 'text':
if counter == 0:
plt.xlabel("$%s$" % line.properties.String,fontsize =16)
elif counter == 1:
plt.ylabel("$%s$" % line.properties.String,fontsize = 16)
elif counter == 3:
plt.title("$%s$" % line.properties.String,fontsize = 16)
counter += 1
plt.grid(ax1.properties.XGrid)
if(hasattr(ax1.properties,'XTick')):
if(hasattr(ax1.properties,'XTickLabelRotation')):
plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel,rotation=ax1.properties.XTickLabelRotation)
else:
plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel)
if(hasattr(ax1.properties,'YTick')):
if(hasattr(ax1.properties,'YTickLabelRotation')):
plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel,rotation=ax1.properties.YTickLabelRotation)
else:
plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel)
plt.xlim(ax1.properties.XLim)
plt.ylim(ax1.properties.YLim)
if legs:
leg_entries = tuple(['$' + l + '$' for l in legs.properties.String])
py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best','best']
MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best','none']
Mat2py = dict(zip(MAT_locs,py_locs))
location = legs.properties.Location
plt.legend(leg_entries,loc=Mat2py[location])
plt.hold(False)
plt.show()

I found Alex's answer very appealing, but I extended his code a bit. First of all, I included the preamble to show where the figure, ylabel, etc. comes from. Second of all, I included the legend!
I'm rather new to Python, so any suggestions for improvements are highly welcomed.
def plotFig(filename,fignr=1):
from scipy.io import loadmat
from numpy import size
from matplotlib.pyplot import plot,figure,hold,xlabel,ylabel,show,clf,xlim,legend
d = loadmat(filename,squeeze_me=True, struct_as_record=False)
ax1 = d['hgS_070000'].children
if size(ax1) > 1:
legs= ax1[1]
ax1 = ax1[0]
else:
legs=0
figure(fignr)
clf()
hold(True)
counter = 0
for line in ax1.children:
if line.type == 'graph2d.lineseries':
if hasattr(line.properties,'Marker'):
mark = "%s" % line.properties.Marker
mark = mark[0]
else:
mark = '.'
if hasattr(line.properties,'LineStyle'):
linestyle = "%s" % line.properties.LineStyle
else:
linestyle = '-'
if hasattr(line.properties,'Color'):
r,g,b = line.properties.Color
else:
r = 0
g = 0
b = 1
if hasattr(line.properties,'MarkerSize'):
marker_size = line.properties.MarkerSize
else:
marker_size = 1
x = line.properties.XData
y = line.properties.YData
plot(x,y,marker=mark,linestyle=linestyle,color=color(r,g,b),markersize=marker_size)
elif line.type == 'text':
if counter < 1:
xlabel("%s" % line.properties.String,fontsize =16)
counter += 1
elif counter < 2:
ylabel("%s" % line.properties.String,fontsize = 16)
counter += 1
xlim(ax1.properties.XLim)
if legs:
leg_entries = tuple(legs.properties.String)
py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best']
MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best']
Mat2py = dict(zip(MAT_locs,py_locs))
location = legs.properties.Location
legend(leg_entries,loc=Mat2py[location])
hold(False)
show()

When you save a MATLAB figure, it dumps the Handle Graphics hierarchy into a structure, saves it to a .mat file, and changes the extension to .fig. So .fig files are just .mat files, and if the data you're looking for was stored somewhere in the original figure it will be in there. If you manually change the extension back to .mat you can load it into MATLAB and take a look.
I'm afraid I don't know much about reading .mat files from Python, but if you have a way of doing that in general, you should also be able to read in a .fig file.

This is much easier way available. It's based on the newer Scipy and loadmat:
http://answerpot.com/showthread.php?3707193-loadmat+and+figure
And my small extension to it for the simple 2D lines is:
from scipy.io import loadmat
d = loadmat('../impulse_all.fig',squeeze_me=True, struct_as_record=False)
# d = loadmat('R11_resuspension.fig',squeeze_me=True, struct_as_record=False)
ax1 = d['hgS_070000'].children
if size(ax1) > 1:
ax1 = ax1[0]
figure
hold(True)
counter = 0
for line in ax1.children:
if line.type == 'graph2d.lineseries':
marker = "%s" % line.properties.Marker
linestyle = "%s" % line.properties.LineStyle
r,g,b = line.properties.Color
marker_size = line.properties.MarkerSize
x = line.properties.XData
y = line.properties.YData
plot(x,y,marker,linestyle=linestyle,color = (r,g,b),markersize=marker_size)
elif line.type == 'text':
if counter < 1:
xlabel("%s" % line.properties.String,fontsize =16)
counter += 1
elif counter < 2:
ylabel("%s" % line.properties.String,fontsize = 16)
counter += 1
hold(False)

Using the posts of Sascha, I just wanted to extract the x-data and y-data stored in a .fig file.
Below is my python function, which is a simplification of the Sascha's function aiming at only extracting the data.
Output is a dictionary. Its keys are the corresponding labels of the data in the figure.
I put it there. Glad if this could save a few minutes to someone else !
import numpy
from scipy.io import loadmat
def read_fig(filename):
output = {}
d = loadmat(filename, squeeze_me=True, struct_as_record=False)
matfig = d['hgS_070000']
childs = matfig.children
ax1 = [c for c in childs if c.type == 'axes'][0]
for line in ax1.children:
try:
if line.type == 'graph2d.lineseries':
x = line.properties.XData
y = line.properties.YData
leg = line.properties.DisplayName
print leg
output[leg] = numpy.column_stack((x, y))
except:
print 'One children is ignored...'
return output

Related

Load .fig figure form Matlab in python. Error bars make figure look weird

I got a .fig file generated in Matlab and I would like to display it in python environment, or at least retrieve the data. It works well when the original figure does not contain error bars. But when I have error bars then it looks like a staircase. Any ideas on how I could fix it?
The code I have been using is this:
## escape disti time for ions ##
size_f =26
def plotFig(filename,fignr=1):
from scipy.io import loadmat
from numpy import size
from matplotlib.pyplot import plot,figure,xlabel,ylabel,show,clf,xlim,ylim,legend,yticks,xticks
d = loadmat(filename,squeeze_me=True, struct_as_record=False)
ax1 = d['hgS_070000'].children
if size(ax1) > 1:
legs= ax1[1]
ax1 = ax1[0]
else:
legs=0
figure(fignr)
clf()
#hold(True)
counter = 0
counter1 =0
# x=np.zeros((1000,1000))
#y=np.zeros((1000,1000))
for line in ax1.children:
if line.type == 'graph2d.lineseries':
if hasattr(line.properties,'Marker'):
mark = "%s" % line.properties.Marker
mark = mark[0]
else:
mark = '.'
if hasattr(line.properties,'LineStyle'):
linestyle = "%s" % line.properties.LineStyle
else:
linestyle = '-'
if hasattr(line.properties,'Color'):
r,g,b = line.properties.Color
else:
r = 0
g = 0
b = 1
if hasattr(line.properties,'MarkerSize'):
marker_size = line.properties.MarkerSize
else:
marker_size = 1
#if counter1 ==0:
x = line.properties.XData
y = line.properties.YData
#counter1=1
#else:
# x1 = line.properties.XData
# y1 = line.properties.YData
if counter ==0:
x = line.properties.XData
y = line.properties.YData
plt.plot(x[y>0],y[y>0],linestyle=linestyle,linewidth=3.0)
counter=1
elif counter==1:
x1 = line.properties.XData
y1 = line.properties.YData
#plt.plot(x1[y1>0],y1[y1>0],linestyle=linestyle,linewidth=3.0)
counter=2
else:
x = line.properties.XData
y = line.properties.YData
plt.plot(x[y>0],y[y>0],linestyle=linestyle,linewidth=4.0,color=[r,g,b])
plt.yscale('log')
plt.xscale('log')
elif line.type == 'text':
if counter < 1:
xlabel(r'$%s$' % line.properties.String,fontsize =18)
#xlabel('%s' % line.properties.String,fontsize = 18)
counter += 1
elif counter < 2:
ylabel(r'$%s$' % line.properties.String,fontsize = 18)
# ylabel('%s' % line.properties.String,fontsize = 18)
counter += 1
ylim(1e-7,1e-1)
#ylim(ax1.properties.YLim)
xlim((1e-2, 1e3))
plt.rcParams["figure.figsize"] = [10,8]
plt.tick_params(axis='x',labelsize=size_f)
plt.tick_params(axis='y',labelsize=size_f)
plt.ylabel(r'$p(t_{esc})$', fontsize=size_f)
plt.xlabel(r'$t_{esc} \ [sec]$', fontsize=size_f)
#plt.savefig(r'C:\Users\nikos.000\vlahos\png\escape distr_elec.png', format='png',dpi=300)#,bbox_inches='tight')
#plt.savefig(r'C:\Users\nikos.000\vlahos\eps\escape_distr_elec.eps', format='eps',dpi=300)#,bbox_inches='tight')
#### Set tick size ####
labels = [r'$10^{-2}$', r'$10^{-1}$',r'$10^{0}$',r'$10^{1}$',r'$10^{2}$', r'$10^{3}$']#,r'$10^{2}$',r'$10^{4}$']
x = [1e-2,1e-1,1e0,1e1,1e2,1e3]
plt.tick_params(axis='x',labelsize=size_f)
plt.tick_params(axis='y',labelsize=size_f)
plt.xticks(x, labels)
labels1 = [r'$10^{-6}$', r'$10^{-4}$', r'$10^{-2}$']
y = [1e-6,1e-4, 1e-2,]
plt.yticks(y, labels1)
if legs:
leg_entries = tuple(legs.properties.String)
py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best']
MAT_locs=['north','south','east','west','northeast', 'northwest', 'southeast', 'southwest','best']
Mat2py = dict(zip(MAT_locs,py_locs))
location = legs.properties.Location
leg1=legend(leg_entries,loc=Mat2py[location],frameon=False,fontsize=size_f)
leg1.legendHandles[1].set_color('black')
#plt.xticks(fontsize=18 )
#plt.yticks(fontsize=18)
#### Save figure ####
plt.savefig(r'C:\Users\nikos.000\vlahos\png\escape distr_prot.png', format='png',dpi=300,bbox_inches='tight')
plt.savefig(r'C:\Users\nikos.000\vlahos\eps\escape_distr_prot.eps', format='eps',dpi=300,bbox_inches='tight')
show()
return
plotFig(r'C:\Users\nikos.000\neutrons\f5b P=1.fig',fignr=1)
This is how the figure looks:
The original .fig file can be found here:
https://www.dropbox.com/sh/iddxpfvxz85abci/AAAU9p0DM5OYtpI7mE-nLAuga?dl=0

Python matplotlib - set_data and set_3d_properties don't seem to be updating my plot

I am currently working on a Yee Solver script for uni, but when I try to animate my 3D graph, the graph is not what is expected. It works for a 2D plot, but I can't seem to translate that into 3D. From my understanding, set_data and set_3d_properties need a 1D array to work, which I am inputting.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.widgets import Slider
# Program Variables
wv_lgth_num = 10
graph_type = '3d'
t = 0
# Physical Constants
c = 3e8
mu_r = 1
eps_r = 1
# Source Constants
f = 2e9
omega = 2*f*(np.pi)
amp = 1.0
wv_lgth = c/f
period = 1/f
# Step size
dz = wv_lgth/20
dt = ((c/f)/20)/c
#dt = ((1/f)/20)/1
# Axis Grids
z_grid = np.arange(0,wv_lgth_num*wv_lgth,dz)
t_grid = np.arange(0,10*period,dt)
# Number of steps
num_z = z_grid.size
num_t = t_grid.size
# Coefficients
coe_E = c*dt/(eps_r*dz)
coe_H = c*dt/(mu_r*dz)
# E and H Matricies
E_mat = np.zeros((num_z,num_t))
H_mat = np.zeros((num_z,num_t))
# Generating Values for E and H
for time in range(0,num_t-1):
for pos in range(0,num_z-1):
# Source Wave
if pos == 0:
H_mat[0,time] = amp*np.sin(omega*t_grid[time])
# All cases of Yee Solver
if pos == 1:
if time == 0:
H_mat[1,0] = 0
E_mat[0,0] = 0
else:
H_mat[1,time] = H_mat[1,time-1] + coe_H*(E_mat[1,time-1] - E_mat[0,time-1])
E_mat[0,time] = E_mat[0,time-1] + coe_E*(H_mat[1,time] - H_mat[0,time])
if pos > 1 and pos != num_z-1:
if time == 0:
H_mat[pos,0] = 0
E_mat[pos-1,0] = 0
if time > 0:
H_mat[pos,time] = H_mat[pos,time-1] + coe_H*(E_mat[pos,time-1] - E_mat[pos-1,time-1])
E_mat[pos-1,time] = E_mat[pos-1,time-1] + coe_E*(H_mat[pos,time] - H_mat[pos-1,time])
if pos == num_z-1:
if time == 0:
H_mat[num_z-1,0] = 0
E_mat[num_z-2,0] = 0
E_mat[num_z-1,0] = 0
if time > 0:
H_mat[num_z-1,time] = H_mat[num_z-1,time-1] + coe_H*(E_mat[num_z-1,time-1] - E_mat[num_z-2,time-1])
E_mat[num_z-2,time] = E_mat[num_z-2,time-1] + coe_E*(H_mat[num_z-1,time] - H_mat[num_z-2,time])
E_mat[num_z-1,time] = E_mat[num_z-2,time]
def update(val):
t = slider_time.val
if graph_type == '2d':
a.set_ydata(E_mat[:,t])
b.set_ydata(H_mat[:,t])
if graph_type == '3d':
a.set_3d_properties(E_mat[:,t])
a.set_data(z_grid,np.zeros((num_z,num_t))[:,t])
b.set_3d_properties(np.zeros((num_z,num_t))[:,t])
b.set_data(z_grid,H_mat[:t])
fig.canvas.draw_idle()
print(H_mat)
print(H_mat[:,t].size)
print(z_grid)
print(np.zeros((num_z,num_t))[:,t].size)
# Creating plot
if graph_type == '3d':
fig, ax = plt.subplots()
ax = plt.axes(projection='3d')
b, = ax.plot3D(z_grid,H_mat[:,t],np.zeros((num_z,num_t))[:,t], label='H')
a, = ax.plot3D(z_grid,np.zeros((num_z,num_t))[:,t],E_mat[:,t], label='E')
plt.title('Light Wave')
ax.set_xlabel('z')
ax.set_ylabel('x')
ax.set_zlabel('y')
plt.legend()
ax_time = plt.axes([0.25,0.1,0.65,0.03])
slider_time = Slider(ax_time,'Time',0,num_t-2,valinit=0,valstep=1)
slider_time.on_changed(update)
plt.show()
if graph_type == '2d':
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
a, = plt.plot(z_grid,E_mat[:,t], label='E (yz plane)')
b, = plt.plot(z_grid,H_mat[:,t], label='H (xz plane)')
plt.title('Light Wave')
plt.xlabel('z')
plt.ylabel('x')
plt.legend()
ax_time = plt.axes([0.25,0.1,0.65,0.03])
slider_time = Slider(ax_time,'Time',0,num_t-2,valinit=0,valstep=1)
slider_time.on_changed(update)
plt.show()
Any help would be appreciated. The middle for loop is just generating my functions, using the Yee Method.

My Matplotlib subplot title removes itself

When I run my code I create a figure, then I create a subplot in that figure. Then when I try to add a title to it using ax.set_title("title") it sometimes shows up for a split second then goes away. I have tried using plot.title aswell with no luck.
I tried recreating the error in a small example but for some reason it worked just fine there, so here is the entire source code of the code.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.style as style
import plotgen
from matplotlib.widgets import Button
class plotWindow():
def __init__(self):
style.use("bmh")
self.dp = 30
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1, label="ax1")
self.cax = 1
self.maxax = 2
self.minax = 1
plotgen.clear("plot1.txt")
plotgen.clear("plot2.txt")
axnext = plt.axes([0.80, 0.01, 0.06, 0.06])
axprev = plt.axes([0.73, 0.01, 0.06, 0.06])
bnext = Button(axnext, 'Next >')
bnext.on_clicked(self.changePlotNext)
bprev = Button(axprev, "< Previous")
bprev.on_clicked(self.changePlotPrev)
ani = animation.FuncAnimation(self.fig, self.animate, interval=500)
plt.show()
def changePlotNext(self, i):
if self.cax < self.maxax:
self.cax += 1
self.ax.set_title("Pump " + str(self.cax))
def changePlotPrev(self, i):
if self.cax > self.minax:
self.cax -= 1
self.ax.set_title("Pump " + str(self.cax))
def animate(self, i):
if self.cax == 1:
plotgen.generate("plot1.txt")
graph_data = open('plot1.txt', 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(float(y))
self.ax.clear()
lx = len(xs)
ly = len(ys)
if len(xs) < self.dp:
pxs = xs
pys = ys
else:
pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
pys = ys[(ly - (self.dp - 1)):(ly - 1)]
self.ax.plot(pxs, pys, "r")
elif self.cax == 2:
plotgen.generate("plot2.txt")
graph_data = open('plot2.txt', 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(float(y))
self.ax.clear()
lx = len(xs)
ly = len(ys)
if len(xs) <= self.dp:
pxs = xs
pys = ys
else:
pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
pys = ys[(ly - (self.dp - 1)):(ly - 1)]
self.ax.plot(pxs, pys)
plotWindow()
As you can see in my changePlotNext and changePlotPrev functions I'm trying to change the title. Sometimes they display for a split second when I change, but then it goes away. And I am very aware that I have not set a title to display before I change the plot.
In animate, you have self.ax.clear(), which is removing all artists, texts, etc. on the Axes, including the title.
A simple option, then, is to reset the title after you clear the axes. So if you add:
self.ax.set_title("Pump " + str(self.cax))
in both places, immediately after you call self.ax.clear(), your titles will still be shown.
Another option would be to stop clearing the axes, but just remove the items you need to remove. I think this is just the lines you have plotted? So, for example, you could remove the call to self.ax.clear(), and add:
for line in self.ax.lines:
line.remove()
in its place. That will remove just the plotted line, but retain the title.

matplotlib figure parameters don't appears

Here is my issue: I have an embedded matplotlib figure in a Qt5 application. When I press the button "edit axis, curve and image parameter", I select my concerned subplot, but only the tab "axis" options appears. it is missing tabs for "curve" and "image".
actual picture
whereas I should have had something like this:
targeted picture
If anyone knows why...
Probably the answer is easy:
If there is no curve (line) in the plot, there will be no "Curves" tab.
If there is no image in the plot, there will be no "Images" tab.
class View2D(MapView):
def show(self, som, what='codebook', which_dim='all', cmap=None,
col_sz=None, desnormalize=False):
(self.width, self.height, indtoshow, no_row_in_plot, no_col_in_plot,
axis_num) = self._calculate_figure_params(som, which_dim, col_sz)
self.prepare()
if not desnormalize:
codebook = som.codebook.matrix
else:
codebook = som._normalizer.denormalize_by(som.data_raw, som.codebook.matrix)
if which_dim == 'all':
names = som._component_names[0]
elif type(which_dim) == int:
names = [som._component_names[0][which_dim]]
elif type(which_dim) == list:
names = som._component_names[0][which_dim]
while axis_num < len(indtoshow):
axis_num += 1
ax = plt.subplot(no_row_in_plot, no_col_in_plot, axis_num)
ind = int(indtoshow[axis_num-1])
min_color_scale = np.mean(codebook[:, ind].flatten()) - 1 * np.std(codebook[:, ind].flatten())
max_color_scale = np.mean(codebook[:, ind].flatten()) + 1 * np.std(codebook[:, ind].flatten())
min_color_scale = min_color_scale if min_color_scale >= min(codebook[:, ind].flatten()) else \
min(codebook[:, ind].flatten())
max_color_scale = max_color_scale if max_color_scale <= max(codebook[:, ind].flatten()) else \
max(codebook[:, ind].flatten())
norm = matplotlib.colors.Normalize(vmin=min_color_scale, vmax=max_color_scale, clip=True)
mp = codebook[:, ind].reshape(som.codebook.mapsize[0],
som.codebook.mapsize[1])
# pl = plt.pcolor(mp[::-1], norm=norm, cmap='jet')
pl = plt.imshow(mp[::-1], interpolation='nearest', origin='lower',cmap='jet')
plt.axis([0, som.codebook.mapsize[1], 0, som.codebook.mapsize[0]])
plt.title(names[axis_num - 1])
ax.set_yticklabels([])
ax.set_xticklabels([])
plt.colorbar(pl)
plt.show()

Python : QWidget: Must construct a QApplication before a QPaintDevice

I am trying to plot graphs using matplotlib when clicked on a button called "generate graph" in a QT window. At first, I found a problem : I cannot close plots or control it when the QT window is opened. But I found this solution :
Cannot move Matplotlib plot window and exit it using red X button
and I test it on an empty plot and it works. However, when I put my code I get this error :
QWidget: Must construct a QApplication before a QPaintDevice
In my qt window I put :
Process = subprocess.Popen(['python', 'mygraph.py'], shell=True).communicate()
in my script mygraph.py :
def main():
print("Beginning plot for section 2.1 ...")
#Get the selected iteration and sector
iter = InterfaceVariationTRANUS("config").DropDownListDisplayIter2_1.currentIndex()
sector = InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex()
#Access the corresponding IMPLOC file and extract the data for the selected sector
nameDirectory = InterfaceVariationTRANUS("config").nameDirectory2_1 + str(iter)
pathToDirectory = os.path.join(InterfaceVariationTRANUS("config").pathOutputDirectoryInstance, nameDirectory)
filepath = os.path.join(pathToDirectory, "IMPLOC_J.MTX")
matrix = pd.read_csv(filepath)
matrix.columns = ["Scen", "Sector", "Zone", "TotProd", "TotDem", "ProdCost", "Price", "MinRes", "MaxRes", "Adjust"]
#Removal of the noise (production equal to zero => adjust equal to zero)
#matrix.Adjust[matrix.TotProd == 0] = 0
row_index = matrix.TotProd == 0
matrix.loc[row_index,'Adjust'] = 0
#matrix.Adjust[matrix.Price == 0] = 0
row_index = matrix.Price == 0
matrix.loc[row_index,'Adjust'] =0
#matrix.Price[matrix.Price == 0] = None
row_index = matrix.Price == 0
matrix.loc[row_index,'Price'] = None
matrix2 = matrix[["Sector","Zone", "Price", "Adjust"]]
#Isolation of the data for the sector selected
nameSector = str(InterfaceVariationTRANUS("config").stockParam.list_sectors[sector])+" "+(InterfaceVariationTRANUS().stockParam.list_names_sectors[sector])
matrix3 = matrix2[matrix2["Sector"].str.contains(nameSector) == True]
matrix4 = matrix3[matrix3["Zone"].str.contains("ext_") == False]
#This boolean is used to allow for multiple graphes to be shown on the same panel.
firstPlot = False
#Plot graph and display it
if(InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex() != InterfaceVariationTRANUS("config").currentSectorPlot2_1):
InterfaceVariationTRANUS("config").numFiguresPlot2_1 = InterfaceVariationTRANUS("config").numFiguresPlot2_1 + 1
InterfaceVariationTRANUS("config").currentSectorPlot2_1 = InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex()
firstPlot = True
fig = plt.figure(self.numFiguresPlot2_1)
fig.canvas.set_window_title(InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentText())
x = np.arange(0, InterfaceVariationTRANUS("config").stockParam.nTotZones, 1)
y = pd.to_numeric(matrix4["Price"])
print("Moyenne = ")
print(y.mean(0))
z = pd.to_numeric(matrix4["Adjust"]*y)/100 + y
# plot data
if(firstPlot):
price = plt.plot(x, y, label = ("Price"))
shadowPrice = plt.plot(x, z, label = ("Price + adjust for iteration "+InterfaceVariationTRANUS("config").DropDownListDisplayIter2_1.currentText()))
plt.legend()
plt.show(block=False) #method 3
plt.draw()
if name == 'main':
main()
Where InterfaceVariationTRANUS is the class of my QT window.
Finally, I found a solution that works correctly for my problem :
import matplotlib.pyplot as plt
plt.switch_backend('Qt4Agg')
...
plt.legend()
plt.draw()
plt.show(block=False)

Categories