Matlab plot: equal distance between ticks - matlab

I have this 3D plot that I'm making but the data are not linear. This implies that on my plot, the distance between the ticks that I want to show is not equal. How can I adapt the scale of the x and y axis so that this is the case, i.e. so that the axis gets divided into equal parts with the current ticks?
I want the same ticks and tick labels, but that they just have an equal distance in between them on the axes, in stead of small between 0.1 and 0.5 and large between 1 and 5.
The current plot looks like this:
RMSEval = xlsread('RMSEvalues.xlsx');
X = RMSEval(:,1);
Y = RMSEval(:,2);
Z = RMSEval(:,3);
figure(1);
xi = linspace(min(X),max(X),30);
yi= linspace(min(Y),max(Y),30);
[XI,YI] = meshgrid(xi,yi);
ZI = griddata(X,Y,Z,XI,YI);
contourf(XI,YI,ZI);
colormap('jet');
xticks([1e-13 5e-13 1e-12 5e-12 1e-11]);
yticks([1e-18 5e-18 1e-17 5e-17 1e-16]);
colorbar;

A plot where the distance between 0.1 and 0.5 is the same as between 1 and 5 would be a log plot, specifically a log-log plot since you want it on both axes. One way to achieve this would be to transform the X and Y values of your data logarithmically and modify the tick mark labels to match the untransformed values rather than the logarithmic ones you're actually plotting.
A rough guess at a solution is below. I say a rough guess because without posting the data you're importing from the xlsx file or a paired down version of that data (as in an MWE) I can't actually test it.
RMSEval = xlsread('RMSEvalues.xlsx');
X = log(RMSEval(:,1));
Y = log(RMSEval(:,2));
Z = RMSEval(:,3);
figure(1);
xi = linspace(min(X),max(X),30);
yi= linspace(min(Y),max(Y),30);
[XI,YI] = meshgrid(xi,yi);
ZI = griddata(X,Y,Z,XI,YI);
contourf(XI,YI,ZI);
colormap('jet');
xticks(log([1e-13 5e-13 1e-12 5e-12 1e-11]));
xticklabels(cellfun(#num2str,num2cell(),'UniformOutput',false));
yticks(log([1e-18 5e-18 1e-17 5e-17 1e-16]));
yticklabels(cellfun(#num2str,num2cell([1e-18 5e-18 1e-17 5e-17 1e-16]),'UniformOutput',false));
colorbar;

Related

How can I plot only a specific range of a curve, while having axis with higher limits?

I need to plot a section of curve, using MATLAB. But I need my axes to be larger than the part I am showing.
For example, I have the following data:
x = 0:50
y = 0.5*x
I would like to plot this data from x=0 to x=20, with xlim([0 50]).
Just to clarify, I do not want to change the range of values of x, I just want to change what is shown on the graph.
Say you have some data
x = 0:50;
y = 0.5*x;
and you would like to plot only a part of it, say everything where x<=20. You can do as follows:
index = x <= 20;
plot(x(index), y(index))
xlim(x([1,end])) % set the x-axis limit to the range of all your `x` values
ylim([min(y),max(y)]) % set the y-axis limit to the range of all your `y` values
Do this:
x = 0:20
y = 0.5*x
plot(x,y)
xlim([0 50]) % This will set x-axis to the desired range
ylim([min(y) max(y)])

In matlab plot scatter points as different sized points that cover the x and y error instead of error bars

In matlab, I want to plot scatter data with both x and y errors, which I can do this using errorbarxy function.
I am wondering, however, if I can use the upper and lower limits of x and y to instead plot the scatter points as different sized semi-transparent points that cover the error 'region' where the error bars would usually cover?
i.e. how can I achieve scatter(x,y,a,c) where a is the area defined by upper and lower limits in each direction?
My code for the normal errorbarxy is:
X = 10 * rand(7,1);
Y = 10 * rand(7,1);
ux = rand(7,1);
uy = rand(7,1);
lx = rand(7,1);
ly = rand(7,1);
errorbarxy(X,Y,ux,uy,lx,ly,'Color','k','LineStyle','none','Marker','o','MarkerFaceColor','w','MarkerSize',11);
set(gca,'YScale','log');
set(gca,'XScale','log');
Note the log scaling.
Thanks for any ideas!
To achieve a scaling of the size of your scatter points you normally subtract the minimum to shift your data to 0, then divide by the maximum to normalise to the interval [0,1]. In this case I'd recommend some increase of interval, say [4,9] to increase the area for visualisation in scatter. So for one dimension:
X = rand(1e3,1)*8+14; %// some random data to make this example work
X = X-min(X); %// shift to 0
X = X/max(X); %// normalise to [0,1]
X = 5*X+4; %// increase area for visualisation purposes

Adjusting the x-axis scale on a bar chart in MATLAB

I am trying to adjust the scale of the x-axis so that the values are closer together, but I am not able to do so.
I need the output to be like this photo:
However, what I actually get is the photo below:
Here's the code I have written to reproduce this error:
x = [0.1 1 10 100 1000 10000];
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
figure;
bar(x,y);
ylabel('Y values');
xlabel('X values');
set(gca,'XTick', [0.1 1 10 100 1000 10000])
How can I adjust the x-axis so that it looks like the first photo?
Because your data has such huge dynamic range, and because of the linear behaviour of the x axis, your graph is naturally going to appear like that. One compromise that I can suggest is that you transform your x data so that it gets mapped to a smaller scale, then remap your x data so that it falls onto a small exponential scale. After, simply plot the data using this remapped scale, then rename the x ticks so that they have the same values as your x data. To do this, I would take the log10 of your data first, then apply an exponential to this data. In this way, you are scaling the x co-ordinates down to a smaller dynamic range. When you apply the exponential to this smaller range, the x co-ordinates will then spread out in a gradual way where higher values of x will certainly make the value go farther along the x-axis, but not too far away like you saw in your original plot.
As such, try something like this:
x = [0.1 1 10 100 1000 10000]; %// Define data
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
xplot = (1.25).^(log10(x)); %// Define modified x values
figure;
bar(xplot,y); %// Plot the bar graph on the modified scale
set(gca,'XTick', xplot); %// Define ticks only where the bars are located
set(gca,'XTickLabel', x); %// Rename these ticks to our actual x data
This is what I get:
Note that you'll have to play around with the base of the exponential, which is 1.25 in what I did, to suit your data. Obviously, the bigger the dynamic range of your x data, the smaller this exponent will have to be in order for your data to be closer to each other.
Edit from your comments
From your comments, you want the bars to be equidistant in between neighbouring bars. As such, you simply have to make the x axis linear in a small range, from... say... 1 to the total number of x values. You'd then apply the same logic where we rename the ticks on the x axis so that they are from the true x values instead. As such, you only have to change one line, which is xplot. The other lines should stay the same. Therefore:
x = [0.1 1 10 100 1000 10000]; %// Define data
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
xplot = 1:numel(x); %// Define modified x values
figure;
bar(xplot,y); %// Plot the bar graph on the modified scale
set(gca,'XTick', xplot); %// Define ticks only where the bars are located
set(gca,'XTickLabel', x); %// Rename these ticks to our actual x data
This is what I get:

Find point of intersection between histfit and line - MATLAB

How can I find the exact y-coordinate of the red gaussian at x = 0.5 without using Data Cursor?
I want the blue line to end when it touches the gaussian. But I need to find the point of intersection between the gaussian of the histfit shown in red, and the blue line at 0.5. I can access the data points of the histfit plot as follows:
C = get(get(gca, 'Children'), 'YData');
C{1,1}
line([0.5 0.5],[0 max(C{1,1})],'Color','b');
However, the data points of the gaussian don't relate to this axes. Meaning, x-axis of C{1,1} is from 1 - 100 and not from 0.1 to 0.9.
Whats the easiest way to find the y-coordinate of the gaussian at 0.5 so that I can replace max(C{1,1}) by that?
Getting XData as well should give you the right x-values:
C = get(get(gca, 'Children'), 'XData');
Alternatively, the values of YData should be at regular intervals, even if not on the correct scale (since it originated from hist), so you could probably find the y-value corresponding to x=0.5 in the plot.
The point x=0.5 between 0.1 and 0.85 (approximately, from the plot) scales to the point x=53.33 between 1 and 100. If the y-value at x=53 isn't accurate enough for plotting, you can just interpolate the value between 53 and 54 and that should be enough.
Here is some code to that should do the job:
XPlotRange = [0.1 0.85];
XDataRange = [1 100];
XPlotToInterp = 0.5;
XDataToInterp = XDataRange(1) + (XPlotToInterp - XPlotRange(1))*diff(XDataRange)/diff(XDataRange);
XData1 = floor(XDataToInterp);
XData2 = ceil(XDataToInterp);
YInterp = interp1([XData1 XData2], [YData(XData1) YData(XData2)], XDataToInterp);
Here YInterp is the interpolated y-value for the corresponding x-value.

MATLAB - Pixelize a plot and make it into a heatmap

I have a matrix with x and y coordinates as well as the temperature values for each of my data points. When I plot this in a scatter plot, some of the data points will obscure others and therefore, the plot will not give a true representation of how the temperature varies in my data set.
To fix this, I would like to decrease the resolution of my graph and create pixels which represent the average temperature for all data points within the area of the pixel. Another way to think about the problem that I need to put a grid over the current plot and average the values within each segment of the grid.
I have found this thread - Generate a heatmap in MatPlotLib using a scatter data set - which shows how to use python to achieve the end result that I want. However, my current code is in MATLAB and even though I have tried different suggestions such as heatmap, contourf and imagesc, I can't get the result I want.
You can "reduce the resolution" of your data using accumarray, where you specify which output "bin" each point should go in and specify that you wish to take a mean over all points in that bin.
Some example data:
% make points that overlap a lot
n = 10000
% NOTE: your points do not need to be sorted.
% I only sorted so we can visually see if the code worked,
% see the below plot
Xs = sort(rand(n, 1));
Ys = rand(n, 1);
temps = sort(rand(n, 1));
% plot
colormap("hot")
scatter(Xs, Ys, 8, temps)
(I only sorted by Xs and temps in order to get the stripy pattern above so that we can visually verify if the "reduced resolution" worked)
Now, suppose I want to decrease the resolution of my data by getting just one point per 0.05 units in the X and Y direction, being the average of all points in that square (so since my X and Y go from 0 to 1, I'll get 20*20 points total).
% group into bins of 0.05
binsize = 0.05;
% create the bins
xbins = 0:binsize:1;
ybins = 0:binsize:1;
I use histc to work out which bin each X and Y is in (note - in this case since the bins are regular I could also do idxx = floor((Xs - xbins(1))/binsize) + 1)
% work out which bin each X and Y is in (idxx, idxy)
[nx, idxx] = histc(Xs, xbins);
[ny, idxy] = histc(Ys, ybins);
Then I use accumarray to do a mean of temps within each bin:
% calculate mean in each direction
out = accumarray([idxy idxx], temps', [], #mean);
(Note - this means that the point in temps(i) belongs to the "pixel" (of our output matrix) at row idxy(1) column idxx(1). I did [idxy idxx] as opposed to [idxx idxy] so that the resulting matrix has Y == rows and X == columns))
You can plot like this:
% PLOT
imagesc(xbins, ybins, out)
set(gca, 'YDir', 'normal') % flip Y axis back to normal
Or as a scatter plot like this (I plot each point in the midpoint of the 'pixel', and drew the original data points on too for comparison):
xx = xbins(1:(end - 1)) + binsize/2;
yy = ybins(1:(end - 1)) + binsize/2;
[xx, yy] = meshgrid(xx, yy);
scatter(Xs, Ys, 2, temps);
hold on;
scatter(xx(:), yy(:), 20, out(:));