MATLAB: Plotting two different axes on one figure - matlab

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

Related

Unable to plot multiple data sets in GUIDE

I'm using GUIDE to display a plot within an axis that contains two data sets: the original signal and the average of the signal, but for some reason it seems to only plot one.
The axis is designated as m_graph and the data sets are avg and signal, which both share time.
plot(handles.m_graph, time,signal)
hold on
plot(handles.m_graph, time, avg)
When I compile the program, only the average is plotted. It seems to skip over the original signal or reset the axis. I've tried plotting just the signal so I know the data is fine.
I feel like I am missing something, maybe the set function?
Sorry my reasoning was a bit wrong; it applies to the current selected axes (it does not parent to the Figure).
However, using axes(h) followed by hold on or just hold(h,'on') will either switch the focus to the axes then turn hold on or turn hold on for a specified axes, respectively.

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.

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

Overlap plots in MATLAB

I currently have a contour plot, just like this one here
http://www.originlab.com/www/helponline/Origin/en/mergedProjects/Tutorial/images/Contour_Plot_with_Major_and_Minor_Levels_Filled_by_Using_Color_Palette/Graph_Gallery_Contour_Plot_Palette_16.png,
now I want to add some simple plots of functions in the length width plane, like f(width) = width^n and so on, but I don't know how to overlap these two plots.
So, you want to retain the current graph when adding new graphs: use hold on. See http://www.mathworks.nl/help/matlab/ref/hold.html.

how to always display "labels" on axis X

If I zoom several time graph all labels from axis X disapear (go away) and there are no visible axis X labels so it is not possible to understand the part of graph where am I.
How can I force matlab to always display labels on axis X and to update them automatically while zooming and to display enough digits so "neighboor" labels must be different.
it depends, are you manually setting the tick marks yourself ('XTick' and 'XTickLabel' axis properties)?
Try this simple example
plot(sin(1:10), 'o-')
without changing anything, you can zoom as much as you want, and the tick labels will always be visible
EDIT
The root cause of the problem is the same as the one raised in your other question, datetick function will manually set the tick labels, thus disabling automatic update on zoom/pan.
The good news is there are already submissions on FEX that tries to solve this exact problem with DATETICK
I run into the same problem even on the new version of MATLAB (r2014). MATLAB does not display sufficient x-axis tick labels as you zoom-in. After several experiments I found the following workaround. Following is a plot before implementing the solution. MATLAB displays only three XTick labels on the x-axis even though there is sufficient space for more (there are often even less labels as you zoom in more).
Suspecting that MATLAB thinks that it does not have sufficient space to display more labels, a workaround can be to rotate the labels. To do that, after you issue the plot commands, e.g.
plot(tsX);
hold on;
plot(tsY);
plot(tsZ);
add the following command
set(gca,'XTickLabelRotation',90);
Now MATLAB plots with more labels
I am going to report this as a bug to the MATLAB guys.