Rescaling Y axis in a plot - matlab

I am trying to adjust the y axis and change it to be from [0 2.5] and to show that it has to be multiplied by a factor of 1000.
Obviously setting the limit with ylim=([0 25]) doesn't work and I can't find a way to do it.
Using to plot:
AveTime = 1.0e+03 * [0.0020, 0.0291, 0.1279, 0.3061, 2.0599];
STDtime = [0.0519, 0.0117, 0.0166, 0.0071, 0.0165];
errorbar([10,25,50,75,100], AveTime, STDtime);

I believe this is what you need, it should work for Matlab versions >= 2014b:
ax = gca;
ax.YAxis.Exponent = 3;
Here's a code example:
clear;
close all;
clc;
x=1:10:1000;
y=3*x;
plot(x,y);
ax = gca;
ax.YAxis.Exponent = 3;
And the plot:

Here is a workaround:
Get the YTick𝘴, divide them by 1000 and set them as YTickLabel𝘴.
set(gca,'YTickLabel',get(gca,'YTick')/1000);
In MATLAB R2014b or later, you can also use:
ax=gca;
ax.YTickLabel= ax.YTick/1000;
On the downside, as Hoki mentioned in the comments,
This is good but only for the final rendering of the figure (if you just want to look/print it). Once you override the YTickLabel, their mode change from auto to manual and any zoom/pan or modification of the axis limit will keep the existing tick labels which may be obsolete after the figure modification.

Related

How to zoom in or magnify programmatically in MATLAB plot graph?

I'm working on a script, where I need to show graph of Fixed Point Iteration and want to zoom in where lines are plotting. Graph is done, it is plotting lines on my graph, problem is that I want to see the values which are produced from function from f(x) and g(x). So I "zoom in" from the figure tools and see which values are produced in the graph right now I am doing it manually in the figure, is there any way to automatically zoom in by just giving the x, y axis? so it means when lines are plotting figure will be automatically zoomed like an animation.
clc;
clear all;
clf;
format short g
syms x;
f = #(x) x-cos(x);
g = #(x) cos(x);
dg = matlabFunction(diff(g(x),x));
figure(1)
z = -3:.001:3;
plot(z,z,'-k',z,g(z),'-k',z,0*z,'-r',0*z,g(z),'-r')
hold on;
x = 1.0;
tol =1.0e-15;
px = x;
x = g(x);
line([px,px],[px,x],'color','blue');
line([px,x,],[x,x],'color','blue');
i = 1;
while(abs(px-x)>tol)
px = x;
x = g(x);
line([px,px],[px,x],'color','blue');
line([px,x,],[x,x],'color','blue');
i = i+1;
data = [i x g(x) f(x)]
drawnow
end
This is what I want in my graph lines.
Link
I tested "zoom" function but it is not helping as per my requirement. Also I tried this one but I can't understand there code.
xlim will allow you to set the range of x-values that are shown. Link: xlim
ylim will allow you to set the range of y-values that are shown. Link: ylim
Together, these should allow you to 'zoom' to different parts of your plot. For example, after your first plot command you could include:
plot(z,z,'-k',z,g(z),'-k',z,0*z,'-r',0*z,g(z),'-r') % Already in your code
xlim([0 1.5]);
ylim([0 1.5]);
You could also include these commands in your for loop so that it gradually zooms in.

How to resize the axes of an graph on Matlab?

I need ideas to resize my axes to have a much more airy graph to better visualize and calculate the gain between the different curves.
I used the code : axis([0 6 1e-3 1e0]) or xlim([0 6]); ylim([1e-3 1e0])
I would like to have for example my curve with: xlim([0:0.2:6]) (just the idea, otherwise it's wrong on matlab).
Thank you!
If I understand what you want, you need more XTicks in the x limits mentioned. After you plot just:
set(gca,'XTick',0:0.2:6)
another way is to write:
h=plot(.... whatever you plot...
h.XTick=0:0.2:6
Logarithmic Plot:
To create the axes the function xticks() and yticks() can be used to set the intervals, start and endpoints. xticks() and yticks() essentially take vectors that define all the ticks on the scales/axes. Just in case you'd like to also edit the interval along the y-axis. This vector can be created by raising each element in the vector (-3,1:0) to be an exponent with a base of 10. Finally, setting gca (the current axis) to logarithmic will allow the vertical spacing between the ticks to be evenly distributed.
axis([0 6 1e-3 1e0]);
Start = 0; Interval = 0.2; End = 6;
X_Vector = (Start: Interval: End);
xticks(X_Vector);
Y_Vector = 10.^(-3: 1: 0);
yticks(Y_Vector);
set(gca, 'YScale', 'log');
title("Logarithmic Plot");
grid;
Ran using MATLAB R2019b

Stretch a polarplot() slice

I'm plotting some polar coordinates in Matlab. See example below:
I'm only plotting a slice but can't figure out how to stretch/ expand this slice, as there is more space in the figure window than used. Speaking in pictures I'd like to 'open it like a fan'.
Minimal working example:
th = linspace(-pi/2,pi/2,100);
polarplot(th,1.1+zeros(size(th)),'g');
ax = gca;
thetalim([-5 5])
rlim([1.05 1.12])
I've tried using ax.Position = [0 0 1 1]; similar to how it works in Cartesian plots but this does not seem to work here.
Any commands or tips I'm missing so far?
I can only think of a workaround solution: increase the theta values passed to polarplot to get larger angles in the graph, and then relabel the theta axis:
stretch = 3; % stretch factor
th = linspace(-pi/2,pi/2,100);
polarplot(th*stretch,1.1+zeros(size(th)),'g'); % apply strech to theta
ax = gca;
thetalim([-5 5]*stretch) % take stretch into account when setting theta axis limit
rlim([1.05 1.12])
ax = gca;
ax.ThetaTickLabel = strsplit(num2str((ax.ThetaTick/stretch))); % correct theta labels
In the last line I am keeping the default theta ticks and relabelling them. You may prefer to specify other values manually, such as
ax.ThetaTick = stretch*(-5:5); % specify theta ticks, including stretch
ax.ThetaTickLabel = strsplit(num2str((ax.ThetaTick/stretch))); % correct theta labels

How to align colorbar tick labels and lines in Matlab

I need to make a plot with a discrete colorbar in Matlab. I do this in the following way:
data = randi(10, 20);
imagesc(data)
my_colormap = rand(10, 3);
colormap(my_colormap)
cb = colorbar
set(cb,'YTickLabel',{'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';})
Now my problem is that the colorbar tick labels and the small lines in the colorbar don't align nicely. How can I even the colorbar tick labels and the small lines better as illustrated in the following pic:
The TickLabel on the colorbar each correspond to a value (a Tick). To place the TickLabels in the middle, you need to place the tick in the middle. To make this dynamic (so that It does not change when resizing the image) was I bit tricky I recall and I do not really recall. To set the ticks just once is not so hard though,
set(hCbar,'YTicks',RightYTicks);
EDIT:
On request I will post an example. This should give a hint of what to do.
x = 1:10;
y = 1:10;
cmap = jet(10);
[x, y] = meshgrid(x,y); %x and y grid
c = x-0.1; %Set color code to increase to the right
hFig = figure;
scatter(x(:),y(:),10,c(:),'filled'); % Simpler for the example
set(gca(hFig),'CLim',[0,10]);
colormap(cmap);
hCbar = colorbar;
set(hCbar,'YTicks',0.5:9.5);
set(hCbar,'YTickLabels',{'A','B','C','D','E','F','G','H','I','J'});
For newer matlab version, the YTicks may have changed name to Ticks And YTickLabels may be called TickLabels.

Matlab 2014b Tick Label Between Tick Marks

I'm creating a bar plot in Matlab 2014b and would like to center the x-axis labels in between the tick marks. For example, in the following graph, the bars are correctly segmented by year using datetick and a slight adjustment I make. However, I would like the labels to appear half way in between the tick marks that are currently specified.
clear; close all;
a = rand(12, 1)-0.1;
x = linspace(datenum('03-31-2012'), datenum('12-31-2014'), 12);
b1 = bar(x, a);
datetick('x', 'yyyy');
display(datestr(x))
ax1 = gca;
bGapWidth = (x(2)-x(1));
bWidth = b1.BarWidth;
ax1.XLim = [x(1)-bGapWidth*bWidth x(end)+bGapWidth*bWidth];
initTick = ax1.XTick;
ax1.XTick = initTick + (bWidth*bGapWidth)/2 + (bGapWidth*(1-bWidth)/2);
I've seen a few similar questions but nothing quite the same. I've also seen suggestions of creating a dummy axis (one for labels, one for tick marks), but I've had some trouble here too - simply setting a new variable equal to the current axes object and making modifications changes the whole plot. Feel free to help me out on this part, or suggest a better solution in general.
clear; close all;
a = rand(12, 1)-0.1;
x = linspace(datenum('03-31-2012'), datenum('12-31-2014'), 12);
b1 = bar(x, a);
datetick('x', 'yyyy');
display(datestr(x))
h1=get(gca,'XTick');
h2=get(gca,'XTickLabel');
h3=length(h1);
xdiff=h1(2)-h1(1); % assuming uniform step interval in x-axis
h1=h1+0.4*xdiff; % this factor of 0.4 can be adjusted
ylim([0 1]);
for i=1:h3-1
text(h1(i),-0.05,num2str(h2(i,:)));
end
% instead of -0.05 relative to y put the labels where you like
set(gca,'XTickLabel',{});