Stop automatic scaling of axes - matlab

I have this code:
function masterPiece()
figure
axis manual
axis([-10 10 -10 10])
plot(3,3,'Marker','o','MarkerFaceColor','red');
This produces the follwing image. How can I set the axis from -10 to 10?

Move the line axis([-10 10 -10 10]) after the plot line.
Or: include hold on before the plot. That will freeze the axis properties.

figure
axis manual
axis([-10 10 -10 10])
hold on
plot(3,3,'Marker','o','MarkerFaceColor','red');
otherwise a new plot is created

Related

Equally spaced x-value for values that are not equally spaced

I'm trying to display a discrete plot with values on the x-axis that are not equally space but I want them to appear equally spaced. I would like a stem plot with the first stick not on the y-axis, and I'd also like to have an horizontal dashed line at y=1.
So far here is what I tried.
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
xv = [1 2 3 4];
stem(xv,y);
set(gca,'XTickLabel',x);
Unfortunately, this is not what I expected. The value on the x-axis are not right and the sticks start on the y-axis and end on the figure edge.
How can I fix this?
EDIT: I initially forgot the horizontal dashed line. Added this.
You just need two tiny additions:
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
xv = [1 2 3 4];
stem(xv, y);
xlim([min(xv)-1 max(xv)+1]); % <--
set(gca, 'xtick', xv); % <--
set(gca, 'xticklabel', x);
You (also) need to explicitly set the xtick option, so that only these ticks are drawn, and no other.
With xlim, you can manipulate the x-axis limits. (Left and right limit might be modified to your needs.)
To add the horizontal dashed line, just add the following at the end:
hold on;
plot([min(xv)-1 max(xv)+1], [1 1], 'k--');
hold off;
(Start and end points of the line might be modified to your needs.)
From Matlab R2018b on, you could also use yline.
The output then looks like this:
When you have a sequence of values that you want to plot equally spaced without any special treatment to what each value actually is, you're essentially defining a set of categories.
MATLAB is good at handling these nicely without any extra trickery to lay them out uniformly on your axes if you declare the values explicitly as categorical.
All you need, therefore, is:
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
stem(categorical(x),y);
yline(1,'--');
ylim([0 1.5]) % Make some space on the y-axis so the horizontal line doesn't sit on the top edge

Cannot plot circle with ezplot in matlab

I want to plot 6 circles in figure MATLAB. But it's cannot appear.
I have think of this code is correct, and I try to give axis limits. But it cannot fixing my problem.
clear all;
clc;
p=[8 9 3 4 7 4];
rtopi=[3 4 16 25 34 25];
n=length(p);
for ii=1:n
f=#(x,y)(x-p(ii)).^2+(y).^2-rtopi(ii)^2;
gambar=ezplot(f);
set(gambar,'color','k','linewidth',2);
grid on;
axis equal;
set(gca,'Color','y');
xlabel('Real');
ylabel('Imaginary');
title('Discs');
axis([-30 30 -30 30]);
end
This is the result :
How to fix it?
Remove the .* in the function definition, just use x^2 not x.^2.
Use 'hold on' before end of loop
Move the background color, grid on, title etc. outside the loop.
And most importantly, declare xmin, xmax for the ezplot. Default is (-2pi to 2pi).
Try: gambar = ezplot(f,[xmin,xmax}) and use the plot limits for the xmin and xmax

Hide Chart axes but show labels in Matlab

I want to hide the complete axes form a figure in Matlab.
However, I do want to show the xlabel.
Here's a workaround which worked at Matlab 2015a:
colormap(gray);
imagesc(-prog(:,:,fig));
xlabel(sprintf('c = %.2f',C(:,:,loop(fig))),'color','k')
axis equal; axis tight;
set(gca,'XTick',[],'YTick',[],'XTicklabel',[],...
'YTicklabel',[],'xcolor','w','ycolor','w')
However, since 2015b and 2016a this doesn't work anymore, the xlabels are not shown in white (instead of black)
You need to change the xlabel color after you change the XColor of the axes otherwise set(gca, 'XColor', 'w') forces everything to be white regardless of what you set it to previously.
figure(1)
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(C)
% Change axes colors and appearance
axis equal; axis tight;
set(gca,'XTick',[],'YTick',[],'XTicklabel',[],...
'YTicklabel',[],'xcolor','w','ycolor','w')
% NOW create your black xlabel
xlabel(sprintf('c = test'),'color','k')

Matlab how to add values in the x-axis of a plot

Plot using `set(gca, 'XTick', [1 10 20 50 100])
Hi everyone!
I have created a graph with the function scatter and in the x-axis there are only three values shown: [1 10 100].
I'd like to add some values, specifically I'd like to show [1 5 10 20 50 100].
How can i do this?
My code is:
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'XTick',[1 10 20 50 100])
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
You want to set your XTick values before you set your XTickLabels since you are constructing your XTickLabels from the values of the XTicks themselves.
What is currently happening is that you have 5 XTick values and only 3 labels. Because of this, MATLAB will repeat the labels that you have to populate labels for all XTick locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTick',[1 10 20 50 100])
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
Better yet, there is no real reason for you to be setting XTickLabel manually here. If you change the XTick locations, the labels will automatically be updated to reflect the new locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca, 'XScale', 'log', ...
'XTick', [1 10 20 50 100], ...
'YScale', 'log')

Plotting two Matlab figures next to each other

I'm trying to plot the two figures next to each other. The idea is to create square plots with 1 to 10 on the x-axis. I'm aware of the subplot function but some how it's not working for me. Help is greatly appreciated.
% plotting the data
data1_plot=data1.*100;
data2_plot=data2.*100;
figure(1),
subplot(1,2,1);
plot((1:10)',mean(data1_plot),'o-','LineWidth',1);hold on
for ii=2:10;
if mean(data1_plot(:,ii))<mean(data1_plot(:,ii-1));
plot([ii-1,ii],mean(data1_plot(:,ii-1:ii)),'r.-');
end
end
set(gca,'XTickLabel',{'Unprofitable';'2';'3';'4';'5';'6';'7';'8';'9';'Profitable'});
axis([0.8,10.2,0.0,1.5]),...
ylabel('Average return'),...
title('\rm Equally-weighted PMU portfolio returns, 1990-2015');hold off;
subplot(1,2,2);
plot((1:10)',mean(data2_plot),'o-','LineWidth',1);hold on
for ii=2:10;
if mean(data2_plot(:,ii))<mean(data2_plot(:,ii-1));
plot([ii-1,ii],mean(data2_plot(:,ii-1:ii)),'r.-');
end
end
set(gca,'XTickLabel',{'Weak';'2';'3';'4';'5';'6';'7';'8';'9';'Robust'});
axis([0.8,10.2,0.0,1.5]),...
ylabel('Average return'),...
title('\rm Equally-weighted RMW returns, 1990-2015');hold off;
plots
You can use
axis square
xlim([1 10])
The first command makes the current axes region square (web) and the second set the x-axis limit.
Example:
subplot(1,2,1)
plot(1:10);
axis square
xlim([0 12]);
subplot(1,2,2)
plot(1:10);
axis square
xlim([1 10]);