Time trace data display with "uncertainty range"

Sorry in advance for asking a question about non-programming for SO, but the forces that should have determined that all MATLAB related questions belong here .

I did some Kalman filters and planned to evaluate state variables to see how they converge over time. Now I would like to visually present the covariance matrix, which is an indicator of estimation uncertainty. So I wrote a little function that colors the range around the score.

(Editing notes: the previous version made a mistake when using 2*cov for the width of each half-strip, when it should be 2 standard deviations)

 function [ls, regions] = plotuncertain( t, y, cov ) t = t(:); y = y(:); stdev = cov(:).^(1/2); a = ones(size(t)); regions(1) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y + 2*stdev; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none'); regions(2) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y - 2*stdev; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none'); ls = line('XData', t, 'YData', y); 

And it looks reasonable:

One cone of uncertainty

But I have two state variables with a similar value that I would like to build on the same axis at the same time.

Two cones of uncertainty

Uh-oh, the initial data for k_1 is shaded (upper half) by the k_2-band. MATLAB did not draw lines and patches in the order in which I submitted them. And even if I manage to control the order they draw, alpha blending is even less optimal than blending colors based on probabilities.

Any ideas how to do both at the same time? Can I somehow take advantage of the fact that I use two different color planes for two variables?

+6
source share
2 answers

When the graphs are too complicated, Matlab starts to behave randomly, like here. I often try to apply the solution described here .

For your specific (and very pleasant) plot, I would change the function by declaring the axis:

 function [ax, ls, regions] = plotuncertain( t, y, cov ) ax = axes; t = t(:); y = y(:); cov = cov(:); a = ones(size(t)); regions(1) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y + 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none'); regions(2) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y - 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none'); ls = line('XData', t, 'YData', y); 

and then call the function with:

 [ax1, ls, regions] = plotuncertain( t, y, cov ); [ax2, ls, regions] = plotuncertain( t, y, cov ); set(ax2,'Visible','off'); linkaxes([ax1 ax2],'xy'); %or any (XLim,YLim) settings 

Thus, transparency in one axis does not depend on another.

EDIT

A way to better control color mixing is to convert each of the axes created in the selected shape into an image, and then merge them. For example, you can use imfuse(im1,im2,'blend') (image processing toolbar) or any other function that mixes 2 images.

The method of extracting images from a figure

 F = getframe(gcf); imwrite(F.cdata, 'image.png'); 

enter image description here

Of course, this solution is suitable only at the last stage of the reporting process (I would not completely use it for a scientific article - see comments, but for impressive presentations). It may also be more efficient to use alternative software that handles transparency / OpenGL better than Matlab does.

+3
source

Although you suggest trying this, I found that replacing patches has changed a lot.

My version of your example, first without sharing:

 t=[1:100]; y=t.^2; cov=t.^2; t2=[1:100]; y2=t2.^2.05; cov2=t2.^2; figure t = t(:); y = y(:); cov = cov(:); a = ones(size(t)); t2 = t2(:); y2 = y2(:); cov2 = cov2(:); a = ones(size(t2)); regions(1) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y + 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'r'); regions(2) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y - 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'r'); ls = line('XData', t, 'YData', y,'Linewidth',1.5); regions(3) = patch('XData', [t2; t2(end:-1:1)], ... 'YData', [y2 - 2*cov2; y2(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'b'); ls = line('XData', t2, 'YData', y2,'Linewidth',1.5); regions(4) = patch('XData', [t2; t2(end:-1:1)], ... 'YData', [y2 + 2*cov2; y2(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'b'); 

not swapped

then with replacement:

 regions(1) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y + 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'r'); regions(3) = patch('XData', [t2; t2(end:-1:1)], ... 'YData', [y2 - 2*cov2; y2(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'b'); ls = line('XData', t2, 'YData', y2,'Linewidth',1.5); regions(2) = patch('XData', [t; t(end:-1:1)], ... 'YData', [y - 2*cov; y(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'r'); ls = line('XData', t, 'YData', y,'Linewidth',1.5); regions(4) = patch('XData', [t2; t2(end:-1:1)], ... 'YData', [y2 + 2*cov2; y2(end:-1:1)], ... 'FaceVertexAlphaData', [0*a; a], ... 'FaceAlpha', 'interp', 'EdgeColor', 'none', 'FaceColor', 'b'); 

swapped

Great effect, I have not seen this before in Matlab.

+2
source

Source: https://habr.com/ru/post/949999/


All Articles