Apply variable marker position to matlab plots - matlab

I have a problem in matlab. I want to plot a graph having 5 plots. Let me go through them.
x axis for each data is from 1:500.
For plot 1 to 3, I want to place marker after every 10 values, whereas for plot 4 to 5 I want to place markers after every 5 values. Is it possible to do it ?
I followed a code something like this:
figure,
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
set(gcf,'Color','white');
plot(ObjVal1(1:10:end),'*r','LineWidth',3);
hold on;
plot(ObjVal2(1:10:end),'-.b','LineWidth',3);
plot(ObjVal3(1:10:end),'+-k','LineWidth',3);
plot(ObjVal4(1:5:end),'sm','LineWidth',3);
plot(ObjVal5(1:5:end),'.b','LineWidth',3);
hold off;
title({'Fitness Value'},'FontWeight','bold','FontSize', 12,'Color','black');
xlabel('Fitness Value --->','FontWeight','bold','FontSize', 12,'Color','black');
ylabel('Iterations --->','FontWeight','bold','FontSize', 12,'Color','black');
legend('CV GDS','CV Momentum','CV Exct LS','CV Back Track','CV Conjugate GDS');
Then I get an output like this :
The problem is quite evident from the picture. The plots of 1-3 is given for 50 values as the subplot is taken for each 10 iterations whereas the 4th and 5th plot is given for 100 values as the subplot is taken for each 5 iterations. I do not want to do this. Basically I want the plot of all the values but with the markers placed at each 10 iterations for plot 1-3 and at each 5 iterations for plots 4-5.
Thanks everybody in advance for your help !

Use first argument to plot to specify x-axis positions of the markers:
plot(1:10:numel(ObjVal3), ObjVal3(1:10:end),'+-k', 'LineWidth', 3);
plot(1:5:numel(ObjVal4), ObjVal4(1:5:end), 'sm', 'LineWidth', 3)

Related

Which type of MATLAB plot would produce spikes on a curve based on certain condition?

I have a 29736 x 6 table in MATLAB. The 6th column of the table consists of zeroes and ones. I would like a plot between the Sample No (1 to 29736 of Table) and the 6th column (ones and zeroes), such that there is a nice spike whenever a 1 occurs and a regular curve when 0 occurs. Can someone suggest what line of code/function can do this, and how to systematically go about it?
EDIT:
I used the following code and got an unwanted result (a solid blue block):
stem(table_fault_test_data.Fault_Condition, 'Marker', 'none');
set(gca, 'YLim', [0 2]); % Adjust the y-axis range
I basically want to refer to the 6th column of my table which contains ones and zeroes, and plot spikes for only the ones.
Are you perhaps looking for a stem plot? You can make one without a marker, so you get spikes for ones and zero for zeroes:
data = rand(1, 100) < 0.2; % Some random sample data
stem(data, 'Marker', 'none'); % Make the stem plot
set(gca, 'YLim', [0 2]); % Adjust the y-axis range

Matlab scatter plot with straight lines connecting the points

Is there an easy command to have a plot like the blue line in the picture (excel)? Matlab defaults to produce something like the line in red. The only way I know to do this is to issue a plot command for each segment of the line:
for i=2:n-1
plot([data(i-1,1) data(i,1)],[data(i-1,2) data(i,2)],'-b'); hold on;
end
You can just plot the entire array and let plot automatically draw straight line segments between each of the points. This is the default behaviour when plotting things in MATLAB. MATLAB plotting smooth lines is not the default behaviour when the plot is produced, so I'm not sure where you're getting that information.
You would need to perform some sort of spline interpolation to get the red line, but you desire the blue curve and so plotting the entire array in a single plot command should suffice.
It's as simple as:
plot(data(:,1), data(:,2), '-b');
Just to be sure that we're on the same page, I'm going to reproduce your data then use the above command to plot the data so you can see for yourself that the behaviour you desire is achieved:
data = [0 0; 1 1; 2 4; 3 6; 4 4]; %// Your data reconstructed
plot(data(:,1), data(:,2), '-b'); %// Main plotting code
%// Some extras
xlim([0 4.5]);
ylim([0 7]);
grid;
I've added in some extra code to get the plot to look like your example. I've made the x-axis limits go up to 4.5 and the y-axis limits go up to 7. I've also placed a grid in the plot.
We get:

How to overlay a stem plot over a box plot in MATLAB?

I have a vector
A=[1 2 3 3 3 4 5]
I am able to show its box plot with
boxplot(A, 'orientation', 'horizontal')
I can also show its distribution with a stem plot like this
[nelements, centers] = hist(A);
stem(centers, nelements/numel(A), 'blue');
My question is how to combine these two plots into one figure? The figure should have its y-axis as probability and x-axis as the A values.
As for the height of the box plot in the figure, it does not matter.
How may I do this?
Use hold on as follows, and be aware that you need to vertically move (using 'position' again) your boxplot to fit the axes of stem
boxplot(A, 'orientation', 'horizontal','position',0.1); hold on
[nelements, centers] = hist(A);
stem(centers, nelements/numel(A), 'blue'); hold off

Using xticklabel with strange results

I have created a scatter plot and am attempting to set the xticklabel property of the set function but not all of the labels are printing on the plot. As you can see in the attached, there are 11 x values. I am including 11 string values in the set function, but only 7 are appearing on x-axis of the plot.
What am I doing wrong?
x is a 242x1 vector
haz is a 242x1 vector
ls is a 242x1 vector
scatter(x,haz,30,ls,'filled');
set(gca,'xticklabel',{'6M';'1Y';'2Y';'3Y';'4Y';'5Y'; ...
'7Y';'10Y';'15Y';'20Y';'30Y'});
title(['Implied hazard rates']);
xlabel('Tenor')
colormap('Summer');
colorbar;
hold on;
Your problem is that your tick labels don't match up with where you think the ticks should be. You need to tell the axes to put ticks at each of the x-locations in your plot, probably like this:
set(gca, 'XTick', unique(x));
% Now set your tick labels...

Changing axes and color of plots in Matlab

How do you get rid of the axes and dotted line grids when you plot in Matlab? Also, how do I make subplots of subplots. Since that's probably not very clear, what I mean is the following...
Let's say I have a 10x10x10 .mat file, so I open each of the 10 frames and plot what I have on each 10x10 frame. I generate 2 different plots for each frame so that in total there are 20 plots. For each frame I generate 2 subplots. When I run the code, I get 10 different figures with 10 subplots. I'd like to get for this example 1 figure with 20 subplots where the first two refer to the first iteration, second two refer to the second, etc.
for i = 1:10
z=z(:,:,i);
figure(i)
subplot(1,2,1)
surf(z)
%code, obtain new array...
subplot(1,2,2)
surf(new)
end;
You can hide the axes with
set(gca,'Visible','off')
And if you want 20 subplots, try the following:
for i = 1:10
z=z(:,:,i);
subplot(10,2,2*i-1)
surf(z)
%code, obtain new array...
subplot(10,2,2*i)
surf(new)
end
When you use figure(i), you're referring to Figure i which will be created if it does not exist. And with subplot you can specify the ordering of the subplots with the first two arguments.
Note:
20 subplots on one figure are not going to be pretty --- you probably won't be able to see anything, so you should probably break it up into several figures.