How to plot unevenly spaced data on Matlab / Origin? - matlab

I want to plot my following data:
x-axis: [0,10,50,100,500,1000,1500]
y-axis: [75.6,78,78.2,81.8,84.7,85.2,86.3]
As seen above, the data on the x-axis are unevenly spaced. When I plot the above data linearly using origin, I get:
I got the similar graph on Matlab too. Notice that most of the Amp data lie for x<500. I want to plot the graph such that the whole output (y-axis) become clearly visible. For this, I tried using the Logarithmic plot. I changed the x-axis into logarithmic in Matlab as follows:
set(gca, 'XScale','log');
In Origin, we can use GUI to change the x-axis to logarithmic. The obtained graphs are as follows:
The obtained graphs are still not good. Any ideas, please!
Thank you very much.

https://www.mathworks.com/help/matlab/ref/semilogx.html
x = [0,10,50,100,500,1000,1500];
y = [75.6,78,78.2,81.8,84.7,85.2,86.3];
semilogx(x,y,'.-', 'markersize', 15);
set(gca,'XTick',x);
set(gca,'XTickLabelRotation',45);

x = [0,10,50,100,500,1000,1500];
y = [75.6,78,78.2,81.8,84.7,85.2,86.3];
y2 = [80,84,85,86,89,90,92];
semilogx(x+1,y,'.-', 'markersize', 15);
set(gca,'XTick',x);
set(gca,'XTickLabelRotation',45);
hold on;
semilogx(x+1,y2,'.-', 'markersize', 15);
hold off;
grid on;
legend('y1','y');

Related

3D graphs of probability density functions in MATLAB

I want to create the 3D plot of the probability density functions of my variable. I have a matrix with dimensions 189x10000, where rows correspond to the time and columns are results of the simulation. Can somebody help me to create a density plot over time? I want my plot to look like this:
A = [1:185]'; % substitute for date vector
K = linspace( -20, 20, 100);
f = zeros(185,100);
xi = zeros(185,100);
r = normrnd(0,1,[185,10000]);
for i=1:185
[f(i,:),xi(i,:)] = ksdensity(r(I,:));
end
a = figure;
meshc(A, K', f')
datetick('x', 'yyyy')
view(85, 50)
set(gca, 'YLim', [-15, 10])
set(gca, 'XLim', [A(1), A(end)])
xlabel('Time')
With this code I get this:
Replace random numbers with density distribution.
If you want a finer grid, then use more points. Your actual data has 10-times as much, right? Otherwise this is as good as it gets; "improving" your plot, e.g. smooth over your data, is more data-doctoring than science.
Solution provided by Adriaan.

Matlab: `mesh()` plot with less number of grid

Let's say data is a matrix of size 129 * 129.
by using
mesh(data, 'FaceColor', 'none', 'EdgeColor', 'black')
we get something like
We can find that the grid are quite intense. I would like to have the same figure but with less number of mesh lines, something like
It is of course doable to plot a smaller data, for example data(1:10:end, 1:10:end). But in this way, the plot are not accurate as before anymore.
Another example is plot(..., 'MarkerIndices', ...). This can give you a plot with less number of markers without modifying the plot. https://www.mathworks.com/help/matlab/creating_plots/create-line-plot-with-markers.html
An alternative approach is to use plot3 to plot the mesh lines manually. That way you can plot each line smoothly using all the data points, but not have as many lines.
[X,Y,Z] = peaks(201);
step = 5;
plot3(X(:,1:step:end),Y(:,1:step:end),Z(:,1:step:end),'k')
hold on
plot3(X(1:step:end,:).',Y(1:step:end,:).',Z(1:step:end,:).','k')
hold off
I think your best option would be to create a surf plot with no grid lines (showing a colored surface with the full resolution of your data), then overlay a down-sampled mesh plot. Something like this:
surf(data, 'EdgeColor', 'none');
hold on;
mesh(data(1:10:end, 1:10:end), 'EdgeColor', 'black');
You could also add some transparency to the surf plot to make the mesh visible through it:
surf(data, 'FaceAlpha', 0.7, 'EdgeColor', 'none');
The answer from #David is good. In addition to his approach, we can also replace plot3 with many infinitesimal mesh. The idea is to plot mesh for single vectors many times.
[X,Y,Z] = peaks(201);
tempz = NaN(201, 201);
tempz(1, :) = Z(1, :);
mesh(X, Y, tempz, 'EdgeColor', 'interp');
hold on
% plot x lines
for i = 2:10:201
tempz = NaN(201, 201);
tempz(i, :) = Z(i, :);
mesh(X, Y, tempz, 'EdgeColor', 'interp');
end
% plot y lines
for i = 2:10:201
tempz = NaN(201, 201);
tempz(:, i) = Z(:, i);
mesh(X, Y, tempz, 'EdgeColor', 'interp');
end
The original is
By using the snippet above, it gives
The benefits of this over #David's answer is that you can preserve all of the fancy properties of mesh, for example shading interp etc.

Multiple plots on a logarithmic scale

I'm trying to plot two lines (data and linear fit) in a single graph with logarithmic scale. My code:
Iots = I_An./Temp.^2; % I Over T Squared
Oot = 1./Temp; % One Over T
[p,~] = polyfit(Oot,Iots,1);
linfit = polyval(p,Oot);
figure('color','w','units','normalized','outerposition',[0 0 1 1]);
hold on
loglog(Oot,Iots,'.','LineWidth',2);
loglog(Oot,linfit,':r','LineWidth',2);
The result is not a logarithmic scale graph:
If I run just one of the plot lines, it works on its own. What should I do? Are there any contradicting commands?
You want to call hold on after creating your first loglog plot. Also, you only need to use loglog on the first plot to create the logarithmic axes. After than you can just call normal plot and it will use the logarithmic axes.
x = linspace(0, 100);
loglog(x, x, '.', 'LineWidth', 2);
hold on
plot(x, x.^2, '.r', 'LineWidth',2);

Two y-axes plot for multiple data set in Matlab

I am trying to create two y-axes plot in Matlab.
I have two groups of data, with each group having three plots of similar type. When I am trying to plot it, the scaling on right hand side y-axis get messed up. I would appreciate some help.
X = [50, 100, 200, 400];
YSKL_Temporal_WOFAE = [3.2000 2.3354 1.9428 1.7658];
YSKL_Spatial_WOFAE = [0.9225 0.9724 1.0986 1.1770];
YSKL_Spatial_WithFAE = [0.2635 0.1653 0.1513 0.1618];
YMSRE_Temporal_WOFAE = [0.3559 0.3027 0.2733 0.2636];
YMSRE_Spatial_WOFAE = [.3151 .2689 .2551 0.2524];
YMSRE_Spatial_WithFAE = [.0933 .0648 0.0663 0.0640];
figure(1);
[AX, p1, p2] = plotyy(X, YSKL_Temporal_WOFAE, X, YMSRE_Temporal_WOFAE);
set(AX,'XTick', X); % This fixes X-axis tick mark (same as data axis)
set(get(AX(1),'Ylabel'),'String','SKL Score')
set(get(AX(2),'Ylabel'),'String','Norm. Residuals')
xlabel('Time (\musec)')
title('SKL and Norm. Residual plotted on different y-axes')
set(p1,'LineStyle','--')
set(p2,'LineStyle',':')
axes(AX(1))
hold on
plot(X, YSKL_Spatial_WOFAE);
hold on
plot(X, YSKL_Spatial_WithFAE);
ylim([0 4])
hold off
axes(AX(2))
hold on
plot(X, YMSRE_Spatial_WOFAE);
hold on
plot(X, YMSRE_Spatial_WithFAE);
ylim([0.0 0.4])
hold off
Plot looks like this:
Please notice the scale on right y-axes
Regards,
Dushyant
The second (right hand side) y-axis tick labels are "frozen" after the call to plotyy. When adjusting the ylim on AX(2) this has no effect on the labels.
Therefore, the axes appearance has to be reset to auto.
Using the following code with the example from the question:
axes(AX(2))
hold on
plot(X, YMSRE_Spatial_WOFAE);
hold on
plot(X, YMSRE_Spatial_WithFAE);
ylim([0.0 0.4])
% include the following statement to allow
% the second y-axis to reset the ticks:
set(AX(2), 'YTickMode', 'auto', 'YTickLabelMode', 'auto')
hold off
Will yield this plot:

2-d graph with two different axis showing different min and max MATLAB

I have the graph below I need to create two different x-axis. The unique part of this problem is where the min and max values need to be located. The range for is 0-100 for both, however the 100% value on the second x-axis needs to be where the 50% value is on the first. See the picture for clarification. The red is what I need to add using MATLAB.
I did a lot of looking and while it's very simple to put two different axis on one graph, I couldn't find a solution for this particular problem. I'd like this to be done in the code and not plot tools.
How about this
% dummy data
y = 1:80;
x1 = 100*sin( 4*pi*y/80 ).^2 ;
x2 = 100*cos( 5*pi*y/80).^2;
Plot the first line
figure;
line( x1, y, 'Color', 'b', 'LineWidth', 2 );
Get position and size of first plot
haxes1 = gca;
haxes1_pos = get(haxes1,'Position');
set the 100% of second plot to 50% of first ("tweaking" the width of the axis)
haxes1_pos(3) = haxes1_pos(3)/2;
haxes2 = axes('Position',haxes1_pos,'XAxisLocation','top','Color','none','XColor','r');
Plot the second line
line( x2, y, 'Color', 'k', 'LineWidth',2,'Parent',haxes2);
And this is what you get