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

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.

Related

Matlab second set of axes not the same dimensions as the first

What I really want to do is what this question is all about:
matlab remove only top and right ticks with leaving box on
To paraphrase the original question, I want to have a plot with tick marks on the left and bottom axes but NO ticks on the top and right axes, but with the box outlining the plot still intact.
Since I don't have enough reputation, I wasn't able to simply comment to follow up. Anyway, I execute the same code given in the answer by mc2, and what I get is the figure below. The logic of the method is to use 2 axes that have the same dimensions and overlay them on top of each other. The first axis has the 'box' property turned off, the second one does not. The code is:
a = gca;
set(a,'box','off','color','none')
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
axes(a)
Assume that the axis that represents "a" already exists. That's the initial figure. (I added the blacked out part on top of the screen shot.) The second axis that was created is clearly not the same dimensions as the initial one--it's the bigger, nearly-square box that surrounds the smaller, rectangular one. Instead, I want the big, nearly-square box to be same dimensions as the smaller one.
Somehow the get(a,'Position') function is not grabbing the correct dimensions that I want it to. Why is this happening?
I didn't make the original figure myself; a collaborator generated the data and sent me *.fig file.

how to change axes limits in a 3D matlab plot

I have a 3-D plot which I want to cut somehow to show the most interested part and avoid the flat parts (as shown in the picture the blue and orange parts to be the least). I think that it can be done using change of the axis limits in x but different for x_{back} and x_{front} which means I want to change the limits of x front axis to (-20,20) and x back to (-80,-40). How can I do this?
I think krisdestruction is right, it would be such an infrequently-used feature that it's probably not worth the development time or added complexity for TMW to implement.
But you could kludge it. If you were to rotate your data so that the feature aligns with the axes then you could crop the plot to the region of interest as desired. Then hide the grid and draw a new one yourself.
If you're careful you can arrange it so that you can still use the axis labels at the front, which will save you some time, but if not you can always use text to draw new ones on.
I would rotate the data using a rotational transform matrix, which will be pretty quick, and you might be able to pull the gridlines out of the gca object and apply the rotation matrix to those too, which would save you from having to compute them all explicitly.
If you expect to do this more than once or twice then you could encapsulate it all in a nice function that works out the rotation angle from a given pair of 'front' and 'back' axis limits.
Then you can post it to the file exchange : )

Place MATLAB legend such that it does not overlap on the plot

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')

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 one modify the axes in MATLAB's phylogenetic tree tool?

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