Adding more points on axis of Matlab plot - matlab

I used the semiologx command to generate these two plots, but I need the plot to show more reference values on the X axis.
How can this be accomplished in MATLAB?

It seems your plot was generated by semilogx, not semilogy. Anyway you can set the values on the x axis (called "ticks") using (for example):
set(gca, 'xtick', [1 10 100 1000])
The 1st argument is the axis handle you wish to change, in this example I just use gca which stands for "get current axes", and returns the currently active axes.

Related

How to break the axis in matlab

How could one break the x axis in the same figure (not the subplot function)?
You can see an example in line graph in panel b in the following picture, one single row data have been split into three parts as well as the x axis.
the question is how this plot can be achieved in matlab.
(like the panel b in this figure)
Ill leave you an exmaple here using surf.
The trick is inserting nans wherever you want the lines to appear "empty"
z=peaks(100);
% If you want to delete a certain amount of rows/cols
z2=z;
z2(:,10:15)=NaN;
z2(:,50:55)=NaN;
z2(:,75:80)=NaN;
% If you want to separate you data without deleting anything
z3=z;
z3=[z3(:,1:15) nan(size(z3,1),5) z3(:,16:75) nan(size(z3,1),5) z3(:,75:100) ];
% This last bit is only for plotting, so you can try it in your computer
subplot(131)
title('Original')
surf(z,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(132)
title('Deleted columns')
surf(z2,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(133)
title('Separatedd data')
surf(z3,'edgecolor','interp')
axis off
view(2)
axis equal
There are a couple of utilities on the File Exchange that allow you to do that:
Break X Axis
BreakXAxis

How to Adjust y axis plot range in Matlab?

I need to plot the following functions in matlab
y1=sign(x)
y2=tanh(x)
y3=(x)/(x+1)
The x-range is -5,5 with 0.1 spacing
The y-plot range should be between -1.5 to 1.5.
Each plot should have a labeled x and y axis and a legend in the lower right corner.
The only things I cant figure out is how to adjust the y plot range. Ive tried editing the actual figure but all that seems to do is distort the graph.
Is there a command within matlab that will let me adjust the y axis plot range?
The other thing I havent figured out yet is adding a legend, I can do it after the figure is created but I guess it needs to be done by matlab command.
Yes, use axis after the plot command:
axis([-5 5 -1.5 1.5])
If you only want to set the y-range without setting the x-range you can use
ylim([-1.5 1.5])
or alternatively axis([-inf inf -1.5 1.5]). I found this from the original MATLAB-source: https://de.mathworks.com/help/matlab/ref/ylim.html
PS: For trigonometric functions I would recommend to use axis equal to have equally spaced x and y-axis (see MATLAB)

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...

Matlab - Continuous Plot and Semilogx on the same figure

I am trying to plot on the same figure the evolution of a function f, with argument x in ]0,1]. I would like to see both the evolution of f far away from 0 and close to 0, on the same figure, with one x axis.
For now I only have two different figures, one using plot with x=[0.1 ... 1], and one using semilogx with x=[1e-9 1e-7 1e-5... 0.1]. I would like to have both graphs on the same figure, with the x axis being logarithmic at the beginning, then linear after a certain x0 (let's say x0=0.1).
I do not want something using plotxx since I want only one x axis.
Do you know if this is possible?
Thank you for your time and help.
Just plot your y vector without specifying the x vector, this will get you a uniformly spaced plot, then use XTick and XTickLabel to make it work. For example,
x1=logspace(-10,-1,10);
x2=linspace(1,10,10);
y1=x1.^0.25;
y2=1./x2;
plot([y1 y2],'-x')
xlabels=num2cell([x1 x2]);
set(gca,'XTick',1:numel(x1)+numel(x2),'XTickLabel',xlabels)
If you want to use Latex to format tick labels, you'll need to download a function from the Matlab File Exchange.

MATLAB - How to zoom subplots together?

I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)