Matlab date help on x-axis - matlab

I have a porkchop plot that looks similar to this:
The contour function used to make it has input arguments for the x and y positions that are serial dates (as that seemed to be required by MATLAB). Then I used the following command to get the format I want:
datetick('x', 2); datetick('y', 2);
The problem I am having is that when I zoom in on the plot the tick labels to not autogenerate and I can be left with no ticks on the x or y axis if I zoom in to use a weeks' date range for example.
I tried enabling 'auto' for XtickMode and YtickMode but when I zoom in or pan after using those commands the relationship between the independent and dependent variables are lost for some reason (aka the dates don't stay with data like they do when you just have numbers on the x axis and zoom in).
Any ideas on how to solve this issue to get the functionality I'm looking for?
I have also tried the command xtickformat('dd-MMM-yy') but I get an error "Invalid numeric tick label format." when I use it with the contour plot.

As far as I know there is no builtin method in MATLAB to do this. I use the datetickzoom function from the MATLAB FileExchange. If you replace all instances of datetick with datetickzoom, it will automatically update the relevant axis labels when you zoom in.

Related

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

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 ''.

Scatter with 2 y-axes and datetick

I would really appreciate some help.
I have to independent datasets. Each dataset contains two variables: dates (as datenumber) and corresponding data. I need to plot both datasets on one scatter plot with dates on x-axis and two y-axes. I have been trying with the following code:
figure(1);
scatter(x1,y1,'g');
set(gca);
ax1=gca;
set(ax1,'YColor','g');
ax2 = axes('Position',get(gca,'Position'),'YAxisLocation','right', XTick'[],'Color','none','YColor','r');
hold on; scatter(x2,y2,'r');
Now, this gives correct y-axis on the right side, however on the right side I end up with two overlapping y-axes.
Also, I need to change x-axis so that it displays dates as opposed to datenumbers. I've tried to incorporate datetick into the code but it again gives me two overlapping x-axes.
Does anyone know how to go about it?
thank you
I tried your script with your sample input and found no problems. Anyway, here's a solution which uses the matlab function plotyy, which is suited to simple plots like this:
%generate input
x1=[732490 732509 732512 732513 732521 732528];
y1=[7.828 7.609 22.422 14.758 26.258 1.477];
x2=[732402 732403 732404 732404 732433];
y2=[0.693 0.645 0.668 0.669 0.668];
figure(1);
[ax, h1,h2]=plotyy(x1,y1,x2,y2,'scatter');
%set colors manually
green=[0 1 0];
red=[1 0 0];
set(h1,'cdata',green);
set(h2,'cdata',red);
set(ax(1),'ycolor',green);
set(ax(2),'ycolor',red);
%note the 'keepticks' and 'keeplimits' options
datetick(ax(1),'x','yyyy-mm-dd','keepticks','keeplimits');
datetick(ax(2),'x','yyyy-mm-dd','keepticks','keeplimits');
Without the datetick call the plotyy function synchronizes the xticks in the plot. When you call datetick, it recalculates the ticks, unless you explicitly tell it not to, see the option keepticks, and this is seen as two sets of x axes (even though the x coordinates are the same, the ticks are located at different positions). The keeplimits option is needed to preserve the original xlim. It obviously needs some more manual work to get plots like this sufficiently pretty.
Also note that I set the axes and data colors manually: there might be a way to do this more elegantly.
Update: keeplimits originally missing
Update2: changed sample data to correspond to updated question comment

Scatter polar plot in matlab

I'm trying to do a wedge plot (right ascension vs redshift). I was thinking I could use a scatter plot in polar coordinates. The polar function in matlab seems very limited. Even this
polar(a(:,1),a(:,2),'Linewidth',1)
gives me an error:
Error using polar (line 23)
Too many input arguments.
Is there a simple way to achieve what I want using Matlab? Do you know of another software that would do it easily?
Thanks,
Mike
Matlab is quite adequate for that, I think.
As for the polar function, it seems it doesn't allow properties (such as 'linewidth') to be specified directly. But you can get a handle to the created object and then set its 'linewidth', or other properties:
h = polar(a(:,1),a(:,2));
set(h,'linewidth',1)
If you want a scatter plot, maybe you'd prefer not to have lines, but instead to plot a marker (such as a dot) at each point:
h = polar(a(:,1),a(:,2),'.');
set(h,'markersize',12)
Example:
To see a list of properties that you can set, as well as their current values, type
get(h)

MATLAB: Plotting two different axes on one figure

The MATLAB surf plot below is essentially two plots plotted adjacent to each other. For clarity, I have included some code below used to prepare the plot:
band1 = horzcat(band1, eSurface2(:,:,1));
band2 = horzcat(band2, eSurface2(:,:,2));
surf(band2,'DisplayName','band2');
surf(band3,'DisplayName','band2');
I would like for the y axis numbering to restart at the start of the second graph. How do I go about doing that?
You can use the 'YTick' and 'YTickLabel' properties of the axis to control the ticks, this way you can make it start from zero for the second graph. It will require some trail and error to get it right. See the relevant doc here (you'll have to scroll all the way to the bottom of the page).
Take advantage of the following feature of 'YTickLabel': "If you do not specify enough text labels for all the tick marks, MATLAB uses all of the labels specified, then reuses the specified labels".

How does MATLAB's normpdf function work?

When trying to plot a normal PDF with mean=0 and standard deviation=20 using the MATLAB command normpdf() I get weird results, see picture.
The code used to plot the figure is as follows:
plot(normpdf((-100:0.1:100),0,20))
What is the correct way of using this function?
When you call plot with ONE argument, it plots those numbers on the y axis, using the index numbers of those values for the x axis. If you wanted the x axis scaled properly, you had to provide them in the first place. Thus...
x = -100:0.1:100;
plot(x,normpdf(x,0,20),'-')
I assume you expected the x-axis to be centered at 0? You need to specify an x-vector for plot. Try plot([-100:0.1:100], normpdf((-100:0.1:100),0,20));.