Coloring Different Models in Model Array Plots (Matlab) - matlab

I would like to visualize different responses of different systems in a single plot with Matlab's control toolbox, and to colorize the various curves so it is easy to differentiate between the different systems.
The response plots are easily created using the control toolbox - e.g. step response (using step), response to an arbitrary input (using lsim), etc.
When using separate model objects for different systems, It's easy to create multi-color plots, e.g., for a step response: step(Sys1, 'b', Sys2, 'r') would give one blue curve and one red cure, if Sys1 and Sys2 are both a single system model.
However, if plotting a model array, there's no way to differentiate between the various curves that belong to the same array. E.g.: step(SysArray, 'b') would make all curves blue. step(Sys,'b','r') is invalid - so no easy way to specify various colors.
Also, using the "Edit Plot" tool, selecting one curve effectively selects all curves, and any changes to the properties (e.g. line color) would affect all curves.
Is there any way to control the properties of each curve separately?

There's no built-in function to do this, so you have to roll your own functionality
% Create n-by-3 array of colours to use
coloursArray = rand(numel(stackedSystems),3); % for this example put in random colours
% Do the plot
step(stackedSystems);
title('My custom title');
grid on
% Change colours
ha = findobj(gcf,'type','axes','visible','on'); % Get handles of all axes (for MIMO responses)
for jdx = 1:numel(ha)
hl = findobj(ha(jdx),'type','line','visible','on'); % Get handles to all lines
for idx = 1:numel(hl)
set(hl(idx),'Color',coloursArray(idx,:)); % Change the colour
end
end

#Phil Goddard
There's no built-in function to do this, so you have to roll your
own functionality
% Create n-by-3 array of colours to use
coloursArray = rand(numel(stackedSystems),3); % for this example put in random colours
% Do the plot
step(stackedSystems);
title('My custom title');
grid on
% Change colours
ha = findobj(gcf,'type','axes','visible','on'); % Get handles of all axes (for MIMO responses)
for jdx = 1:numel(ha)
hl = findobj(ha(jdx),'type','line','visible','on'); % Get handles to all lines
for idx = 1:numel(hl)
set(hl(idx),'Color',coloursArray(idx,:)); % Change the colour
end
end
Thanks for the code!
Here are some slight changes to work better with model sampling through parameter variation, including the colorbar.
I was hoping it would also work with pzmap, for example, but it creates 2*n lines (poles and zeros), so it would need a bit more work.
function colorSamplingPlot(values,var)
% values is an array with parameter value assigned to each curve
% var is a string for the colorbar label
n = numel(findobj(gca,'type','line','visible','on'));
% Create n-by-3 array of colours to use
coloursArray = colormap(jet(n));
% Change colours
% Get handles of all axes (for MIMO responses)
ha = findobj(gcf,'type','axes','visible','on');
for jdx = 1:numel(ha)
% Get handles to all lines
hl = findobj(ha(jdx),'type','line','visible','on');
for idx = 1:numel(hl)
% Change the color; inverted color order to match colorbar
set(hl(idx),'Color',coloursArray(numel(hl)-idx+1,:));
end
end
colorbar;
caxis([min(values),max(values)]);
hcb = colorbar;
hcb.Label.String = vars;
end

Related

Separate initialization and display of plot

What is a proper way to separate the initialization and the display of plots in Matlab? (I mean plots in a wide sense here; could be plot, plot3, scatter etc.) To give a concrete example, I have a pretty complex 3D visualization that uses sphere and mesh to draw a static sphere mesh and then scatter3 to plot a moving trajectory on the sphere. To be able to do this in real time I have implemented some simple optimizations, such as only updating the scatter3 object each frame. But the code is a bit messy, making it hard to add additional features that I want, so I would like improve code separation.
I also feel like it might sometimes be useful to return some kind of plot object from a function without displaying it, for example to combine it with other plots in a nice modular way.
An example of what I have in mind would be something like this:
function frames = spherePlot(solution, options)
% Initialize sphere mesh and scatter objects, configure properties.
...
% Configure axes, maybe figure as well.
...
% Draw sphere.
...
if options.display
% Display figure.
end
for step = 1:solution.length
% Update scatter object, redraw, save frame.
% The frames are saved for use with 'movie' or 'VideoWriter'.
end
end
Each step might also be separated out as a function.
So, what is a neat and proper way to do stuff like this? All documentation seems to assume that one wants to display everything right away.
For example
% some sample data
N = 100;
phi = linspace(-pi, pi, N);
theta = linspace(-pi, pi, N);
f = #(phi, theta) [sin(phi).*cos(theta); sin(phi).*sin(theta); cos(phi)];
data = f(phi, theta);
% init plot
figure(1); clf
plot3(data(1,:), data(2,:), data(3,:)); % plot path, not updated
hold on
p = plot3([0 data(1,1)], [0 data(2,1)], [0 data(3,1)]); % save handle to graphics objects to update
s = scatter3(data(1,1), data(2,1), data(3,1), 'filled');
axis equal
xlabel('x'); ylabel('y'); zlabel('z');
t = title('first frame'); % also store handle for title or labels to update during animation
% now animate the figure
for k = 1:N
p.XData = [0 data(1,k)]; % update line data
p.YData = [0 data(2,k)];
p.ZData = [0 data(3,k)];
s.XData = data(1,k); % update scatter data
s.YData = data(2,k);
s.ZData = data(3,k);
t.String = sprintf('frame %i', k); % update title
drawnow % update figure
end
Basically you can update all values for a graphics handle, in this case 'p' and 's'. If you open the matlab doc for plot or plot3 you will find a link to all properties of that primitive: e.g. Line Properties. Similar documentation pages exist for scatter/imagesc etc.
So the general idea is to first create a figure with the first frame, save the handles to the objects you would like to update (p = plot(...), and then enter a loop in which you update the required property of that graphics object (e.g. p.Color = 'r', or p.XData = ...).

MATLAB: combining and normalizing histograms with different sample sizes

I have four sets of data, the distribution of which I would like to represent in MATLAB in one figure. Current code is:
[n1,x1]=hist([dataset1{:}]);
[n2,x2]=hist([dataset2{:}]);
[n3,x3]=hist([dataset3{:}]);
[n4,x4]=hist([dataset4{:}]);
bar(x1,n1,'hist');
hold on; h1=bar(x1,n1,'hist'); set(h1,'facecolor','g')
hold on; h2=bar(x2,n2,'hist'); set(h2,'facecolor','g')
hold on; h3=bar(x3,n3,'hist'); set(h3,'facecolor','g')
hold on; h4=bar(x4,n4,'hist'); set(h4,'facecolor','g')
hold off
My issue is that I have different sampling sizes for each group, dataset1 has an n of 69, dataset2 has an n of 23, dataset3 and dataset4 have n's of 10. So how do I normalize the distributions when representing these three groups together?
Is there some way to..for example..divide the instances in each bin by the sampling for that group?
You can normalize your histograms by dividing by the total number of elements:
[n1,x1] = histcounts(randn(69,1));
[n2,x2] = histcounts(randn(23,1));
[n3,x3] = histcounts(randn(10,1));
[n4,x4] = histcounts(randn(10,1));
hold on
bar(x4(1:end-1),n4./sum(n4),'histc');
bar(x3(1:end-1),n3./sum(n3),'histc');
bar(x2(1:end-1),n2./sum(n2),'histc');
bar(x1(1:end-1),n1./sum(n1),'histc');
hold off
ax = gca;
set(ax.Children,{'FaceColor'},mat2cell(lines(4),ones(4,1),3))
set(ax.Children,{'FaceAlpha'},repmat({0.7},4,1))
However, as you can see above, you can do some more things to make your code more simple and short:
You only need to hold on once.
Instead of collecting all the bar handles, use the axes handle.
Plot the bar in ascending order of the number of elements in the dataset, so all histograms will be clearly visible.
With the axes handle set all properties at one command.
and as a side note - it's better to use histcounts.
Here is the result:
EDIT:
If you want to also plot the pdf line from histfit, then you can save it first, and then plot it normalized:
dataset = {randn(69,1),randn(23,1),randn(10,1),randn(10,1)};
fits = zeros(100,2,numel(dataset));
hold on
for k = numel(dataset):-1:1
total = numel(dataset{k}); % for normalizing
f = histfit(dataset{k}); % draw the histogram and fit
% collect the curve data and normalize it:
fits(:,:,k) = [f(2).XData; f(2).YData./total].';
x = f(1).XData; % collect the bar positions
n = f(1).YData; % collect the bar counts
f.delete % delete the histogram and the fit
bar(x,n./total,'histc'); % plot the bar
end
ax = gca; % get the axis handle
% set all color and transparency for the bars:
set(ax.Children,{'FaceColor'},mat2cell(lines(4),ones(4,1),3))
set(ax.Children,{'FaceAlpha'},repmat({0.7},4,1))
% plot all the curves:
plot(squeeze(fits(:,1,:)),squeeze(fits(:,2,:)),'LineWidth',3)
hold off
Again, there are some other improvements you can introduce to your code:
Put everything in a loop to make thigs more easily changed later.
Collect all the curves data to one variable so you can plot them all together very easily.
The new result is:

Plot confusion matrix

I want to plot a confusion matrix in MATLAB. Here's my code;
data = rand(3, 3)
imagesc(data)
colormap(gray)
colorbar
When I run this, a confusion matrix with a color bar is shown. But usually, I have seen confusion matrix in MATLAB will give counts as well as probabilities. How can I get them? How can I change the class labels which will be shown as 1,2,3, etc.?
I want a matrix like this:
If you do not have the neural network toolbox, you can use plotConfMat. It gets you the following result.
I have also included an independent example below without the need for the function:
% sample data
confmat = magic(3);
labels = {'Dog', 'Cat', 'Horse'};
numlabels = size(confmat, 1); % number of labels
% calculate the percentage accuracies
confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
% plotting the colors
imagesc(confpercent);
title('Confusion Matrix');
ylabel('Output Class'); xlabel('Target Class');
% set the colormap
colormap(flipud(gray));
% Create strings from the matrix values and remove spaces
textStrings = num2str([confpercent(:), confmat(:)], '%.1f%%\n%d\n');
textStrings = strtrim(cellstr(textStrings));
% Create x and y coordinates for the strings and plot them
[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center');
% Get the middle value of the color range
midValue = mean(get(gca,'CLim'));
% Choose white or black for the text color of the strings so
% they can be easily seen over the background color
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
% Setting the axis labels
set(gca,'XTick',1:numlabels,...
'XTickLabel',labels,...
'YTick',1:numlabels,...
'YTickLabel',labels,...
'TickLength',[0 0]);
If you have the neural network toolbox you can use the function plotconfusion. You can create a copy and edit it to customise it further, for example to print custom labels.

How To Put String Labels on Contours for Contour Plots in MATLAB

I am wondering if it is possible to label the contours of a MATLAB contour plot with a set of user-defined strings?
I am currently using the following code snipper to produce a labelled contour plot:
%Create Data
X = 0.01:0.01:0.10
Y = 0.01:0.01:0.10
Z = repmat(X.^2,length(X),1) + repmat(Y.^2,length(Y),1)';
%Create Plot
hold on
[C,h] = contourf(X,Y,Z);
%Add + Format Labels to Plot
hcl = clabel(C,h,'FontSize',10,'Color','k','Rotation',0);
set(hcl,'BackgroundColor',[1 1 1],'EdgeColor',[0 0 0],'LineStyle','-',)
hold off
The issue with this code is that the labels are automatically generated by MATLAB. Even as I can easily change the contours that are labels, I cannot change the labels that they get.
Ideally, I would like to label them with a set of strings that I define myself. However if that is not possible, then I am wondering if it is possible to change the numeric format of the labels. The reason for this is that the code above actually produce a contour plot for an error rate, which I would like to display as a % value (i.e. use 1% in the contour label, instead of 0.01 etc.).
In this case, hcl is actually an array which stores handles to every contour label on your plot. When you set properties using the array (as in your code),
set(hcl, 'name', 'value')
You will set the property of every label to the same value.
You can change the properties of individual labels by iterating over the array. For example, this is how you would add a percentage sign:
for i = 1:length(hcl)
oldLabelText = get(hcl(i), 'String');
percentage = str2double(oldLabelText)*100;
newLabelText = [num2str(percentage) ' %'];
set(hcl(i), 'String', newLabelText);
end

How to show color gradient on scatter plot in matlab?

I have a scatter plot that will overlay several sets of data. Each set of data currently displays as the next color in the default colormap. This is my code right now:
figure
hold on
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:));
end
hold off
What i would like is to color code each set of data (indexed by i) to be the next color on a gradient. For example, the data for i=1 would be blue, i=5 would be purple, and i=10 would be red.
How could I do this?
You should add another parameter to scatter - called CData
https://www.mathworks.com/help/matlab/ref/scatter.html
Description: scatter(x,y) creates a scatter plot with circles
at the locations specified by the vectors x and y. This type of graph
is also known as a bubble plot.
In your example:
figure
hold on
colorVec = linspace(1,0, size(RunRawNetLength,1));
colorVec = transpose(colorVec);
colorVec = repmat(colorVec,[1 3]);
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:),'CData', colorVec );
end
hold off
Method 1:
You can vectorize your data, so you don't need a loop and than add a color by series:
% specify your color map:
colorCode = lines(size(RunRawNetLength,1)); % or any other colormap...
% define the correct color for each series:
coloVev = repmat(colorCode,size(RunRawNetLength,1),1);
% plot it all at once without a loop:
scatter(RunRawArea(:),RunRawNetLength(:),[],coloVev)
Method 2:
If you have the Statistics and Machine Learning Toolbox you can also do this with gscatter:
% define the series of data:
group = repmat(1:size(RunRawNetLength,1),1,size(RunRawNetLength,1));
% plot it all at once without a loop:
gscatter(RunRawArea(:),RunRawNetLength(:),group,colorCode);
if your colormap has not enough colors for all the series, then the function loops over and start from the first one.
Result:
In both cases the result for some random data will be (the main difference is that gscatter fill the data points and add the legend by default):
Note that I used the colormap lines which has only 7 entries, so in this example with 10 series it repeats itself.