two output graphs in matplotlib scatterplot animation - python

I have some code to animate a scatterplot in matplotlib, everything runs smoothly, except that I end up with two output graphs at the end. One contains the desired animation, while the other is blank.
While it is probably a simple issue someone, I cannot see why, nor figure out why I am getting two graphs. Can anyone see what is giving me the "bogus" empty graph?
Cheers.
import math, numpy, time, matplotlib, pyvisa
from pyvisa import visa
from matplotlib import pyplot, animation
import os.path
class Realtime_measurement_and_scatter_plot:
def __init__(......):
self.animaiton = animation.FuncAnimation(self.figure, self.take_new_data_and_update_graph, init_func=self.initialise_graph, frames = self.number_of_measurements, interval=100, blit=False)
def initialise_graph(self):
self.figure = matplotlib.pyplot.figure()
self.axes = matplotlib.pyplot.axes()
self.axes.set_title("Realtime Plot")
self.x_data = []
self.y_data = []
self.scatterplot = matplotlib.pyplot.scatter(self.x_data, self.y_data)
return self.scatterplot,
def take_new_data_and_update_graph(self, i):
...
averaged_voltage = numpy.average(measured_voltages )
new_time = float(i) * float( self.time_between_measurements )
self.x_data = numpy.append( self.x_data, averaged_voltage )
self.y_data = numpy.append( self.y_data , new_time )
self.scatterplot = matplotlib.pyplot.scatter(self.x_data, self.y_data)
return self.scatterplot,
animation_instance = Realtime_measurement_and_scatter_plot(1000000, 1 , 1, 6, "Super_FUN_TEST.0",22)
matplotlib.pyplot.show()

tcaswell's comment about "init uses attributes you don't create until initialize_graph"
prompted me to fix that.
Changing this so that init creates the figure, axes ect instead of them being created in intialize_graph removes the two output graphs problem; solving my problem!

Related

Overlapping charts in `matplotlib.animation.FuncAnimation` overwrite the alpha parameter

I am trying to plot a random walk in two ways:
static using plot method from matplotlib.pyplot,
animated using FuncAnimation from matplotlib.animation.
The first one works pretty well, you can see an example of a really simple Brownian motion below.
I tried to implement the code which would be capable of animating the random walk however I struggle with colors and the alpha parameter.
First of all the alpha parameter does not work. After successive iterations, the points overlap losing their transparency. In addition, the colors of the individual points change in each iteration, making the animation very unreadable.
You can see one animation below. The paths are the same as in the figure above.
The code I used to create the animation.
import os
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.animation import PillowWriter
class Plotter:
def __init__(self, paths, link, annotate, grid, save, alpha, cmap):
self._paths = paths
self._link = link
self._annotate = annotate
self._alpha = alpha
self._cmap = cmap
self._fig, self._ax = plt.subplots()
self._animation = None
if grid:
self._ax.grid()
self._plot()
if save:
self._check_path()
self._filename = self._create_filename()
self._save()
def _plot(self):
self._animation = FuncAnimation(
self._fig, self._anim, interval=500, frames=len(self._paths[0][0]), repeat=False,
)
def _anim(self, frame):
self._ax.set_title(f'Frame: {frame}')
for path in self._paths:
x, y = path
if self._link:
self._ax.plot(x[:frame+1], y[:frame+1], linestyle='--', marker='o', alpha=self._alpha)
else:
self._ax.scatter(x[:frame+1], y[:frame+1], alpha=self._alpha, cmap=self._cmap)
def _save(self):
self._animation.save(self._filename, dpi=300, writer=PillowWriter(fps=25))
#staticmethod
def _check_path():
if not os.path.exists('figures/'):
os.mkdir('figures/')
#staticmethod
def _create_filename():
filename = 'figures/{name}.gif'
name = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
return filename.format(name=name)
And that's how I created the .gif.
paths = [
(
[0, -0.6655408436280401, 0.10568259609257569, -0.26149279371869266, -0.5009666521908381, 0.3639625899869371, -0.49976435812228837, -0.48682212081220344, -1.2885433457534117, -0.9850803395821125, -0.018343580936876158],
[0, -0.7463614308516193, -1.3829259649135648, -0.45277423905538894, 0.5181285733354615, 1.0200223928090424, 1.523982469709905, 2.5238987154492216, 3.1215968645836893, 4.074440085625576, 4.330213499967454]),
(
[0, 0.06636040923368702, 0.05255193650063124, 0.597356910675901, -0.2793019906386772, -0.894506810533089, 0.06788127670582911, -0.3820663494179474, -0.3801418070740879, 0.6063698370900061, -0.3466678114238062],
[0, 0.9977957186149565, 1.9977003771103456, 1.1591375946582005, 1.6402500251210983, 2.4286173440649295, 2.700295774457584, 3.5933507188734235, 2.593352570806755, 2.4296614716942626, 2.732513317512087]
)
]
Plotter(paths=paths, link=True, annotate=False, grid=True, save=True, alpha=0.5, cmap='Pastel1')
Is it possible to set fixed colours for individual points and a fixed value for the alpha parameter?
A way to approach this is by creating two empty plots (one for each path) in your init function when and update the plots at each frame. You can fix the values for alpha and color while doing that.
Following that, it's also a good idea to fix the limits of your plot to prevent the plot from jumping from frame to frame.
At each frame, you can then update your plots with the values from your paths variable by using the set_data function (see doc here).
See the code you provided with the edit I suggested:
import os
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.animation import PillowWriter
import numpy as np
class Plotter:
def __init__(self, paths, link, annotate, grid, save, alpha, cmap):
self._paths = paths
self._link = link
self._annotate = annotate
self._alpha = alpha
self._cmap = cmap
self._fig, self._ax = plt.subplots()
self._animation = None
#Create a list of two empty placeholder plots and initialize alpha and color
self._lns=[self._ax.plot([],[], linestyle='--', marker='o',alpha=self._alpha,color='tab:blue'),self._ax.plot([],[], linestyle='--', marker='o',alpha=self._alpha,color='tab:orange')]
self._ax.set_xlim([-2,2]) #set xlim
self._ax.set_ylim([-2,5]) #set ylim
if grid:
self._ax.grid()
self._plot()
if save:
self._check_path()
self._filename = self._create_filename()
self._save()
def _plot(self):
self._animation = FuncAnimation(self._fig, self._anim, interval=500, frames=len(self._paths[0][0]), repeat=True)
def _anim(self, frame):
self._ax.set_title(f'Frame: {frame}')
for i,path in enumerate(self._paths):
x, y = path
if self._link:
self._lns[i][0].set_data(x[:frame+1], y[:frame+1]) #update values of the plots
def _save(self):
self._animation.save(self._filename, dpi=300, writer=PillowWriter(fps=2))
#staticmethod
def _check_path():
if not os.path.exists('figures/'):
os.mkdir('figures/')
#staticmethod
def _create_filename():
filename = 'figures/{name}.gif'
name = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
return filename.format(name=name)
And the output gives:

Cannot save animated gif with transparent background (matplotlib.animation)

I am trying to save an animation with a completely transparent background. Setting:
fig1 = (...,facecolor=(1,1,1,0))
Does not seem to work. Also, just as a side note, if you do that and view the plot then you get these weird transparency effects and lagging animation. Curious why that happens too, but mostly I just want the background to save as transparent.
If I then try:
line_ani.save('lines1.gif', writer='imagemagick',savefig_kwargs={"facecolor": (1,1,1,0)})
Then I get an output which does not have a transparent background and makes the lines thick. Same curiosity as above as why making the figure alpha to 0 would give this effect.
Another attempt:
fig1 = (...,facecolor=(1,1,1,0))
line_ani.save(...,savefig_kwargs={"transparent": None})
Also doesn't produce a transparent background.
If I just include the facecolor in the dictionary, then it gives the undesired line thickening bug.
line_ani.save(...,savefig_kwargs={"transparent": None,"facecolor":(1,1,1,0)})
The code is below.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from matplotlib.pyplot import figure
def update_line(num, data, line):
line.set_data(data[..., :num])
return line,
def plots():
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.xticks([])
plt.yticks([])
plt.box()
# Since I'm calling things twice, it's convenient to define these
fs = (3,3)
inter = 100
frames = 219
lw = 0.25
alph = 0
fig1 = plt.figure(figsize=fs)
l, = plt.plot([], [],'r',linewidth =lw)
# Generate placeholder for data and set initial conditions
DAT =np.zeros((2,300))
DAT[0][0] = 0
DAT[1][0] = 1
theta=2*np.pi*(1/np.e +0.01)
# 2D Rotation Matrix
def R(x):
return [[np.cos(x),-np.sin(x)],[np.sin(x),np.cos(x)]]
# Generate the data
for i in range(len(DAT[0])):
if i < len(DAT[0])-1:
DAT[0][i+1]=DAT[0][i]*R(theta)[0][0] + DAT[1][i]*R(theta)[0][1]
DAT[1][i+1]=DAT[0][i]*R(theta)[1][0] + DAT[1][i]*R(theta)[1][1]
# Animate the data
plots()
line_ani = animation.FuncAnimation(fig1, update_line, frames, fargs=(DAT, l),
interval=inter, blit=True,repeat_delay = 2000)
plt.show()
# Save the animation
matplotlib.use("Agg")
fig1 = plt.figure(figsize=fs)
l, = plt.plot([], [],'r',linewidth = lw)
plots()
line_ani = animation.FuncAnimation(fig1, update_line, frames, fargs=(DAT, l),
interval=inter, blit=True,repeat_delay = 2000)
print("Saving animation...")
now=time.time()
line_ani.save('lines1.gif', writer='imagemagick',savefig_kwargs={"transparent": None})
later = time.time()
print("Saved in time: ", int(later-now),"seconds")
If you run the code it should show you the animation and then save it. It also will calculate the runtime.
Setting transparent = True does the trick...
line_ani.save('lines1.gif', writer='imagemagick',savefig_kwargs={"transparent": True})

Python Scatter Plot Multiple Colorbar issue

I am trying to draw scatter plot with dynamic data. I am able to draw the data points through looping; but everytime it creates new colorbar.
Here is my code:
import time
from threading import Thread
import pandas as pd
import matplotlib.pyplot as plt
import random
class RealTime:
def __init__(self):
self.flight_complete = True
self.data = pd.DataFrame(data=None, columns=list('ABC'))
self.fig=None
self.axis = None
def on_launch(self):
plt.ion()
self.fig = plt.figure()
self.axis = self.fig.add_subplot(111)
def create_data(self):
x = round(random.uniform(-1, 1), 2)
y = round(random.uniform(-1.65, 1.65), 2)
z = 0.5
temp_data = pd.DataFrame([[x, y, z]], columns=list('ABC'))
self.data = self.data.append(temp_data, ignore_index=True)
# Plot the data
self.plot()
def start_fly(self):
self.on_launch()
fly = Thread(target=self.fly_running)
fly.start()
def fly_running(self):
for _ in range(10):
print("Flying")
# Create the data
self.create_data()
time.sleep(0.1)
return
def plot(self):
plt.gca().cla()
self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
s=50, label="Real Time Position",
c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)
plt.colormaps()
plt.title("Flight Path Map")
self.fig.canvas.draw()
self.fig.canvas.flush_events()
if __name__ == '__main__':
obj = RealTime()
obj.on_launch()
obj.fly_running()
I have read this post : How to retrieve colorbar instance from figure in matplotlib. But I couldn't really work with that.
Do you know why it creates a new colorbar? and how to avoid it?
Best Regards
Panda's plot is creating new colobar because you're asking it to create one (colorbar=True), and it looks like there is now way to tell the function that there is already a colorbar and that it should use that instead.
There are many ways to go around this problem.
the first one would be not not use DataFrame.plot() but instead use matplotlib directly to generate the plot. That will give you more control over the axes that are used and will let you recycle the colorbar from frame to frame. Here are some links that might be relevant:
How do you add a colormap to a matplotlib Animation?
Updating the positions and colors of pyplot.scatter
the second option if you want to keep your code close to what it is now it to erase the whole figure at each frame, and let pandas recreate the axes it need every time. i.e.:
def plot(self):
self.fig.clf()
self.axis = self.fig.add_subplot(111)
self.axis = self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
s=50, label="Real Time Position",
c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)

Animate points with labels with matplotlib

I've got an animation with lines and now I want to label the points.
I tried plt.annotate() and I tried plt.text() but the labes don't move.
This is my example code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
newData = np.array([[1+num,2+num/2,3,4-num/4,5+num],[7,4,9+num/3,2,3]])
line.set_data(newData)
plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line,
fig1 = plt.figure()
data = np.array([[1,2,3,4,5],[7,4,9,2,3]])
l, = plt.plot([], [], 'r-')
plt.xlim(0, 20)
plt.ylim(0, 20)
plt.annotate('A0', xy=(data[0][0], data[1][0]))
# plt.text( data[0][0], data[1][0], 'A0')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=200, blit=True)
plt.show()
Can you help me please?
My next step is:
I have vectors with origin in these Points. These vectors change their length and their direction in each animation step.
How can I animate these?
Without animation this works:
soa =np.array( [ [data[0][0],data[1][0],F_A0[i][0][0],F_A0[i][1][0]],
[data[0][1],data[1][1],F_B0[i][0][0],F_B0[i][1][0]],
[data[0][2],data[1][2],F_D[i][0][0],F_D[i][1][0]] ])
X,Y,U,V = zip(*soa)
ax = plt.gca()
ax.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1)
First thanks a lot for your fast and very helpful answer!
My Vector animation problem I have solved with this:
annotation = ax.annotate("C0", xy=(data[0][2], data[1][2]), xycoords='data',
xytext=(data[0][2]+1, data[1][2]+1), textcoords='data',
arrowprops=dict(arrowstyle="->"))
and in the 'update-function' I write:
annotation.xytext = (newData[0][2], newData[1][2])
annotation.xy = (data[0][2]+num, data[1][2]+num)
to change the start and end position of the vectors (arrows).
But what is, wehn I have 100 vectors or more? It is not practicable to write:
annotation1 = ...
annotation2 = ...
.
:
annotation100 = ...
I tried with a list:
...
annotation = [annotation1, annotation2, ... , annotation100]
...
def update(num):
...
return line, annotation
and got this error:
AttributeError: 'list' object has no attribute 'axes'
What can I do? Have you any idea?
I'm coming here from this question, where an annotation should be updated that uses both xy and xytext. It appears that, in order to update the annotation correctly, one needs to set the attribute .xy of the annotation to set the position of the annotated point and to use the .set_position() method of the annotation to set the position of the annotation. Setting the .xytext attribute has no effect -- somewhat confusing in my opinion. Below a complete example:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig, ax = plt.subplots()
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
L = 50
theta = np.linspace(0,2*np.pi,L)
r = np.ones_like(theta)
x = r*np.cos(theta)
y = r*np.sin(theta)
line, = ax.plot(1,0, 'ro')
annotation = ax.annotate(
'annotation', xy=(1,0), xytext=(-1,0),
arrowprops = {'arrowstyle': "->"}
)
def update(i):
new_x = x[i%L]
new_y = y[i%L]
line.set_data(new_x,new_y)
##annotation.xytext = (-new_x,-new_y) <-- does not work
annotation.set_position((-new_x,-new_y))
annotation.xy = (new_x,new_y)
return line, annotation
ani = animation.FuncAnimation(
fig, update, interval = 500, blit = False
)
plt.show()
The result looks something like this:
In case that versions matter, this code has been tested on Python 2.7 and 3.6 with matplotlib version 2.1.1, and in both cases setting .xytext had no effect, while .set_position() and .xy worked as expected.
You have the return all objects that changed from your update function. So since your annotation changed it's position you should return it also:
line.set_data(newData)
annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line, annotation
You can read more about matplotlib animations in this tutorial
You should also specify the init function so that the FuncAnimation knows which elements to remove from the plot when redrawing on the first update. So the full example would be:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Create initial data
data = np.array([[1,2,3,4,5], [7,4,9,2,3]])
# Create figure and axes
fig = plt.figure()
ax = plt.axes(xlim=(0, 20), ylim=(0, 20))
# Create initial objects
line, = ax.plot([], [], 'r-')
annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))
annotation.set_animated(True)
# Create the init function that returns the objects
# that will change during the animation process
def init():
return line, annotation
# Create the update function that returns all the
# objects that have changed
def update(num):
newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num],
[7, 4, 9 + num / 3, 2, 3]])
line.set_data(newData)
# This is not working i 1.2.1
# annotation.set_position((newData[0][0], newData[1][0]))
annotation.xytext = (newData[0][0], newData[1][0])
return line, annotation
anim = animation.FuncAnimation(fig, update, frames=25, init_func=init,
interval=200, blit=True)
plt.show()
I think I figured out how to animate multiple annotations through a list. First you just create your annotations list:
for i in range(0,len(someMatrix)):
annotations.append(ax.annotate(str(i), xy=(someMatrix.item(0,i), someMatrix.item(1,i))))
Then in your "animate" function you do as you have already written:
for num, annot in enumerate(annotations):
annot.set_position((someMatrix.item((time,num)), someMatrix.item((time,num))))
(You can write it as a traditional for loop as well if you don't like the enumerate way). Don't forget to return the whole annotations list in your return statement.
Then the important thing is to set "blit=False" in your FuncAnimation:
animation.FuncAnimation(fig, animate, frames="yourframecount",
interval="yourpreferredinterval", blit=False, init_func=init)
It is good to point out that blit=False might slow things down. But its unfortunately the only way I could get animation of annotations in lists to work...

matplotlib show() doesn't work twice

I have a strange problem, with matplotlib. If I run this program, I'm able to open and close several time the same figure.
import numpy
from pylab import figure, show
X = numpy.random.rand(100, 1000)
xs = numpy.mean(X, axis=1)
ys = numpy.std(X, axis=1)
fig = figure()
ax = fig.add_subplot(111)
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=5) # 5 points tolerance
def onpick(event):
figi = figure()
ax = figi.add_subplot(111)
ax.plot([1,2,3,4])
figi.show()
fig.canvas.mpl_connect('pick_event', onpick)
show()
On the contrary, if I use the same code of onpick function into my custom widget it opens the figure only the first time, into the other events it enters into the functions but doesn't display the figure:
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
import time
STEP = 0.000152
class MplCanvas(FigureCanvas):
def __init__(self):
# initialization of the canvas
FigureCanvas.__init__(self, Figure())
self.queue = []
self.I_data = np.array([])
self.T_data = np.array([])
self.LvsT = self.figure.add_subplot(111)
self.LvsT.set_xlabel('Time, s')
self.LvsT.set_ylabel('PMT Voltage, V')
self.LvsT.set_title("Light vs Time")
self.LvsT.grid(True)
self.old_size = self.LvsT.bbox.width, self.LvsT.bbox.height
self.LvsT_background = self.copy_from_bbox(self.LvsT.bbox)
self.LvsT_plot, = self.LvsT.plot(self.T_data,self.I_data)
#self.LvsT_plot2, = self.LvsT.plot(self.T_data2,self.I_data2)
self.mpl_connect('axes_enter_event', self.enter_axes)
self.mpl_connect('button_press_event', self.onpick)
self.count = 0
self.draw()
def enter_axes(self,event):
print "dentro"
def onpick(self,event):
print "click"
print 'you pressed', event.canvas
a = np.arange(10)
print a
print self.count
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(a)
fig.show()
def Start_Plot(self,q,Vmin,Vmax,ScanRate,Cycles):
self.queue = q
self.LvsT.clear()
self.LvsT.set_xlim(0,abs(Vmin-Vmax)/ScanRate*Cycles)
self.LvsT.set_ylim(-3, 3)
self.LvsT.set_autoscale_on(False)
self.LvsT.clear()
self.draw()
self.T_data = np.array([])
self.I_data = np.array([])
# call the update method (to speed-up visualization)
self.timerEvent(None)
# start timer, trigger event every 1000 millisecs (=1sec)
self.timerLvsT = self.startTimer(3)
def timerEvent(self, evt):
current_size = self.LvsT.bbox.width, self.LvsT.bbox.height
if self.old_size != current_size:
self.old_size = current_size
self.LvsT.clear()
self.LvsT.grid()
self.draw()
self.LvsT_background = self.copy_from_bbox(self.LvsT.bbox)
self.restore_region(self.LvsT_background, bbox=self.LvsT.bbox)
result = self.queue.get()
if result == 'STOP':
self.LvsT.draw_artist(self.LvsT_plot)
self.killTimer(self.timerLvsT)
print "Plot finito LvsT"
else:
# append new data to the datasets
self.T_data = np.append(self.T_data,result[0:len(result)/2])
self.I_data = np.append(self.I_data,result[len(result)/2:len(result)])
self.LvsT_plot.set_data(self.T_data,self.I_data)#L_data
#self.LvsT_plot2.set_data(self.T_data2,self.I_data2)#L_data
self.LvsT.draw_artist(self.LvsT_plot)
self.blit(self.LvsT.bbox)
class LvsT_MplWidget(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.canvas = MplCanvas()
self.vbl = QtGui.QVBoxLayout()
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
This widget is needed for an animation plot and when the experiment is finished if I click on the plot it should appear a figure, that appears only the first time.
Do you have any clue?
Thank you very much.
At the start of your code, enable interactive mode via
plt.ion()
I have new information about this that a google search turned up
This is from the writer of matplotlib. This came from http://old.nabble.com/calling-show%28%29-twice-in-a-row-td24276907.html
Hi Ondrej,
I'm not sure where to find a good
explanation of that, but let me give
you some hints. It is intended to use
show only once per program. Namely
'show' should be the last line in your
script. If you want interactive
plotting you may consider interactive
mode (pyplot.ion-ioff) like in the
example below.
Furthermore for dynamic plotting all
animation demos might be useful.
Maybe you want to have also a look at
http://matplotlib.sourceforge.net/users/shell.html
.
best regards Matthias
So it seems it is an undocumented "feature" (bug?).
Edit: here is his code block:
from pylab import *
t = linspace(0.0, pi, 100)
x = cos(t)
y = sin(t)
ion() # turn on interactive mode
figure(0)
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2))
point = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3)
for j in arange(len(t)):
# reset x/y-data of point
setp(point[0], data=(x[j], y[j]))
draw() # redraw current figure
ioff() # turn off interactive mode
show()
So maybe by using draw() you can get what you want. I haven't tested this code, I'd like to know its behavior.
I had the same issue with show() only working the first time. Are you still on version 0.99.3 or thereabouts? I was able to resolve my problem recently, if you're still interested in changing the behaviour of show(), try this:
I noticed this paragraph titled multiple calls to show supported on the what's new part of the matplotlib download site.
A long standing request is to support multiple calls to show(). This has been difficult because it is hard to get consistent behavior across operating systems, user interface toolkits and versions. Eric Firing has done a lot of work on rationalizing show across backends, with the desired behavior to make show raise all newly created figures and block execution until they are closed. Repeated calls to show should raise newly created figures since the last call. Eric has done a lot of testing on the user interface toolkits and versions and platforms he has access to, but it is not possible to test them all, so please report problems to the mailing list and bug tracker.
This was 'what's new' for version 1.0.1, at time of writing the version in synaptic was still on 0.99.3. I was able to download and build from source v1.0.1. The additional packages I also required to satisfy dependencies were libfreetype6-dev tk-dev tk8.5-dev tcl8.5-dev python-gtk2-dev; your mileage may vary.
Now that i have matplotlib.__version__ == 1.0.1 , the following code works how I would expect:
from matplotlib import pyplot as p
from scipy import eye
p.imshow(eye(3))
p.show()
print 'a'
p.imshow(eye(6))
p.show()
print 'b'
p.imshow(eye(9))
p.show()
print 'c'
def onpick(self,event):
print "click"
print 'you pressed', event.canvas
...
ax.plot(a)
fig.show() # <--- this blocks the entire loop
Try:
def onpick(self,event):
print "click"
print 'you pressed', event.canvas
...
ax.plot(a)
self.draw()
self.update()
My workaround to this problem is to never call close.
I'm pretty sure you can control the transparency of a widget in PyQt. You might try controlling the visibility using Qt instead of matplotlib. I'm sure someone else who knows more about matplotlib can give a better answer than that though :D
You can create a figure instance by:
fig = plt.figure(0)
And draw your stuff by manipulate this fig.
You can use fig.show() for anytime to show your figure.

Categories

Resources