Multiple plots on a logarithmic scale - matlab

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);

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.

How to plot unevenly spaced data on Matlab / Origin?

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');

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:

How to add new plots of different scale to an existing histogram?

I have a Matlab figure with two histograms on it
,
created with hist() function. Now I want to add two plots in the same figure (bell distribution actually:
,
but they have different scale. I thought I could use plotyy, but I already have my first plot-scale on the figure. How can I add the second plot-scale?
Generally, this is one way to do it:
%// example data
rng(0,'twister')
data = randn(1000,3);
x = linspace(-4,4,100);
y = 16 - x.^2;
%// generate two axes at same position
ax1 = axes;
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
%// move second axis to the right, remove x-ticks and labels
set(ax2,'YAxisLocation','right')
set(ax2,'XTick',[])
%// plot hist and line plot
hist(ax1,data); hold on
plot(ax2,x,y)
ylabel(ax1,'label of hist')
ylabel(ax2,'label of plot')
xlabel(ax1,'Hello World!')

Matlab: How to smooth colors of matrix in plot3( ) function?

I am using plot3 to plot a matrix such that the resultant figure shows different colors for each vector of that matrix:
plot3(Grid.x1(:,:),Grid.x2(:,:),phi(:,:))
How can I smooth the coloring of this plot? Thanks!
You can use varycolor from FileExchange to construct and control a continuous range color spectrum. This way the transition between different lines will seem more natural and proximal lines will have similar colors.
You can read more and see examples usage here http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/
Example:
[X,Y,Z] = peaks(128);
% raw plot3()
figure; plot3(X, Y, Z);
% define set equal to line number
ColorSet = varycolor(128);
% smooth plot3()
figure; hold all;
set(gca, 'ColorOrder', ColorSet);
plot3(X, Y, Z); view(3);
Update:
For a continuous 3D plot (i.e. a surface) you can use surfl instead of plot3 and display your data as a 3-D Surface Plot (with Shading). You can additionally apply any colormap on the resulting surface, i.e. gray or ColorSet as above.
surfl(X,Y,Z);
shading interp;
colormap(gray);