How does one modify the axes in MATLAB's phylogenetic tree tool? - matlab

I want to display pretty dendrograms for some agglomerative clusters I am generating in Java. I write the clusters out to a file in Newick format. Then, I can get a pretty picture that is almost what I want.
tr = phytreeread('myfile.tree')
phytreetool(tr)
Unfortunately, the X axis is not what I want. I would prefer to "reverse" the axis, because the iterations of the clustering progress from right to left, e.g. firstName and setFirstName get clustered in the first iteration. Does anybody know how I can do that, or at least turn off the X axis labeling? (What is the default axis trying to tell me anyway?)

First, you will need to gain access to the handle for the axes in which the dendrogram is plotted. If it's the only figure open, you can use the function FINDALL like so:
phyAxes = findall(0,'Type','axes');
Now, what you want to change isn't the x-axis direction, since this will reverse the plotted dendrogram as well. You actually want to change just the labels used for the x-axis tick marks. If you want to just turn them off, you can do this:
set(phyAxes,'XTick',[]);
Now, I'm not sure what the x-axis is meant to tell you. In your example, it appears that each branch point is positioned at an integer value along the x-axis starting at 0 for the left-most branch point (the "root", I guess). The right-most branch containing firstName and setFirstName is positioned at a value of 21. If you want to change the axis labeling so that the right-most branch is at 0 and the left-most branch is at 21, you can modify the axes as follows:
set(phyAxes,'XTick',0:21,'XTickLabel',num2str((21:-1:0).'));

This could help you?
set(gca,'XDir','reverse')
EDIT
You may find a lot of interesting here. Cheers!

I needed something similar. I don't know if it is new to Matlab R2021b, but there is now a plot(tree) function which will draw the tree on a regular Matlab figure. After plotting, I identify the largest X value and update the XTickLabels.
B = [1 2 ; 3 4];
tree = phytree(B);
h = plot(tree);
ax = h.axes;
xdata = get(findobj(h.axes,'Type','Line'),'XData');
maxdist = max([xdata{:}]);
ax.XTickLabel = num2str(maxdist - str2double(ax.XTickLabel));
Phylogenetic tree with reversed Xtick labels

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

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:

Indicate name of filled areas inside area plot - Matlab?

It's difficult to identify in this area plot each one of the many filled areas by just looking at the legend (it's like 16!).
So, I was wondering if there's a way to place some sort of labels (with a pointer perhaps?) inside the plot that clearly identifies each filled areas?
Thanks!
Here is an alternative using annotation objects and the textarrow option, which displays text along with an arrow. This could be useful to you to point in narrow regions where text would hide data.
For example:
clear
clc
close all
x = 1:128;
%// Plot data
figure
hAxes1 = axes('Position',[.1 .1 .6 .8]);
image(x.');
axis off
colormap(jet(128))
%// Define labels
ColorLabels = {'Red';'Orange';'Green';'Blue';'More blue'};
%// Define starting and ending x and y positions
xstart = .8;
xend = .6;
ystart = linspace(.1,.8,numel(ColorLabels));
yend = linspace(.15,.8,numel(ColorLabels));
for k = 1:numel(ColorLabels)
annotation('textarrow', [xstart xend],[ystart(k) yend(k)],...
'String', ColorLabels{k});
end
gives the following output:
There may be a builtin way that I don't know about but I find the lower-level text function is usually the best answer for this kind of thing.
It'll require some housekeeping - you have to supply an x and y coordinate and the text to print, e.g.
text(20, 400, 'Region 4')
which will centre the label on (20,400) (see the 'VerticalAlignment' and 'HorizontalAlignment' name-value pairs in the docs for fine-tuning), so for me it's usually preferable to write a small wrapper that will work them out from the data. Of course this is usually specific to the particular type of data you're using and rarely generalises to other uses of area plots, but that's most likely why there isn't already a generic labelling function (that I'm aware of).

How to reverse the direction of Y-Axis of MatLab figure generated by `imagesc()` function

I am trying to display data in a matrix using imagesc() function but it is showing row index in decreasing order (Assuming origin at left-bottom). Any idea what mistake i could be making or how to correct this?
The matrix only has zeros and ones in it.
Set Ydir property of the current axes to normal
By default, imagesc uses reverse for YDir
set(gca,'YDir','normal');
See Documentation for Axes properties
Before:
After:
Note: This completely flips the inside data as well (it supposed to). As you are dealing with matrices, I hope this is what you want.
If you don't want to affect inside data, you need to change order of YTickLabels instead.
There's another option which requires slightly less code:
axis ij
Reverse the coordinate system so that the y values increase from top to bottom.
As in this case (as it is already reversed), you could use
axis xy
To get back to normal, so that y values increases from bottom to top.
As mentioned in the docs of axis.

Remove tick marks on second y-axis, but retain box on

Provided with some code like the following:
x=1:.5:10
plot(x,sin(x))
set(gca,'box','on')
I'm trying to get the left axis to maintain it's tick marks and the right axis to have none.
I know I don't want to do the following:
set(gca,'box','off')
set(gca,'Ytick','[]') %this turns off the left and right axes tick marks. I just want the right off.
I would really,really prefer to not use plotyy. Any help would be appreciated.
Are creating dumby axes the only option here?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/261486
I think that you are stuck using a dummy axes (or a variety of even more unappealing options).
FWIW, the code required is only a few lines; the shortest I can get it is below:
a1 = axes('box','on','xtick',[],'ytick',[]); %Under axis, used as background color and box;
a2 = axes(); %Over axis, used for ticks, labels, and to hold data
propLink = linkprop([a1 a2],'position'); %Keep the positions the same
x=1:.5:10 %Make and plot data
plot(a2, x,sin(x))
set(a2,'box','off','color','none'); %Set top axes props last ("plot" overrides some properties)