How can I multiple plot in one figure at Matlab? - matlab

Hi I'm trying to implement as following code.
plot(bins,r);
plot(bins,g);
plot(bins,b);
But I want to plot in one figure.
Is there any way?

For multiple plots in the same figure and not the same axis. You have to use subplot(x,y,z). The first argument 'x' is the number of plot you want to produce, in your case 3. Second 'y' just adjusts the size of the plots, you can use 1. The third 'z' is the position of the plot, whether a certain plot comes first, second or third.
subplot(3,1,1)
plot(bins,r);
subplot(3,1,2)
plot(bins,g);
subplot(3,1,3)
plot(bins,g);
To distinguish between all three plot you can add another argument to plot() so that you can change colors. For example:
plot(bins,r,'r')
'r' will make the color of the plot red, 'b' makes it blue, 'k' makes it black...so on.

Yes, you can plot everything in one go:
plot(bins,r,bins,g,bins,b)
or use hold on after the first call to plot.

You need to use hold on
hold on retains plots in the current axes so that new plots added to
the axes do not delete existing plots. New plots use the next colors
and line styles based on the ColorOrder and LineStyleOrder properties
of the axes. MATLABĀ® adjusts axes limits, tick marks, and tick labels
to display the full range of data.
hold on
plot(bins,r)
plot(bins,g)
plot(bins,b)

Related

make different subplot have the same range of colors on matlab

I'm trying to plot several data on subplots, but each one have its own range of color (colorbar) and I want all to be the same. I can change the labels of the colorbar but if ranges are too different it would mostly show only one color. Is there a way to make matlab think that its all just one plot and then decide the colorbar scale accordingly?
To change colorbar label its quite easy:
allaxes = findall(f, 'type', 'axes');%f is the figure handle
for i=1:length(allaxes)
cc=get(allaxes(i),'Colorbar');
mm(i,:)=get(cc,'Limits');
end
limits=[min(min(mm)) max(max(mm))];
for i=1:length(allaxes)
cc=get(allaxes(i),'Colorbar');
set(cc,'Limits',limits);
end
Here there is an example (look for the one on the right):

Legend each curve in a single figure from a matlab plot

I want to plot four curves in a single figure from matlab, so I am using hold on. Furthermore, I want to create an legend to each curve, so I wrote the code:
clear all
x=linspace(0,10,100);
x2=linspace(-5,15,100);
x3=linspace(-10,20,100);
x4=linspace(35,40,100);
figure(1)
plot(x,x2)
legend('x2')
hold on
plot(x,x3)
legend('x3')
hold on
plot(x,x4)
legend('x4')
hold on
plot(x,x)
legend('x')
hold off
But the result is that all my curves are in the same color, and just the last legend "x" has appeared in the figure (see it below).
How can I set one legend to each curve? All curves must have different colors.
This depends a bit on your matlab version. In older versions (and in octave), plots added through the use of hold on get the same color. In R2015b (i don't know when this was introduced), the individual plots get different colors, but still only one legend is displayed.
To get multiple colors and multiple legend entries, you can specify all data to be plotted in one call, the same for the legends:
plot(x, x, x, x2, x, x3, x, x4);
or
plot(x, [x', x2', x3', x4']);
For the legends, approach the same way:
legend('x', 'x2', 'x3', 'x4');
If you want to build up a legend without knowing the number of entries before hand, you would want to search for "dynamic legends". See for example here:
http://undocumentedmatlab.com/blog/legend-semi-documented-feature

How to plot contours with selected colors and formatted labels

I'm trying to plot contour using my computed data with limited contour labels and and colors as given in the top panel of this image:
But I ended up with a slightly different plot (see the plot in the bottom of the above image).
I want to modify my plot with the following three specifications
Restrict contour labels in 2 or 3 decimal places
Remove plot labels in the area where the contours are too close to each other.
Plot with two colors as in the first image
Here is my code:
f=load('fort.15');
ngridx=180;
ngridy=180;
x=f(:,3);
y=f(:,4);
z=f(:,5);
xlin=linspace(min(x),max(x),ngridx);
ylin=linspace(min(y),max(y),ngridy);
[X,Y]=meshgrid(xlin,ylin);
Z=griddata(x,y,z,X,Y,'linear');
[c,h] = contour(X,Y,Z,20);
set(h,'LineWidth',2,'LineColor',rgb('SteelBlue'),'ShowText','on',...
'LabelSpacing',800 )
axis([0 6 -5 7])
I'm not an expert in Matlab. Please help me get the right plot.
I'm attaching my data file here.
Well, I got only 2 of 3. Deine the level in which the color has to change (here scl) and you good to go:
scl = 6.5; % switch color level;
[c1,h1] = contour(X,Y,Z,scl:max(Z(:)),'Color','r');
hold on
[c2,h2] = contour(X,Y,Z,min(Z(:)):scl,'Color','b');
clabel(c2,h2);
axis([0 6 -5 7])
The idea here is to builed your plot from two contour objects, using hold on command. the vector scl:max(Z(:)) define the levvels to show in the first contour, and the get the red color and no lables. And a similar logic works for the secound contour.
If you want to lable some red contours, or remove lables from the blue ones, you need to replace h2 in the clabel function with a vector of the levels you want to lable. If you will be mo specific in the comments I'll update my answer.
Changing the formatting of the lables, is probably possible somehow, but it's really not trivial, so I left it by now.

Axis commands changes TightInset property to zero

I use some things from this question get-rid-of-the-white-space-around-matlab-figures-pdf-output to get rid of the white space when saving figure plots and images. This works fine, but my problem is when I use commands like "axis square" or "axis image". Ivt sets TightInset property of corresponding axes to 0 in "y" direction. I mean when I use this:
inset=get(a,'TightInset');
second and fourth numbers in "inset" are always zero, even if I set title and x-label. So in the end I don't see plot titles and x-labels.
Can I fix it somehow? So far I do it manually by adding some suitable number, but it is not convenient.
EDIT: Some more code for example. It displays two histograms.
h=figure;
subplot(121)
bar(bins,histogram1,'hist');
axis square
title('Historgram 1');
xlabel('Intensity');
ylabel('Pixel count');
set(gca,'xlim',[0 1])
subplot(122)
bar(bins,histogram_out,'hist');
axis square
title('Histogram 2');
set(gca,'xlim',[0 1])
xlabel('Intensity');
ylabel('Pixel count');
and if I call
a=get(h,'Children');
for i=1:length(a)
inset=get(a(i),'TightInset');
%...some stuff here
end
those y-related numbers in inset are zeros. If I comment axis square and do this, then inset are correct and it does what I need.
As far as I know when you use 'TightInset' you'll get only the graph or image, axis will be removed. I downloaded a function from mathworks file exchange 'saveTightfigure.m' to solve a similar problem. But I did not needed axes. If you need axes may be you can edit limits set inside the function. I mean you can give a little more space at left and bottom for keeping the axes.

Color circle at end of stem in stem plot depending on value

I have a script that plots a Fourier series using stem(). I know that I can fill the circle at the end of the stems with stem( , 'fill'). However I would like it to only fill the circle if the magnitude is negative, or alternatively, fill all of them, but have an outer circle of another color on those that have a negative magnitude.
I tried using a variable inside stem(), and also a full if-else statement, but both returned errors.
Any way this can be done?
You can remove marker from stem plot with 'Marker' property set to 'none'. Then add markers where ever you like with plot or scatter function. Add hold on after the stem command and hold off after all plotting commands.
For example:
x = randn(20,1);
stem(x,'Marker','none')
hold on
plot(find(x>0),x(x>0),'ro')
plot(find(x<0),x(x<0),'bx')
hold off