Automatically update a figure in a loop

One thing I found out quickly is that I could not update a matplotlib plot in Python, at least automatically.  Then I found out this little trick.  The idea is that at the end of the loop one must put a [cci lang="python"]fig1.canvas.draw()[/cci] (obviously change the [cci lang="python"]fig1[/cci] part so that it reflects the variable name of the figure).  It works very well and seems to be quite quick for simple things.

[cc lang="python"]
fig1 = figure()

for ii in range(30):

## Clear the figure
clf()

# Do a plot that changes for each
# iteration
plot( range(ii) )

# Now set the xlimit so that it
# doesn’t change the plot
xlim((0,30))
ylim((0,30))

## Now the magic, we must update the
## canvas
fig1.canvas.draw()

[/cc]

Display Image with Black Background

The Problem

There are times when an image is created where we don’t do a calculation for part of the image and want to display it black (for example) and not the colormap color which would correspond to zero.

For example, in the image:

original let’s say we are only interested in the block in the center and not the blue background around it.

We can create a background of NaN’s to represent the region which we want to not view, then use the alpha (transparency) channel to enable viewing only of the non-NaN region.

Code

Given a set of data, for example:

[cc lang="matlab"]

%% Create the test image
A = zeros(128,128);
A(33:96,33:96) = repmat( linspace(0, 1, 64), [64 1]);

figure(1),clf;
imagesc( A ); axis(‘off’); axis(‘square’);
colormap(jet);
colorbar;

print -f1 -djpeg original.jpg

%% The better image
B = nan*ones(128,128);
B(33:96,33:96) = repmat( linspace(0, 1, 64), [64 1]);

figure(2),clf;
imagesc( B ); axis(‘off’); axis(‘square’);
colormap(jet);
colorbar;

%% Now set the alpha map for the nan region
z = B;
z(~isnan(B)) = 1;
z(isnan(B)) = 0;

alpha(z);
set(gca, ‘color’, [0 0 0]);

print -f2 -djpeg blocked.jpg

[/cc]

Propagation of Errors for MTR Asymmetry

The Problem

It is important to calculate the propagation of errors for combinations of variables. There are some good resources on the interenet for linear propagation of error including Wikipedia (here).

It is assumed the MTR z-spectral data has means and standard devations for positive offsets (mmp, ssp) and means and standard deviations for negative offsets (mmn, ssn). The asymmetry is calculated as [cci lang="matlab"] mm = (mmp – mmn) ./ mmp[/cci].

Code

Given a set of data, for example:

[cc lang="matlab"]
function [mm,ss] = calcasym(mmp, ssp, mmn, ssn)

mmn = fliplr(mmn);
ssn = fliplr(ssn);

%% Calculate the asymmetry
mm = (mmp – mmn) ./ mmp;

%% Calculate the error.
mean_num = mmp-mmn;
std_num = sqrt( ssp.^2 + ssn.^2 );

mean_denom = mmp;
std_denom = ssp;

ss = (mean_num./mean_denom) .* sqrt( (std_num ./ mean_num).^2 + (std_denom ./ mean_denom).^2 );

mm = mm * 100; ss = ss * 100;
[/cc]