Cannot plot circle with ezplot in matlab - 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

Related

slope of lsline changes according to xaxis

I want to fit a lsline (h=lsline) to a scatterplot in MATLAB. The data is in the xrange between [-2.5 2.5] and I display them with and xlim of [-3 3]. I want to show the lsline only in the range between [-2.9 2.9].
However, when I add h.XData=[-2.9 2.9] the slope of the line changes. Does that make sense to anyone? There is no datapoint between 2.5 and 3 and I expected to only see differences in the length of the line?
Simply change the axis limits after plotting the line:
x = -2.5:.125:2.5;
y = .5 .* x + randn(1,numel(x))*.2;
scatter(x,y);
xlim([-2.9 2.9])
lsline;
xlim([-3 3])
ylim([-1.6 2])

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

How to plot a multidimensional array in matlab?

I have a table as follows
system:index 2017_06_18 2017_06_19 2017_06_20 2017_06_21
2 612.8099664 1174.656713 1282.083251 815.3828357
3 766.4103726 1345.135952 1322.726083 749.998993
4 765.0230453 1411.669136 1350.437586 610.9541838
5 553.5858458 1374.14789 1152.086957 566.7924468
6 466.9780908 1311.903756 1060.494001 559.1982264
7 257.1162602 1270.182385 988.5455285 562.9224932
8 230.6611542 1310.971988 1001.548768 502.3266959
I want to plot a 2d-colormap representing system:index as y axis, dates as x axis and values under dates as colors. I tried with the following code but it did not give what I want.
clear
clc
filename = 'TurbidityDailyMean.xlsx';
data = xlsread(filename,'TurbidityDailyMean','A1:E8');
figure;
hold on
for i = 2:5
y = data(:,1);
x = data(:,i);
plot(x,y)
end
I need to map a colormap as mentioned above. But from what I tried it gives something else. And another fact is that I can't insert system:index and dates row into matlab with relevant data.
Thanks to my supervisor Dr.Kavinda,enter image description here the solution I adopted from him was as follows
data1 = csvread('TurbidityDailyMean.csv',1,1);
[mm,nn]=size(data1)
xx=ones(mm,nn);
yy=ones(mm,nn);
for j=1:nn
xx(:,j)=j;
end
for i=1:mm
yy(i,:)=i;
end
zz=data1;
h1=pcolor(xx,yy,zz);
shading flat
set(gcf,'color',[1,1,1])
axis([1 5 1 7]);
jet2=jet;
jet2(1,:)=1.0;
colormap(jet2)
caxis([0.0 0.13])
hold on
xlabel('Julian Day');
ylabel('y (?~1 km)');
colorb=colorbar;
set(get(colorb,'ylabel'),'String','Normalized Red Band Reflectance','fontsize',15,
'color', 'k');
set(get(colorb,'xlabel'),'fontsize',20, 'color', 'k');
grid on

Movement of rectangle with tot function

How can I make this rectangle move from the top left down to the bottom right?
clear all
close all
clc
n=10;
h=1;
for t=0:n-1
clf;
axis([0 sum(1:n) 0 sum(1:n)]);
axis manual
tot = sum(0:t);
patch([tot tot+(t+1)*h tot+(t+1)*h tot],[tot tot tot+(t+1)*h tot+(t+1)*h],...
[2 6 3 7],'EdgeColor','none','FaceAlpha',(1-(t+1)*.7/n));
pause(1/6)
end
Also, try to use this function somewhere.
sum(1:t)-tot-(t+1)*h
You could compute the Y location for patch function differently, but from your current code, the simplest change will be to reverse the Y axis direction:
axis ij; % put this before patch
You just need to substruct your y coordinates from the total height sum(0:n):
patch([tot tot+(t+1)*h tot+(t+1)*h tot],...
sum(0:n)-[tot tot tot+(t+1)*h tot+(t+1)*h],...
[2 6 3 7],'EdgeColor','none','FaceAlpha',(1-(t+1)*.7/n));

Stop automatic scaling of axes

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