Here is my code:
xslice = [bestcoefs(1), cc1(no1)];
yslice = [bestcoefs(2), cc2(no2)];
zslice = [cc3(1), bestcoefs(3)];
slice(V, xslice, yslice, zslice, 'linear');
cb = colorbar;
xlabel('c1'); ylabel('c2'); zlabel({'likelihood of (c1,c2,c3)','c3'});
view(3);
V is a matrix of probabilities 6x13x9 and bestcoefs(1), cc1(no1), etc. are points where I want to slice the plot. However, I get this result:
Why does it come out like this? I want it to look like the first one here.
When I run:
% some data:
V = randn(6,13,9);
bestcoefs = randi(6,3,1);
cc = randi(6,3,1);
% your code with slight modifications:
xslice = [bestcoefs(1), cc(1)];
yslice = [bestcoefs(2), cc(2)];
zslice = [cc(1), bestcoefs(3)];
slice(V, xslice, yslice, zslice, 'linear');
cb = colorbar;
xlabel('c1'); ylabel('c2');...
zlabel({'likelihood of (c1,c2,c3)','c3'});
view(3);
I get something like this:
Which looks fine to me. Try to see if your bestcoefs, cc's and no's are defined correctly.
Related
I would like to create a plot with discending data, but I have some problem (see image).I would like that the green line followes yaxes on the right. Why I have this result?
Below there is my script
%grafico con doppio asse y
workdays = dataset.regione;
workhours = dataset.("somma_classe1");
y1 = dataset.("conteggio_classe1");
[~,arr] = sort(y1,'descend');
X = reordercats(categorical(workdays),workdays(arr));
figure;
bar(X,workhours,'yellow');
title('title 1+2')
ylabel('count news','FontSize',11)
hold on
plot(y1,'green')
yyaxis right
ylabel('media impact');
I am trying to use Matlab to generate a line graph, but the line terminates at the last point, and doesn't go all the way to the origin. Is there any way to make it so that the line goes beyond the points in code?
I've attached the code that I'm currently using, along with pictures of what the graph looks like right now and how I want it to look.
%Enter Data
fnet = [0.465, 0.560, 0.670, 0.763, 0.870, 0.971, 1.063];
faccel = [0.434, 0.514, 0.612, 0.684, 0.776, 0.850, 0.915];
asys = [0.4963, 0.6034, 0.7074, 0.8088, 0.9210, 1.030, 1.138]
mh = [0.050, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11]
x = fnet;
y = asys;
%Model Equation
model = 'm*x'
%the model can be changed, ex. m*x^2
IV = 'x'
DV = 'y'
%Create and perform curve fit
newfit = fittype(model, 'Independent', IV, 'Dependent', DV);
%result and goodness of fit, prime symbol converys rows to columns
[result, gof] = fit(x', y', newfit, 'StartPoint', 1)
%plot fits and data points, create plot object for formatting
p = plot(result, x, y);
%style the data points
p(1).MarkerSize = 10;
p(1).Marker = '.';
p(1).MarkerFaceColor = 'blue';
%p(1).MarkerEdgeColor = 'green';
%style the line of best fit
p(2).LineWidth = 1;
p(2).Color = 'black';
%Create graph object, set formatting to latex
graph = gca;
set(graph, 'defaultTextInterpreter', 'latex');
set(legend, 'visible', 'off');
%format title and subtitle
graph.Title.String = {'System Acceleration vs. Net Force on System', 'in Modified Atwood Machine'};
graph.Title.FontSize = 16;
%subtitle, where we will place our equation and statistics
%specifically, the equation w/ units, r squared, slope with plusminus %
graph.Subtitle.Interpreter = 'latex';
graph.Subtitle.String = '$a_{sys} = 1.064 m^{-1}F_{net}, \, r^2=0.9994, m=1.064 \pm 0.007$';
graph.Subtitle.FontSize = 13;
%format x and y axes
graph.XLabel.Interpreter = 'latex';
graph.XLabel.String = '$F_{net} \: (N)$';
graph.XLabel.FontSize = 15;
graph.XLim = [0,1.5];
graph.XGrid = 'on';
graph.XMinorGrid = 'on';
graph.XMinorTick = 'on';
graph.YLabel.Interpreter = 'latex';
graph.YLabel.String = '$a_{sys} \: (\frac{m}{s^2})$';
graph.YLabel.FontSize = 15;
graph.YLim = [0,1.5];
graph.YGrid = 'on';
graph.YMinorGrid = 'on';
graph.YMinorTick = 'on';
Instead of using plot directly on the fit result object, you can call plot with a bit more control and evaluate the result directly for the line
Replace this:
p = plot(result, x, y);
with this:
hold on
p(1) = plot( x, y );
p(2) = plot( [0,2.2], feval(result, [0,2.2]) );
Note you could add the marker and line options during the plot calls now if you wanted, instead of updating p(1) and p(2) in retrospect.
I am trying to plot a graph using a straight line in MATLAB; however, I can only print it using dot circles.
I tried changing "ro-" with "r-" and other different solutions but nothing worked. When using "r-" it does not print anything.
This is my code:
for T = temp
figure(i)
for xb = linspace (0,1,10)
xt = 1-xb;
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot = Pb*xb + Pt*xt;
yb = (Pb*xb)/Ptot;
plot(xb, Ptot, 'bo-.')
hold on
plot(yb, Ptot, 'ro-')
end
i = i + 1;
saveas(gcf, filename, 'png')
end
This is what I get:
This is what I want:
How can I make this plot with lines?
To plot a line, the plot command must get all the points along the line in one function call. Repeated calls to plot will add new lines to the figure, and in this case each line is composed of a single point. In short, MATLAB doesn't know you want to connect these dots.
So how do you get all the data points in one array? Simply store them within your loop:
T = 70;
xb = linspace(0,1,10);
Plot = zeros(size(xb)); % preallocate output array
yb = zeros(size(xb)); % preallocate output array
for ii=1:numel(xb)
xt = 1-xb(ii);
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot(ii) = Pb*xb(ii) + Pt*xt;
yb(ii) = (Pb*xb(ii))/Ptot(ii);
end
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')
But it is actually easier to do this without a loop at all:
T = 70;
xb = linspace(0,1,10);
xt = 1-xb;
Pb = 10^(6.89272 - (1203.531/(T+219.888)));
Pt = 10^(6.95805 - (1346.773/(T+219.693)));
Ptot = Pb*xb + Pt*xt;
yb = (Pb*xb)./Ptot; % note ./ is the element-wise division
figure
plot(xb, Ptot, 'b-')
hold on
plot(yb, Ptot, 'r--')
I have several csv files and I plotted them using plot3 to create the following image:
Now I would like to turn this into a surface plot because I would like to colour the plot according to height. I made the following with scatter3:
clearvars;
files = dir('*.csv');
name = '\epsilon_{y} over time for Vertical section';
des_col_1 = 'Vertical section.epsY []';
des_col_2 = 'Length [mm]';
set(gca,'FontSize',20)
a = gca;
ii = 1;
x_data = [];
y_data = [];
z_data = [];
tStart = tic;
for file = files'
csv = xlsread(file.name);
[n,s,r] = xlsread(file.name);
des_cols = {des_col_1,des_col_2};
colhdrs = s(1,:);
[~,ia] = intersect(colhdrs, des_cols);
colnrs = flipud(ia);
file.name = n(:,colnrs);
file.name = file.name(1:end,:);
x_data = [x_data; file.name(:,2)];
y_data = [y_data; ones(size(file.name(:,2))).*ii];
z_data = [z_data; file.name(:,1)];
ii = ii+1;
end
tEnd = toc(tStart);
fprintf('%d minutes and %f seconds\n',floor(tEnd/60),rem(tEnd,60));
view(40,40);
zlabel({'True strain (%)'});
xlabel({'Length along sample (mm)'});
ylabel({'Stage'});
title({name});
scatter3(a,x_data,y_data,z_data,10,z_data);
colormap(jet); %# or other colormap
which gives me this
That was made with a smaller set of data than the first one as a test. It does almost what I want but I was wondering if there was a way to generate a true 3D surface from all my data. I can create a matrix with x, y, and z values for all points and I tried to replace scatter3(a,x_data,y_data,z_data,10,z_data); with
[X,Y] = meshgrid(x_data,y_data);
f = scatteredInterpolant(x_data,y_data,z_data);
Z = f(X,Y);
surf(a,X,Y,Z);
but the plot that I get does not look very good
I'm pretty sure there's something wrong with the interpolation but I'm not very good with surfaces so I don't know how to correct it.
The reason surf is giving you the error is you are creating long nx1 arrays Where n = number of points per file times number of files. For x,y,& z_data and you need them into a matrix instead. So try the following changes:
for file = files'
<snipped out for length>
x_data = [x_data; file.name(:,2).'];
y_data = [y_data; ones(1,numel(file.name(:,2))).*ii];
z_data = [z_data; file.name(:,1).'];
ii = ii+1;
end
This should make x, y, and z_data the size nxm (n = number of files, m = number points per file).
Then you should be able to just do
surf(x_data,y_data,z_data)
Given a domain (2D grid) and two lines:
D = [-0.99 0.99;-0.99 0.99]; %domain
x = 0.0676 ;
y = 0.7630];
with the lines given as a vector:
pt = [0.0676 0.7630]
what is the most efficient way for me to find the four midpoints in Matlab?
Thank You
If I understand the question right, you're trying to achieve this.
This code does it, you can certainly improve the performance by making better use of vector and matrix operations, but if it's only for a problem this simple the optimization isn't worth it.
dlim_x = [-0.99 0.99];
dlim_y = [-0.99 0.99];
x = 0.0676;
y = 0.7630;
domain_vec_x = [dlim_x(1) dlim_x(1) dlim_x(2) dlim_x(2) dlim_x(1)];
domain_vec_y = [dlim_y(1) dlim_y(2) dlim_y(2) dlim_y(1) dlim_y(1)];
line(domain_vec_x, domain_vec_y);
line([x x], dlim_y, 'linestyle', ':');
line(dlim_x, [y y], 'linestyle', ':');
xlim([1.1*dlim_x]);
ylim([1.1*dlim_y]);
hold on;
x1 = mean([dlim_x(1), x]);
x2 = mean([x, dlim_x(2)]);
y1 = mean([dlim_y(1), y]);
y2 = mean([y, dlim_y(2)]);
plot(x1,y1,'^')
plot(x2,y1,'^')
plot(x1,y2,'^')
plot(x2,y2,'^')