Filling markers when plotting in a loop - matlab

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

Related

Change existing plot lines according to colormap

I have a plot with a few lines in Matlab and I want to control the line colors post-hoc:
figure; hold on;
for ind=1:4
plot(rand(1,10))
end
I know I can use
set(0,'DefaultAxesColorOrder',summer(4))
before plotting, to change the plot line colors, but (how) can I achieve the same thing after looking at the plot? Possibly try-out a few different colorschemata?
Each plot by default takes its color from the property 'ColorOrder' of their axis, which in turn is taken by default from the 'DefaultAxesColorOrder' of the groot object.
Once the plots have been created, you need to modify their colors individually; changing the above mentioned properties will not affect them:
g = findobj(gca, 'Type', 'line'); % children of current axis that are lines
c = summer(numel(g)); % desired color scheme, with that many colors
for k = 1:numel(g)
set(g(k), 'color', c(k,:));
end

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!

Matlab - Colour markers in plot according to criteria

I have a quick question on a matlab plot. I want to draw a line with round markers on each point, but I want the markers to have a different color according to some criteria. I managed to display on the markers the different criteria but I was not able to change their colours.
To be more specific on the code below:
d3 is a (1x240) vector of y values for the plot.
RiskierInd is a (1x240) matrix with the criteria (it has values from
1 to 12).
What I want is the markers with criteria 1 to be in one colour, markers with criteria 2 to be with another colour, and so on and so forth.
The code below plots the line with the markers and with the criteria inside the markers.
% plot the data
figure
d3 = vals;
n = 1:numel(d3);
plot(n,d3, '-ob','markersize',10,'markerfacecolor','w');
for idx = 1:numel(d3)
text(n(idx),d3(idx), num2str(RiskierInd(idx)),...
'FontSize',8,...
'HorizontalAlignment','center');
end
I did check this post which is similar, but couldn't figure out how to implement it.
Also, is it possible to add a legend with the color of the markers afterwards?
You can use scatter instead of plot for this. You can replace
plot(n,d3, '-ob','markersize',10,'markerfacecolor','w');
with
hold on
plot(n, d3,'b-');
scatter(n, d3, [], RiskierInd, 'filled');
caxis([1 12]);
Then, to display the correspondence between the colors and the values you can simply add colorbar to your code.
Edit If you want to define custom colors, you can use colormap with a custom n-by-3 array of RGB colors. For instance, to have exactly 12 colors you can do:
colormap(jet(12));
Best,

Overlaying data on an image in matlab

I have two plots I want to overlay on top of each other as shown in the link below:
The inputs are the two images on the left and the output is the image on the right. Here's the code I used:
reference = imread('ref_foam.png');
figure, imshow(reference);
hold on;
h = imshow(data,[]);
hold off
colormap jet;
alphamap = zeros(size(reference,1),size(reference,2));
for i = 0:size(data,1)-1
for j = 0:size(data,2)-1
if(~(data(i+1,j+1) == 0))
alphamap(i+1,j+1) = 0.75;
end
end
end
set(h, 'AlphaData', alphamap);
Anytime there's a zero in the data array, it sets that transparency to zero or else it will set the transparency to 0.75.
Now, my questions are: How can I get the colormap to apply to only the data array? In this example it works but if I convert "reference" to a grayscale, the colormap applies to that as well. The input for the colormap is an axes handle, is there anyway to input the image's handle (h) so that it only applies to the top (data) array? Also, I'd like to implement a colorbar as well. Is there anyway to apply the colorbar only to the data array? Thanks.
You probably want to use the subimage command - it lets you create images on the same figure with different colormaps.
Once create and modified as in your script, overlay the images by setting their axes positions on top of each other.

MATLAB: line specification marker size

When plotting multiple data series using both line specification (X,Y,linespec) triplets and (PropertyName,PropertyValue) doublets, only a single MarkerSize can be specified and this size applies to all data series. For instance,
plot(X1,Y1,'.b',X2,Y2,'-r','MarkerSize',5)
Is it possible to specify a different MarkerSize for each of the different data series without resorting to plotting the data series separately or subsequently altering plot handle properties? Neither of the following two commands is valid, but they give an idea of the desired result:
plot(X1,Y1,'.b',X2,Y2,'-r','MarkerSize',[5 10])
plot(X1,Y1,'.b','MarkerSize',5,X2,Y2,'-r','MarkerSize',10)
Try:
h = plot(X1,Y1,'.b',X2,Y2,'*r');
set(h(1),'MarkerSize',5);
set(h(2),'MarkerSize',2);
You can use scatter. It has the SizeData property which is a vector.
x = rand(10,1);
y = rand(10,1);
s = scatter(x,y);
set(s,'SizeData',linspace(1,100,10))
If you want to use line plot with markers, you can draw your plot, use hold on, and then draw scatter on top of it.
For that it is probably
plot(x1,0,'+','MarkerSize',10)
Or any other plot within the loop just
plot(x?, 0, '+', 'MarkerSize', 10, 'MarkerEdgeColor', 'r')