Matlab 2016a - how to get xTickLabels on only some of the xTicks on an imagesc? - matlab

I'm trying to create a 24x366 heatmap using imagesc with the x-axis labelled at 13 evenly spaced points as {'jan 15','feb 15',...,'dec 15','jan 16'}, and the y-axis labelled at every row from 1 to 24, like this:
Desired imagesc axes
When I run the script, it displays the y-axis as I want it, but it only displays the first label on the x axis and ignores the others. I can get this to work for a plot, but I can't get it to work for an imagesc. I've included my script below. Does anyone know how to make the imagesc display all 13 labels on the x-axis at evenly spaced intervals?
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
figure
imagesc(rand(24,366))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)

The problem with your code is that your only one of your ticks from testspacing falls in the range of the plot testspacing=[1 43201 ...]. You can check the range of your x-axis by running xlim without any arguments.
You can rescale testspacing to fit your x-axis e.g. like:
xmax = 366;
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
testspacing = testspacing/max(testspacing)*xmax;
figure
imagesc(rand(24,xmax))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)
or you just generate testspacing properly. Since you are putting own labels on the axis anyway you might just choose use testspacing = [0:30.5:366] or testspacing = [0:30:366] depending on what you want. This will also help you debug your own code later on.
On another note you should think about reducing the number of labels in general and decide which of the labels are really helpful. Maybe every 2nd or 3rd month is enough. You can "remove" individual labels by setting them to empty strings ''.

Related

How to rescale vector field onto a different grid?

I currently have a vector field that looks something like this, generated with the following basic structure, where Z is some matrix:
[X,Y] = meshgrid(x,y)
[grad_x, grad_y] = gradient(Z)
quiver(X,Y,grad_x,grad_y)
I would like for this plot to be rescaled, such that the x-axis ranges from 1.5 to 3.8 and the y-axis ranges from 100 to 250, but for the arrows themselves to look identical. The only difference in the figure should be the axes labels.
I have tried:
grad_x_rescaled = [(grad_x - min(grad_x))./(max(grad_x)-min(grad_x))].*(3.8-1.5);
grad_y_rescaled = [(grad_y - min(grad_y))./(max(grad_y)-min(grad_y))].*(250-100);
But the problem with this is that although the grad_x and grad_y get rescaled overall, the scaling of the arrows themselves relative to each other are not conserved, and results in below (note the thick black streaks are presumably arrowheads, but the important thing is that the direction and relative sizes of the arrows are not exactly like in the first case.
Is there a matlab function or an expression to renormalize data into a new range, but such that the renormalized data is scaled relative to itself (such as the arrows should be scaled the same relative to one another)?
To simply change the axes tick labels you could use Matlab's ability to specify tick marks and tick labels. Basically you would just tell Matlab where to put the ticks and what the labels should say like this:
xticks(linspace(0,1,6))
xticklabels(linspace(1.5,3.8,6))
yticks(linspace(0,1,6))
yticklabels(linspace(100,250,6))

Aligning histogram plots

I have a given data set, and I want to compare the histograms of this data when represented as a bar histogram and a line histogram. Specifically, I want to use
myhist = histogram(mydata)
to get the bar histogram, and plot on the same figure a line histogram using
mylinehist = plot(myhist.Values)
However, when I do that, I get the following figure
It seems like the line histogram mimics the shape of the bar histogram, but offsets it by a certain amount on the x-axis. Is there a way to align the two so I can have them overlapping? I tried using a command like
align([mylinelist,myhist],'Left','None')
but to no avail. Thanks!
You need to specify the x-axis values for your line plot. These should be the midpoints of your histogram bins.
Try:
midpts = myhist.BinEdges + (myhist.BinWidth / 2);
plot(midpts(1:myhist.NumBins), myhist.Values);

How to set matlab xticks equal distance with unequal numerical spaces?

I am plotting data which is polynomial but I would like to show it on a straight line so that it is clearer.
I want the x-axis tick marks to show N^2 where N is the tick location. I also want the actual ticks on the graph to be evenly spaced even though the numbers aren't.
I tried to change the ticks using
xtick_squared = (0:10:100).^2
set(gca,'xTick',xtick_squared,'xTickLabel',xtick_squared)
but it gives this
How can I display them each being even spaced while reatining their values as well as adjusting the data?
This should work fine:
x = 0:10:100;
y = (x .^ 2) + 5;
labs = sprintfc('%d',x.^2);
plot(x,y);
set(gca,'XTickLabel',labs);
Actually, it's all about changing the tick labels without changing the ticks themselves. Here is the final output:
You can specify the location of x-ticks and then rename them.
Here is the code example:
x=[0,10,20,30,40,50].^2;
plot(x,x)
set(gca,'XTick',linspace(0,2500,6),'XTickLabels',num2cell(x))
P.S.: Note, that your actual data is plotted on evenly spaced grid. I've just renamed the ticks' names.
Edit: if you want to plot your actual data, you need to scale x-axis correspondingly. If you want to plot(x,x) you need to call plot(sqrt(x),x) instead:
x=[0,10,20,30,40,50].^2;
plot(sqrt(x),x)
set(gca,'XTick',linspace(0,2500,6),'XTickLabels',num2cell(x))

Matlab - Plot difference between two graphs in specific points

I have two graphs, one is the exact graph of a solution, the other is a numerical approach. I have 4 specific points in my figure (t=0.25,0.5,0.75,1), where I want to illustrate the difference between the two graphs with a straight line. I found the errorbars function but i don't see any use there. Hope you can help me!
Edit:
this is the example figure:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
I have 4 points now, t=0.25; t=0.5; t=0.75; t=1; At this points, I just want a vertical line between the two plots. I already have tried this: plot([t(1),y(1)],[t(1),x(1)])
but it just creates a line over the whole figure.
✶ It seems that you're not using hold on before using plot command the second time because otherwise you'd have got the desired result (which is actually not a correct way of plotting a vertical line).
✶ You're mixing up the values of x and y for plot(x,y). To plot a vertical line, it should be used like this: plot([x,x], [y1,y2])
For your case, you may not notice the difference between plot([t(1),y(1)],[t(1),x(1)]) (which is incorrect) and plot([t(1),t(1)],[x(1),y(1)]) (which is correct) because it is by chance that the values are same. Plot it for some other points and you'll realize the difference.
Fixed Code:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
hold on
plot([t(1) t(1)],[x(1) y(1)])
% You have 't' on your horizontal axis and 'x'and 'y' values on the vertical axis
axis equal % just for better visualization
Output:

How to set x and y values when using bar3 in Matlab?

Quick version
How can I control the x- and y-values for a 3-d bar plot in Matlab?
Details
Say we have an 10 x 20 data matrix and we plot it using bar3, and we want to set the x- and y-values. For instance:
foodat = rand(10,20);
xVals = [5:14];
yVals = [-3:16];
bar3(xVals, foodat);
xlabel('x'); ylabel('y');
Is there a way to feed it the yVals as well? Otherwise the y axes always defaults to [1:N].
Note I don't just want to change the labels using XTickLabel and YTickLabel. I need to change the actual values on the axes, because I am plotting multiple things in the same figure. It isn't enough to just change how the (wrong) axis ticks are labeled. So this is different from issues like this:
How can I adjust 3-D bar grouping and y-axis labeling in MATLAB?
Other things I have tried
When I try changing the xvals with:
set(gca,'XTick', xVals)
set(gca,'YTick', yVals)
The values are taken in, but actually show up on the wrong axes, so it seems x and y axes are switched using bar3. Plus, it is too late anyway as the bar graph was already plotted with the wrong x- and y-values, so we would end up giving ticks to empty values.
Note added
Matlab tech support just emailed me to let me know about the user contributed function scatterbar3, which does what I want, in a different way than the accepted answer:
http://www.mathworks.com/matlabcentral/fileexchange/1420-scatterbar3
I found a way of doing it. Ill give you a piece of code, then you'll need to "tidy up" , mainly the axis limits and the Xticks, as bar3 does set up the Xticks inside, so if you want others you'll need to set them manually yourself.
So the trick here is to get the Xdata from the bar3 handle. The thing here is that it seems that there is a handle for each row of the data, so you need to iterate for each of them. Here is the code with the current output:
foodat = rand(20,10);
xVals = [5:14];
yVals = [-3:16];
% The values of Y are OK if called like this.
subplot(121)
bar3(yVals, foodat);
subplot(122)
h=bar3(yVals, foodat);
Xdat=get(h,'XData');
axis tight
% Widdth of barplots is 0.8
for ii=1:length(Xdat)
Xdat{ii}=Xdat{ii}+(min(xVals(:))-1)*ones(size(Xdat{ii}));
set(h(ii),'XData',Xdat{ii});
end
axis([(min(xVals(:))-0.5) (max(xVals(:))+0.5) min(yVals(:))-0.5, max(yVals(:))+0.5])
Note: Y looks different but is not.
As you can see now the X values are the ones you wanted. If you'd want other size than 1 for the intervals between them you'd need to change the code, but you can guess how probably!