Increase resolution in X-axis for an existing x-y graph in MATLAB - matlab

In MATLAB, I have an existing set of values for X,Y where X = [0,1,2, ... 255], and Y is in the range of -5 to 4.
When I plot this graph MATLAB, obviously interpolates these values.
I require to increase the resolution of the X-axis to X = [0, 0.25, 0.5, 0. 75, 1 .... 254.75, 255].
I am not looking for a simple averaging kind of operation. Rather I want it to be as good as MATLAB which does it very smoothly. Please guide me.

A few things to know:
Matlab doesn't interpolate if you haven't made it interpolate. The figure you see is scalable, so there is no interpolation, at most a line connecting between adjacent dots.
If you want to interpolate just use interp1....
fore example,
Xnew= 0:0.25:255;
Ynew=interp1(X,Y,Xnew,'spline');
plot(Xnew,Ynew);
should do.

Related

MATLAB yaxis customization for 0.8 0.9 0.99 0.999 numbers [duplicate]

Is it possible to make a plot in matlab that does not actually take the logs of the values? I'm plotting wide ranges of values and when I try to make a log plot of them, those below 1 become negative. I would just like it to plot the values on a log scale without taking their logs.
Alternatively, set(gca,'XScale','log') if you have your plot already.
Yes, it is possible. Use the loglog command.
The example from the Mathworks website:
x = logspace(-1,2); % generate a sequence of points equally spaced logarithmically
loglog(x,exp(x),'-s')
grid on
If you do not want both axes to be log scale, use semilogx or semilogy.
So, you want to plot liner data on logarithmic axes? You can exponentiate you values before using the log plot. This way the point p=(10,3) will plot at the x=10 position.

What is missing for a better MATLAB Plot3 visualization? [duplicate]

This question already has answers here:
Produce a 3D stem plot with a custom colormap in MATLAB
(2 answers)
Closed 7 years ago.
I have produced the following figure using MATLAB plot3 function.
This figure is not good.
Because, I think, it's too hard for readers to estimate the coordinates from this figure.
Points height (Z value) is too hard to be estimated from the figure.
What is missing in my figure that makes it hard to be interpreted ?
To Play with the data:
The visualized data is here. The function to produce my current figure is here. Either comment the mArrow3 call, or download it from here.
To better see height you can use stem3 to draw a vertical line from the floor to each point. You can enhance the representation with a semi-transparent patch at zero height to highlight the floor.
% // Random data
x = -20+50*rand(1,50);
y = 150*rand(1,50);
z = -5+10*rand(1,50);
%// With plot
figure
plot3(x,y,z,'.','markersize',8)
grid on
axis equal
view(-33, 14)
%// With stem3 and patch
figure
stem3(x,y,z,'.','markersize',8)
grid on
hold on
patch([-20 30 30 -20], [0 0 150 150], [0 0 0 0], 'k', ...
'edgecolor', [.5 .5 .5], 'FaceAlpha' , .1)
axis equal
view(-33, 14)
I think the problem might be intrinsic to these kind of plots: the 0d dots of your data are hard to interpret perspectivically, your brain can't decypher at which depth the data points are located. For instance, it would seem to me that you have no data poinst above z=0 and above x=15, which is obviously wrong, but my brain attributes most of your points to the z=-5 plane.
Unless your data points have finite volume which proportionally changes with distance (which can not be done with matlab, and probably wouldn't help much anyway), you might want to reconsider your way of visualization. How about having 3 plots, one each with camera along the x, y, and z axes?
EDIT: the suggestion of Luis Mendo makes me think I should probably have a more open mind when trying to answer a question:)
You could also use different colors/markers/point size to discriminate between various regions in your data. For instance values having z below 0 are red and those above are green. Here is a simple example using scatter3 with 4 distinct regions. Thanks to Luis Mendo for the dummy data.
clc;clear;close all
% // Random data...thanks Luis Mendo
x = -20+50*rand(1,50);
y = 150*rand(1,50);
z = -5+10*rand(1,50);
%// Get indices for various regions in your data
region1 = find(z>=-4 & z<-2);
region2 = find(z>=-2 & z<0);
region3 = find(z>=0 & z<2);
region4 = find(z>=2 & z<4);
%// Draw each region with its own color/size
scatter3(x(region1),y(region1),z(region1),20,'r','filled')
hold on
scatter3(x(region2),y(region2),z(region2),40,'y','*')
scatter3(x(region3),y(region3),z(region3),60,'g','filled')
scatter3(x(region4),y(region4),z(region4),80,'b')
grid on
view(-33, 14)
kkuilla's answer about heat map produced a much better result:

Draw evenly-spaced height lines of a function in MATLAB

I would like to draw height lines of a function (represented by matrices, of course), using MATLAB.
I'm familiar with contour, but contour draws lines at even-spaced heights, while I would like to see lines (with height labels), in constant distance from one another when plotted.
This means that if a function grows rapidly in one area, I won't get a plot with dense height lines, but only a few lines, at evenly spaced distances.
I tried to find such an option in the contour help page, but couldn't see anything. Is there a built in function which does it?
There is no built-in function to do this (to my knowledge). You have to realize that in the general case you can't have lines that both represent iso-values and that are spaced with a fixed distance. This is only possible with plots that have special scaling properties, and again, this is not the general case.
This being said, you can imagine to approach your desired plot by using the syntax in which you specify the levels to plots:
...
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v.
...
So all you need is the good vector v of height values. For this we can take the classical Matlab exemple:
[X,Y,Z] = peaks;
contour(X,Y,Z,10);
axis equal
colorbar
and transform it in:
[X,Y,Z] = peaks;
[~, I] = sort(Z(:));
v = Z(I(round(linspace(1, numel(Z),10))));
contour(X,Y,Z,v);
axis equal
colorbar
The result may not be as nice as what you expected, but this is the best I can think of given that what you ask is, again, not possible.
Best,
One thing you could do is, instead of plotting the contours at equally spaces levels (this is what happens when you pass an integer to contour), to plot the contours at fixed percentiles of your data (this requires passing a vector of levels to contour):
Z = peaks(100); % generate some pretty data
nlevel = 30;
subplot(121)
contour(Z, nlevel) % spaced equally between min(Z(:)) and max(Z(:))
title('Contours at fixed height')
subplot(122)
levels = prctile(Z(:), linspace(0, 100, nlevel));
contour(Z, levels); % at given levels
title('Contours at fixed percentiles')
Result:
For the right figure, the lines have somewhat equal spacing for most of the image. Note that the spacing is only approximately equal, and it is impossible to get the equal spacing over the complete image, except in some trivial cases.

MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).

Matlab - Continuous Plot and Semilogx on the same figure

I am trying to plot on the same figure the evolution of a function f, with argument x in ]0,1]. I would like to see both the evolution of f far away from 0 and close to 0, on the same figure, with one x axis.
For now I only have two different figures, one using plot with x=[0.1 ... 1], and one using semilogx with x=[1e-9 1e-7 1e-5... 0.1]. I would like to have both graphs on the same figure, with the x axis being logarithmic at the beginning, then linear after a certain x0 (let's say x0=0.1).
I do not want something using plotxx since I want only one x axis.
Do you know if this is possible?
Thank you for your time and help.
Just plot your y vector without specifying the x vector, this will get you a uniformly spaced plot, then use XTick and XTickLabel to make it work. For example,
x1=logspace(-10,-1,10);
x2=linspace(1,10,10);
y1=x1.^0.25;
y2=1./x2;
plot([y1 y2],'-x')
xlabels=num2cell([x1 x2]);
set(gca,'XTick',1:numel(x1)+numel(x2),'XTickLabel',xlabels)
If you want to use Latex to format tick labels, you'll need to download a function from the Matlab File Exchange.