Paint each graph trace the same color in MATLAB - matlab

I have the following MATLAB code and I'm trying to make all of the plot traces black:
x = 20:0.01:30;
m1 = 25;
s1 = 2.5;
pdfNormal_1 = normpdf(x, m1, s1);
m2 = 25.478;
s2 = 0.1637;
pdfNormal_2 = normpdf(x, m2, s2);
m3 = 25.478;
s3 = 0.189;
pdfNormal_3 = normpdf(x, m3, s3);
set(gcf,'color','w');
g=findobj(gca,'Type','patch');
%set(g(1),'FaceColor',[0 .5 .5],'EdgeColor','w')
%set(g(2),'FaceColor',[0 1 1],'EdgeColor','w')
%set(g(3),'FaceColor',[0 1 1],'EdgeColor','w')
set(gca,'Fontsize',12,'Fontname','euclid')
xlabel(' ') %título eixo xx
hold all;
%plot(x, pdfNormal_1, x, pdfNormal_2, x, pdfNormal_3);
%set(gcf,'Color',[0 0 0])
plot(x,pdfNormal_1,'-.')
plot(x,pdfNormal_2,':')
plot(x,pdfNormal_3,'-','LineWidth',2)
Can someone help me? I removed the % from set(...);, but it plots nothing.

I don't quite know what exactly you want. You can either interpret this as:
You wanting all of your lines to be black
You wanting the background pane to be black.
Let's answer each question anyway so we have our bases covered.
Wanting all lines black
For the third parameter in plot, you can use a single letter that specifies what colour you want for your lines within your plot. Therefore, if you want all three of your plots black, use k after each invocation to plot. b is actually reserved for blue. Also, because you are calling plot more than once, each call to plot by default will overwrite the contents of the current figure with the latest invocation to plot, and so if you want all three plots to appear at the same time, you need to use hold on. Therefore, place this at the end of your code:
hold on;
plot(x,pdfNormal_1,'k-.')
plot(x,pdfNormal_2,'k:')
plot(x,pdfNormal_3,'k-','LineWidth',2);
You can also get rid of any set commands as these aren't useful. What you're actually doing is setting the background of the figure to white, which is probably not what you want to do. By background, I mean the area where the axes appear, not the drawing area of the plot itself.
Wanting the background pane black
If you want the background of where the plot appears to be black, it's a very simple one line statement. You need to set the current axes colour, not the current figure. Therefore, replace your set(gcf...) statement with set(gca...). Therefore:
set(gca,'Color',[0 0 0])
Now, if this is what you want, it'll be up to you to figure out what colours will appear on this plot nicely. Red certainly appears nice here!
For more information about how plot works, check out the documentation page on MathWorks: http://www.mathworks.com/help/matlab/ref/plot.html. It's actually very well explained!

Related

Draw over legend in a Matlab figure

I would like to place an arrow over a legend in a matlab plot but when I add the arrow, the legend defaults to being "on top" (see the picture, the black line being covered by the legend).
Is there a way to push a subfigure, such as an arrow, to the "top" so that it appears over all other component of the figure, including the legend? I've tried to use uistack but that doesn't seem to work with legends. uistack as the doc says should "Reorder visual stacking of UI components".
edit:
Very simple example: the line that I'm drawing should appear on top of the legend.
figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
uistack(l,'bottom');
You can make the legend background transparent - so you will see your arrow through the legend
figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
l.BoxFace.ColorData = uint8([255 255 255 127]');
l.BoxFace.ColorType = 'truecoloralpha';
The ColorData Property is [R G B Transparency]
For Info: This was done using R2015b.
You can copyobj the current graphical axes gca and set its Color property to none. This method will draw the line over the legend's patches and associated texts.
Explanation : Copyobj will copy and display all the axes related to the bar and line but not the legend (legends have axes on their own). The display of the copied axes will overlay perfectly with the original one. And 'Color','none' makes the white background of the copied axes transparent, thus making the legend visible again but visible under the line.
Here is the code
f = figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b, 'Location','SouthWest');
% add some magic
hax = copyobj(gca, f); % copy the current axes to the figure
set(hax, 'Color', 'none') % set the new axes's background transparent
What MATLAB version do you use? uistack seems to not work on legends anymore, since MATLAB 2015b (see this similar problem).
If, as you say, the line can be appear at any place, the best workaround may be choosing the best legend location
l = legend(b,'value','Location','Best','AutoUpdate','off');

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 multiple stacked plots

I've never seen a plot like the following (the plot in (a) ). Is it even possible?
According to the profile page of #Ander Biguri
Matlab can even make your dinner, if you know how to use it.
Which answers the question, if this is even possible ;-)
All we need is basic knowledge of the axes command - the rest is just tweaking to make it look nice. Let's have a look at it:
We'll start off by creating some sample data:
t = 100:220;
x1 = -(10*(t-130)).^2;
x2 = -(10*(t-150)).^2;
x3 = -(10*(t-170)).^2;
Then we'll create an initial figure with a white background
fig = figure(1);
set(fig,'Color','w');
Now we can create a new axes object and plot x1 on it:
ax(1) = axes('Position',[0.1,0.1,0.6,0.6]);
plot(ax(1),t,x1+10^4*rand(size(x1)),'-k',t,x1,'-r');
We'll remove the box around the axes, so only the x- and y-axes remain. Further we resize the plot, so we'll have enough space for the other two plots. We also set the color to none, i.e. transparent.
set(ax(1),'Color','none');
set(ax(1),'Box','off');
set(ax(1),'Position',[0.1,0.1,0.6,0.6]);
Now we need to create the second graph. We'll just create another axes object at a position which we like:
ax(2) = axes('Position',[0.2,0.2,0.6,0.6]);
plot(ax(2),t,x2+10^4*rand(size(x2)),'-k',t,x2,'-r');
set(ax(2),'Color','none');
set(ax(2),'Box','off');
and so on:
ax(3) = axes('Position',[0.3,0.3,0.6,0.6]);
plot(ax(3),t,x3+10^4*rand(size(x3)),'-k',t,x3,'-r');
set(ax(3),'Color','none');
set(ax(3),'Box','off');
And simple as that, we get something that doesn't even look that bad:
Using multiple waterfall plots, as Horchler suggested:
%// create some sample data
t=10:20:110;
x=0:1:200;
Y=bsxfun(#(x,t) normpdf(x,t,20),x,t.'); %//' fix the code formatting on SO!!
%// Make a colormap to to set the colour of the lines
colormap([1 0 0;0 0 0]);caxis=[0 1];
%// Plot the first set of lines (red ones)
h1=waterfall(x,t,Y,zeros(size(Y)));
set(h1,'FaceColor','none','LineWidth',2) %// tweak the properties
hold on
%// Plot the second set of lines (black lines), just the red lines with some noise
h2=waterfall(x,t,Y+0.002*(rand(size(Y))-0.5),ones(size(Y)));
set(h2,'LineWidth',2)
hold off
view([16 28])
we can get this:

How can I order items in a Matlab legend via the figure editor?

I've created a histogram with multiple sets of data. The data sets are color imgages converted to grayscale and taken over a given time period (e.g. pic 1 # time=0, pic 2 # time=5min, etc.) which is why I need the legend entries to show up in a specific order. When I put the legend in, the entries are scattered around in no specific order and I can't figure out how to get the entries switched up the way I need them.
In 2017a and higher (using part of the answer by Peter M above)
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X,Linux) to "Cut" the Plot line.
Create a new figure (File -> New -> Figure).
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line in the new figure.
"Cut" the Plot line in the new figure.
"Paste" the Plot line in the original figure.
Repeat steps 2-6 for every plot line, in the order that you want
them to appear in the legend. Right click on the legend and
"Refresh"
If you are regenerating the plot often of course it is probably a good idea to make sure that the script puts them in the correct order. Luis Mendo provided an answer here, but his answer to a slightly different wording was a little more detailed: how to change the sequence of the legend.
Pre 2017a Only! It seems that 2017a breaks this behavior, so the following trick does not work in the latest versions.
To answer your specific question, here is a neat trick if you are just doing this once in awhile on a plot that doesn't have many lines, and only want to use the Figure Editor...
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X, Linux) to "Cut" the Plot line.
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line.
Repeat steps 2-4 for every plot line, in the order that you want them to appear in the legend.
Right click on the legend and "Refresh"
You can get a handle to each plotted object, and in the legend use the handles to control which string applies to which plotted object.
Example:
h1 = plot(1:5, 1:5, 'r');
hold on
h2 = plot(1:5, 2:6, 'b');
legend([h1 h2],'First red','Second blue')
The answer above also works for R2017a and higher...here is a little bit more general example:
x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;
figure
plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');
legendEntries = get(gca,'legend')
plotHandles = get(gca,'children')
legendEntries = legendEntries.String;
newOrder = [3,4,1,2];
legend([plotHandles(newOrder)],legendEntries{newOrder})
flipud(h) or fliplr(h) to change complete order of the array up-down oder left-right
Through try and error I found this easy fix:
1) select the plot line that should be at position 1 (at the top)
2) Strg + X
3) Strg + V in the same plot figure
Repeat step 2 and 3 about 3 or 4 time. This will result in the plot beeing at the bottom of the legend. Now select the plot that should be at the 2nd position, again Step 2 and 3 a couple times. The previous plot moves one place up, the 2nd picked plot is now at the bottom.... continue with the rest of the plots the same way

Matlab - plot two pcolors on top of each other, with different colormaps

I'm plotting two pcolors on top of each other (using the m_map algorithm m_pcolor). The reason for this is that the second pcolor has transparency in it, and so shows the pcolor underneath. The first plot consists of just ones and zeroes, and I would like it to be just black and white. I would like the second to use the colormap jet, but I can't work out how to set one colormap without changing the other. My code at the moment is:
h1 = m_pcolor(Lon', Lat', black_background);
hold on;
h = m_pcolor(Lon', Lat', input_matrix);
Thanks in advance,
Adam
For this limited application, the easiest way is probably to append a row of zeros onto the colormap, deal with scaling (the clim property) yourself so that each plot takes advantage of the appropriate part of the colormap.
cm=colormap('jet'); %# Nx3
cm = [cm; 0 0 0]; %# append black row: (N+1)x3
h1 = m_pcolor(Lon',Lat',black_background);
set(h1,'clim',[length(colormap),length(colormap)])
hold on
h2 = m_pcolor(Lon', Lat', input_matrix);
set(h2,'clim',[length(colormap)-1, length(colormap)-1])
This should get you close, but I haven't tested it since I'm not on my matlab machine.
Another option is freezeColors from the file exchange (but this may only work for different axes within the same figure window, I'm not sure about different plots in the same axes object).