How to switch display of x & y values on a MATLAB plot, without switching plot() args - matlab

Consider the following code:
x = 0:0.1:pi;
y = sin(x);
plot(x,y)
I want to switch the display of this plot, such that x displays on the vertical axis and y displays on the horizontal axis.
Obviously for this example, the easiest way is to plot(y,x). However in my actual code I'd have a ton of plot calls to edit among several functions, and I want to switch back and forth easily. It's a bird's eye East-North plot, and some experiment geometries don't come out very well with East on the x axis.
Thanks in advance!

what you want is to toggle between views after selecting the axes of you plot.
For example, say you run your code and have a bunch of plots on the screen. You choose the plot you want to flip by clicking on the empty space within the plot box. Then you type in the command line some function name that is designed to flip the plot.
This function can be view ( view(90,90) or view(0,90) ), or a generic function such as:
function flipplot
h=get(gca);
xd=h.Children.XData;
yd=h.Children.YData;
h.Children.XData=yd;
h.Children.YData=xd;
end
you can save that flipplot function as flipplot.m, then each time you do the above (select a plot with your mouse etc) and enterflipplot on the command line you get what you want.

Related

Horizontal line in Matlab with date on x axis

I have a piece of code in Matlab where I need to plot a graph including to lines depending on dates (x axis). I want then to add an horizontal line at y=0 to mimic the x axis as it is not shown due to negative value.
Here is the code:
figure1 = figure('Name','Historical Performance');
plot(QuarterEnd,perf(1,:),'b',QuarterEnd,perf(2,:),'r','DatetimeTickFormat','QQQ-y','LineWidth',2)
xlabel('Quarter');
ylabel('Performance');
ax = gca;
ax.XTickLabelRotation=45;
legend('Historical performance from prediction','Best Historical performance','LOcation','Southwest');
I have tried something like
line(QuarterEnd,[0 0],'Color','g')
But it does not span the right interval. I only need the line to be displayed over the interval where there are data points. Is there an easy way to do so?

Clickable/ Interactive contour plots in Matlab

I have formed a 2D matrix of 180X360. In fact, it is a LatXLong grid of 1°X1°. Each grid point has a value calculated according to my algorithm.
If I want to plot this LatXLong grid using any contour function, it is easy to do.
Now, what I need to do is to make this grid a clickable/ interactive contour plot in a way that when the user clicks anywhere on my grid plot, he gets an onscreen information or a further plot to be displayed specifically related to that grid point.
In short, I want to make a grid/contour plot in which all grid points are hyperlinks and linked to further background information.
check this answer:
if you don't want to have the variable as title of the plot, you can modify the code as:
function mouseExample()
h = plot(rand(10,1), 'o-');
set(h, 'ButtonDownFcn',#buttonDownCallback)
function out = buttonDownCallback(o,e)
p = get(gca,'CurrentPoint');
out = p(1,1:2);
% title( sprintf('(%g,%g)',p) ) % --> no need this line anymore
end
end
the information is saved in the P variable that you can use later.
To get started, look into ginput and text. ginput will let you click on points in your plot and return the coordinates to some function that can generate information to be displayed in the current plot using text of by opening another figure.
You can use ginput in a loop to display multiple data points as you go:
for t = 1:10
[x,y] = ginput(1);
text(x,y,'some info');
end
I don't know of a way to remove the gird lines. NKN's solution might do that for you.

Plotting multiple series in matlab

The following commands produce some very strange results -
plotyy(1:3,2:4,3:5,4:6)
hold on
plotyy(1:3,2.1:4.1,3:5,4.1:6.1)
I basically want to plot two different series on the left y axes and two more series on the right y axes. The above commands work fine for the left series, but produce weird results for the right one. The second green line doesn't look like it should.
The problem that you are having is related to the way that the plotyy creates they plot. plotyy creates two different axes that it plots on, and then mounts them into a single figure. When you issue the hold on command, you are only freezing one of the axes. To fix this, you need to hold each one individually, and then plot back onto them using the plot command.
[ax,hl,hr] = plotyy(1:3,2:4,3:5,4:6);
hold(ax(1), 'on')
hold(ax(2), 'on')
plot(ax(1), 1:3,2.1:4.1)
plot(ax(2), 3:5,4.1:6.1)
Indeed pretty weird behavior. For fun, select the 'hand' tool in the plot window and then drag the graph around, you see that only one of the two green curves moves and that on the right side there are two sets of labels drawn on top of each other. I would qualify this as a bug in matlab (far from the only ugly behavior in Matlab's plots). This seems to be a workaround for what you want to achieve:
[AX, H1, H2] = plotyy(1:3, [2:4;2.1:4.1], 3:5,[4:6;4.1:6.1]);
>> set(H1, 'color','b')
>> set(H2, 'color','g')
Note that this only works if the two left plots have the same set of x-values, and similar for the right plots, like in your case. A=[4000;0;1]. But this is a workaround, the real solution is given by slbass.

How can I order items in a Matlab legend via the figure editor?

I've created a histogram with multiple sets of data. The data sets are color imgages converted to grayscale and taken over a given time period (e.g. pic 1 # time=0, pic 2 # time=5min, etc.) which is why I need the legend entries to show up in a specific order. When I put the legend in, the entries are scattered around in no specific order and I can't figure out how to get the entries switched up the way I need them.
In 2017a and higher (using part of the answer by Peter M above)
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X,Linux) to "Cut" the Plot line.
Create a new figure (File -> New -> Figure).
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line in the new figure.
"Cut" the Plot line in the new figure.
"Paste" the Plot line in the original figure.
Repeat steps 2-6 for every plot line, in the order that you want
them to appear in the legend. Right click on the legend and
"Refresh"
If you are regenerating the plot often of course it is probably a good idea to make sure that the script puts them in the correct order. Luis Mendo provided an answer here, but his answer to a slightly different wording was a little more detailed: how to change the sequence of the legend.
Pre 2017a Only! It seems that 2017a breaks this behavior, so the following trick does not work in the latest versions.
To answer your specific question, here is a neat trick if you are just doing this once in awhile on a plot that doesn't have many lines, and only want to use the Figure Editor...
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X, Linux) to "Cut" the Plot line.
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line.
Repeat steps 2-4 for every plot line, in the order that you want them to appear in the legend.
Right click on the legend and "Refresh"
You can get a handle to each plotted object, and in the legend use the handles to control which string applies to which plotted object.
Example:
h1 = plot(1:5, 1:5, 'r');
hold on
h2 = plot(1:5, 2:6, 'b');
legend([h1 h2],'First red','Second blue')
The answer above also works for R2017a and higher...here is a little bit more general example:
x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;
figure
plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');
legendEntries = get(gca,'legend')
plotHandles = get(gca,'children')
legendEntries = legendEntries.String;
newOrder = [3,4,1,2];
legend([plotHandles(newOrder)],legendEntries{newOrder})
flipud(h) or fliplr(h) to change complete order of the array up-down oder left-right
Through try and error I found this easy fix:
1) select the plot line that should be at position 1 (at the top)
2) Strg + X
3) Strg + V in the same plot figure
Repeat step 2 and 3 about 3 or 4 time. This will result in the plot beeing at the bottom of the legend. Now select the plot that should be at the 2nd position, again Step 2 and 3 a couple times. The previous plot moves one place up, the 2nd picked plot is now at the bottom.... continue with the rest of the plots the same way

Make clicking MATLAB plot markers plot subgraph

In Matlab 2011b, I have a multidimensional matrix which is to be initially presented as a 2D plot of 2 of its dimensions. I wish to make the markers clickable with the left mouse button. Clicking on a marker draws a new figure of other dimensions sliced by the clicked value.
This question is related to Matlab: Plot points and make them clickable to display informations about it but I want to run a script, not just pop up data about the clicked point.
Googling hinted that ButtonDownFcn could be used, but examples I found require manually plotting each point and attaching a handler, like so:
hp = plot(x(1), y(1), 'o');
set(hp, 'buttondownfcn', 'disp(1)');
As there are many markers in the main graph, is it possible to just attach a handler to the entire curve and call the subgraph-plotting function with the index (preferable) or coordinates of the marker clicked?
this is an idea of what you need, and should help get you started if I understand your requirements.
In this case, when you select a curve, it will draw it in the bottom subplot preserving the color.
function main
subplot(211)
h = plot (peaks);
set (h,'buttondownfcn', #hitme)
end
function hitme(gcbo,evendata)
subplot (212)
hold on;
col = get (gcbo,'Color');
h2 = plot (get (gcbo,'XData'),get (gcbo,'YData'));
set (h2,'Color', col)
pt = get (gca, 'CurrentPoint');
disp (pt);
end
You can explore your options for get by simply writing get(gcbo) in the hitme function.