Matlab gplot extra customly defined colors - matlab

I have a question regarding Matlab's function gplot. I would like to call gplot several times in a for-loop to plot several graphs/paths in the same figure.
However, I would like to use more colors than standardly available. With the ordinary plot command this can, for example, be done in the following way:
plot([1 2],[3 4],'Color',[rand rand rand])
but this seems not possible within gplot. Does anyone know how to do this. My code is currently:
col=char('r', 'g', 'm', 'y','r', 'g', 'm', 'y')
hold all
for i=1:k
gplot(Cell_Matrix{i},coordinates,col(i))
end
hold off
So now I have repeated using the same color (excluding some I do not want), but I would like to add way more new colors (around 25 extra).

Not the cleanest solution, but you could manipulate Matlab's default plotting order. So before your existing code, call:
figure;
colors = get(gca,'ColorOrder');
colors now contains the color data for Matlab's default color cycle when using hold all. You should be able to delete and add rows of RGB values to this matrix.
For example, to just use k random colors:
colors = rand(k, 3);
Once this matrix is set to your satisfaction, just replace the color order in your current axes with:
set(gca,'ColorOrder',colors)
Then execute a modified version of the remainder of your posted code:
hold all;
for i=1:k
gplot(Cell_Matrix{i},coordinates)
end

Related

pzmap or pzplot color handle for multiple plots

I am plotting a closed loop poles of a systems using pzmap.m or pzplot.m whichever you prefer. I want to see the movement of poles and zeros as I change a variable depicted by L.
The function does not have a direct handle for color. In the example you can just choose the standard colors but cannot give your own color. Since I have to plot multiple times on the same figure, I create a handle for every iteration in a for loop and use findobj to set the color of the curve. To get the colors I want to have a colorbar. So I use jet to get the color distribution for my graph. But the market size stays the same and I have one ugly looking figure.
MWE:
cb=jet(10);
s=tf('s');
L_array=5:5:50;
figure; hold on;
for i=1:length(L_array)
L=L_array(i);
G=((58.2+11.7*L)*s^2*25^2+(3996.8 + 815.7*L)*s*25+815.7*25^2)/(s^2*(s^2*25^2+126.9*s*25+(3996.8+1.9*25^2)));
CL=feedback(G,1);
pzmap(CL);
h = findobj(gcf,'type','point'); set(h,'markers',12,'color',cb(i,:));
clear L G CL h
end
grid;
c=colorbar
c.Label.String = 'L'
If you run it you'll see that the size doesn't change and graph looks crazy with the y axis on either side labelled with ticks. I want a proper colorbar from blue to red and the colors distributed properly but can't get it after multiple tries. If I can get less cluttering that would be helpful too.
The are several problems in your code
h = findobj(gcf,'type','point'); The things drawn in the screen are actually of type 'line'!
Every iteration, you catch all the points, modify them an dimdediately after delete the properties with clear.
Additionally, h = findobj(gcf,'type','line'); will not return a single thing, but a set of them, so you need to index through it to set the properties. My modified code working looks like this:
clear;clc
cb=parula(10);
s=tf('s');
L_array=5:5:50;
figure; hold on;
for i=1:length(L_array)
L=L_array(i);
G=((58.2+11.7*L)*s^2*25^2+(3996.8 + 815.7*L)*s*25+815.7*25^2)/(s^2*(s^2*25^2+126.9*s*25+(3996.8+1.9*25^2)));
CL=feedback(G,1);
pzmap(CL);
end
% lets do this in the end!
% you will have length(L_array)*2 +1 items in h
h = findobj(gca,'type','line');
for jj=2:length(h)
set(h(jj),'MarkerSize',12,'Color',cb(floor(jj/2),:));
end
grid;
colormap(parula); % I am using parula, you can use jet, but I discourage it
c=colorbar;
PD: there are tons of reasons not to use jet!! Use perceptually uniform colormaps please!

Filling markers when plotting in a loop

I am trying to plot 31 different vectors in a single figure in MATLAB. I am using a circular marker 'o', for plotting every vector and I want to have every vector painted in a different color and also I want to fill the markers as well with the same color as the markers edges.
I am using the following code:
while (n<=31)
plot(x(n),y(n),'o',rand(1,3)) % Not filled markers
n=n+1;
end
The problem is that since I am using a random for the color selection, when I try to run the following code:
while (n<=31)
plot(x(n),y(n),'o','MarkerFaceColor',rand(1,3)) % Filled markers
n=n+1;
end
The marker edge and the marker filling have different colors. I don't know how to fix this, maybe I shouldn't use the random for selecting the color but I don't know how to fix it, to obtain the same color in the marker edge and in the filling.
I would recommend using scatter for this rather than creating n different plot objects that you then have to manage. Using the CData property it is possible to set a separate color for each of your data points.
colors = rand(31, 3);
x = rand(31,1);
y = rand(31,1);
s = scatter(x, y, 'filled', 'CData', colors);
'MarkerEdgeColor' is the property you want, in addition to FaceColor
while (n<=31)
c = rand(1,3);
plot(x(n),y(n),'o','MarkerFaceColor', c, 'MarkerEdgeColor', c);
n=n+1;
end

Different color line with plotpc in matlab

I am plotting two plotpc charts (i want to see decision boundaries from perceptron and from Bayesian net) and I need them to have different color.
plotpv(P,T);
hold all;
plotpc(net.IW{1,1},0,'r');
plotpc([w1(maxind(1)), w2(maxind(2))],0,'g');
title('Decision boundaries');
However all my trials ended up with failure and I always get same colors like this:
Thank you for help.
You need to assign the output of plotpc (line handle) to some variable, which you can then use to alter line appearance, e.g.
hPlot = plotpc(net.IW{1,1},0);
set(hPlot, 'Color', 'r');
Also, I don't think plotpc accepts a color as a third argument - you should get a warning when you do that.

Customized Legend in Matlab

Here I have a for loop to plot the content of my matrix.
Based on 'type' value I determine the plot object (ks,bO,rX)
for k = 1:length(data1)
if(type(k)==1)
h=plot(data1(k,1),data1(k,2),'ks');set(h,'linewidth',2);hold on;
elseif(type(k) ==0)
h=plot(data1(k,1),data1(k,2),'bO');set(h,'linewidth',2); hold on;
else
h=plot(data1(k,1),data1(k,2),'rX');set(h,'linewidth',2); hold on;
end
end
I am little bit confused to find a way to put legend in my final figure, which shows my own explanation regarding each object(ks,bO,rX).
By default, MATLAB will see the output of this loop as not three plots but as many individual plotted points. Even though some of the points are plotted with the same settings, it doesn't automatically recognise them as part of the same series. If you give it three legend entries, it will assign them to whatever the first three points plotted were.
The simplest way around this is to change the way you plot and use logical indexing, rather than a loop:
h=plot(data1(type==1,1),data1(type==1,2),'ks'); set(h,'linewidth',2);
hold on;
h=plot(data1(type==0,1),data1(type==0,2),'bO'); set(h,'linewidth',2);
h=plot(data1(type==-1,1),data1(type==-1,2),'rX'); set(h,'linewidth',2);
Now we have only three plots, so giving legend the three should give us the correct match between those plots (in the order they were plotted) and our labels:
legend({'Type 1'; 'Type 0' ; 'Type -1'})

How do I visualize a matrix with colors and values displayed?

I want to create images like this from a double precision matrix using MATLAB.
Sample image:
http://twitpic.com/2xs943
You can create this sort of plot yourself pretty easily using the built-in functions imagesc and text and adjusting a number of parameters for the graphics objects. Here's an example:
mat = rand(5); % A 5-by-5 matrix of random values from 0 to 1
imagesc(mat); % Create a colored plot of the matrix values
colormap(flipud(gray)); % Change the colormap to gray (so higher values are
% black and lower values are white)
textStrings = num2str(mat(:), '%0.2f'); % Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings)); % Remove any space padding
[x, y] = meshgrid(1:5); % Create x and y coordinates for the strings
hStrings = text(x(:), y(:), textStrings(:), ... % Plot the strings
'HorizontalAlignment', 'center');
midValue = mean(get(gca, 'CLim')); % Get the middle value of the color range
textColors = repmat(mat(:) > midValue, 1, 3); % Choose white or black for the
% text color of the strings so
% they can be easily seen over
% the background color
set(hStrings, {'Color'}, num2cell(textColors, 2)); % Change the text colors
set(gca, 'XTick', 1:5, ... % Change the axes tick marks
'XTickLabel', {'A', 'B', 'C', 'D', 'E'}, ... % and tick labels
'YTick', 1:5, ...
'YTickLabel', {'A', 'B', 'C', 'D', 'E'}, ...
'TickLength', [0 0]);
And here's the figure this generates:
If you run into trouble with the x-axis tick labels you choose being too wide and overlapping one another, here's how you can handle it:
Newer versions of MATLAB: Not sure which version this was added, but in newer versions axes objects now have the properties '{X|Y|Z}TickLabelRotation', which allow you to rotate the labels and fit them better.
Older versions of MATLAB: For older versions you can find some submissions on the MathWorks File Exchange that can rotate the tick label text, like XTICKLABEL_ROTATE from Brian Katz.
h = imagesc(magic(8))
impixelregion(h)
http://www.mathworks.com/help/toolbox/images/ref/impixelregion.html
Requires Image Processing Toolbox
If you only care about looking at zero/non-zero entries in your matrix (e.g. if it's sparse), use spy.
Else, use imagesc.
PS: I can't access your image
I expect you could persuade Matlab to draw that, if you look at the File Exchange you may find someone has already written the code. But it would be a lot easier, if you don't have the code, to use MS Excel.
EDIT: So I gave this some more thought and here's what I came up with. I've not mastered posting graphics to SO, so trust me, this will lead you towards a solution. But it would honestly be easier with Excel.
First define a matrix with your data values; I call the matrix G in the following. Then execute the commands:
image(G);
colormap(gray)
Now, I had to do some fiddling around, rescaling the data, to get a good graphic, but this should produce a gray-scale plot with numeric axes. Now, go to your figure window and open the plot tools.
Select the X axis and hit the Ticks button. All you have to do now is edit the labels to the texts that you want. Do the same for the Y axis. Write the numbers in the squares on the plot -- use the Text Box from the Annotations menu.
After a lot of fiddling about you'll have the graphic you want. At this point, I suggest that you choose the menu command File | Generate M-File and do just that. If you want to create such graphics programmatically in future just turn the generated M file into a proper function that does what you want.
But it's still a lot easier in Excel.