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).
Related
I have a Matlab plot that contains relatively high amount of different data and hence if i use standard legend representation, i could not figure out which legend color belongs to which curve.
So, is there any way to put legends on each curve? Lets say "BUS7" text should be located on the purple curve (undermost one).
Thanks.
I am trying to do something like this:
The text(x, y, txt) function allows you to add text at a given location on the plot. link.
Looking at your example, it seems that you want to have all the labels on the left-hand side. So, one way to do it would be like this, where I have generated my own dummy data:
leg = {'First Curve', 'Second Curve', 'Third Curve'};
figure;
x = linspace(1,10,100);
for ii = 1:3
y = x + ii*10;
plot(x, y);
hold on
text(x(1), y(1)+2.5, leg{ii});
end
Regretfully, this does not do anything clever with where the label is placed - it is merely placed slightly above the first point of the curve. You can clearly easily move it along the curve, but if you have many curves, like in your case, the labels will likely overlap and you will need to tinker a bit to avoid this.
I want to plot a graph, mark a point and write legend in a loop.
labels = {}
for i = 1: size(error,1)
r = plot(handles.axes1,temp(i).time,temp(i).signal);
hold (handles.axes1, 'on')
a = %find minimum index
xmin = temp(i).time(a);
ymin = temp(i).signal(a);
plot(handles.axes1,xmin,ymin,'ro')
labels{end+1} = sprintf('Type : %s, %f,%f',error{i,1}.main(1,1).type,xmin,ymin)
end
grid on
legend(r, labels);
Labeling does not work, as it takes only 1st elements ignoring extra element. and the whole method is a mess with color code all messed up, is there elegant way of doing this where my legend colour matches the plot ones
Another way to do this is to use the DisplayName keyword.
for i = 1:N_lines
%...
r(i) = plot(handles.axes1, temp(i).time, temp(i).signal, 'DisplayName', labels{i});
%...
end
legend('show')
The advantage of doing it like this is that it attaches the name directly to the plotted point. If you are debugging, and open the plot as its being written in the plot browser, as each point get plotted the name will appear next to the point in the right-hand pane. You also don't need to keep track of a separate labels variable in case you end up later reordering your points for some reason. This way the labels always travel with their associated points.
I should add that when you call the legend command with a cell of labels, among other things it backfills 'DisplayName' so that you can still change and query it after the plot has been built even if you don't explicitly set it like I've done above. However, I find this method to be self-documenting and more convenient because it's one less thing to keep track of.
You need to make r an array. As it is, you're only storing the last plot handle so when you call legend it's only going to use the last plot and the first label.
% Create a unique color for each plot
colors = hsv(size(error, 1));
for i = 1:size(error, 1)
r(i) = plot(handles.axes1,temp(i).time,temp(i).signal, 'Color', colors(i,:));
%...
labels{i} = sprintf('Type : %s, %f,%f',error{i,1}.main(1,1).type,xmin,ymin);
end
legend(r, labels)
As a side-note it's best to not use i or j as your looping variable and also, don't name your variable error as that's the name of a MATLAB built-in.
I used Matlab to create a polar coordinate and converted it into Cartesian coordinate.
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
I get data on this mesh and produce a contourf plot over it.
My problem is that I get a center line in my contourf plot which I would like to remove, could some help me with this
Thank you
If I extend a little bit your example to get something I can plot, I do reproduce the problem:
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
Z = sqrt( X.^2 + Y.^2 ) ;
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel) ;
The black line at the interface is because the function contourf create patch objects and these objects tend to "close" themselves (they will create a line between the first and last point defined in their profile).
This is easier to observe if you do not complete the definition of your profile over 360 degrees. The picture on the right shows you the same example but with the grid only from 0:350 and with the LineStyle set to :.
As you can see, it is difficult to control how Matlab will actually render this specific profile limit. There are ways to control specific edges of patch objects but in this case it would involve retrieving the handle of each patch object (10 in my case but many more in more complex cases), locating the edge you want to control and basically redefining the patch (each of them). You'd be better off drawing the patches from scratch yourself.
Fortunately, there is a simple ways out of that: Get rid of all the patch edge lines...
but then you may miss your isolines! No problem, just plot them on top of the patches !
You get all your colored patches (with no border) and a set of (iso)lines over which you have full control.
Two easy way to get you patch without lines (i) set the shading to shading flat, or (ii) specify 'EdgeColor','none' in the parameter of the contourf function.
To get your isolines on top, use the sister contour function.
So using the same X,Y and Z data than previously:
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel,'EdgeColor','none') ; %// set of patches without border
% shading flat %// use that if you didn't specify ('EdgeColor','none') above
hold on
[C2 ,hc2] = contour(X,Y,Z,isoLevel,'LineColor','k') ; %// now get your isolines
will render:
It's a good idea to store the handle hc2 in case you want to modify your isolines properties (color, style, thicknes etc ...).
Also, specifying the isoline levels is recommended. This way you can make sure both contour and contourf will use the same set of isovalues. It could probably work without this (because the underlying dataset is the same), but personally I always prefer to be explicit and not rely on background calculations.
I'm trying to make a legend in a matlab figure that takes less space, in particular vertical size. Ideally, if I could change the inter-line spacing of the legend, this would have solved it, but I can't seem to find a way to do it.
I've searched around in Mathworks questions and Google. I've even tried to "write" from scratch the legend, but it doesn't work when I try to export to eps.
Is there a way to control the inter-line spacing in Matlab's legend? something undocumented maybe?
One way would be to adjust the aspect ratio of the legend:
set(h,'PlotBoxAspectRatioMode','manual');
set(h,'PlotBoxAspectRatio',[1 0.8 1]);
(Default is [1 1 1]).
You can also play with the exact positioning of the various legend elements. If h is the handle for your legend:
hc = get(h,'Children');
Now, hc will be of length 3 x the number of items in your legend.
hc(1) is the marker, hc(2) is the line, hc(3) is the text.
(and so on for subsequent items).
hc(1) will have a entry YData(vertical position, single value), hc(2) also has YData (vertical position - vector of two identical values), and hc(3) contains Position - [x y z] vector. The Y values for all these three should be the same.
Obtain the y-positions:
yd = zeros(length(hc)/3,1);
for n = 1:length(yd);
yd(n) = get(hc(1+(n-1)*3),'YData');
end
Assuming your legend doesn't have any text that goes over more than one line,yd should be evenly spaced steps. Define a new spacing of your choice, yd2.
Set new positions:
% markers
for n = 1:length(yd2)
set(hc(1+(n-1)*3),'YData',yd2(n));
end
% lines
for n = 1:3
set(hc(2+(n-1)*3),'YData',[yd2(n),yd2(n)]);
end
% text
for n = 1:3;
pos = get(hc(3+(n-1)*3),'Position');
pos(2) = yd(n);
set(hc(3+(n-1)*3),'Position',pos);
end
Problem - this shifts all the text and markers but doesn't change the border box. The easiest way if trying to make fine adjustments is to first define the final size/position of the legend, and then reposition/resize elements within to taste.
Also, you may find that when writing out images MATLAB helpfully redraws the image, resetting your adjustments. See this blog post on Undocumented MATLAB for info on that.
I used the regress function to find the slope for some data I plotted. I have managed to plot the data and the fitted line both on the same plot. I know how to make it clear that the fitted line is the slope, but I would also like to add a box in corner of the graph (dont care where) that shows the actual value of the slope (basically shows the value that the regress function returns), and I'm trying to find a way to do this automatically (like if there's a function for that or something). Can anybody help (I hope I explained my question well enough...)?
I didn't try to recreate your slope line but have you considered using an annotation?
Example:
x = [-1:.2:1];
plot(x,x.^2,'-bo');
annotation('textbox', [.4 .4 .1 .1], 'String', ...
['slope at x = 0.6 is: ',num2str(2*.6)]);
Which shows:
Of course you can control how the box is positioned, formatted, and so forth.
Check the help files for more detailed info. In some cases you might also consider using a legend().
The function text adds text to a figure. It requires a position and a string to display. In addition, you can highly customize the appearance of the text. For example:
x = 1:100;
y = randn(size(x)) + 0.3*x;
plot(x,y,'.');
p = polyfit(x,y,1);
hold on;
plot(x, polyval(p,x),'k-');
h = text(min(xlim(gca)), max(ylim(gca)), ...
sprintf('%fx + %f', p(1), p(2)),...
'verticalalignment','top',...
'horizontalalignment','left');
Then, to see the various settinsg you can change, look at:
get(h)
Those properties can almost all be changes at creation (like verticalalignment above) or after creation (e.g. set(h, verticalalignment, 'top')).