Add labels for x and y using the plotmatrix function MATLAB - matlab

I managed to plot a matrix (16X16) but I want to add labels for each single x and y axis's. As shown below, the labels are written vertically on the y and mixed with each other and also written on the x graph itself and also mixed. Is there a way to add the labels beside the axis without being mixed (as show in the second photo)?
Current graph:
What I want to do:
My code (just stopped after plotting three label as it wasn't working):
[~,ax] = plotmatrix(corr);
ylabel(ax(1,1),'ABCDEFGHIJKLMNOP')
ylabel(ax(2,1),'ABCDEFGHIJKLMNOP')
ylabel(ax(3,1),'ABCDEFGHIJKLMNOP')
xlabel(ax(16,1),'ABCDEFGHIJKLMNOP')
xlabel(ax(16,2),'ABCDEFGHIJKLMNOP')
xlabel(ax(16,3),'ABCDEFGHIJKLMNOP')

If I understand your problem you want to be able to give individual y-labels for the rows and x-labels for the columns. Unfortunately when you use xlabel and ylabel the resulting text overlaps. Here are two solutions
Solution 1: Use the big-axes to set the labels
Use single label for the horizontal axis and vertical axis by referencing the big-axes
[~,~,HBigAxe] = plotmatrix(corr);
xlabel(HBigAxe,'Horizontal Label for Columns');
ylabel(HBigAxe,'Vertical Label for Rows');
Solution 2: use rotation and alignment to avoid overlapping labels
If you want each row and column to have there own labels you can rotate and set the horizontal alignment of the label. For example:
[~,ax] = plotmatrix(corr);
ylabel(ax(1,1),'Y Axis Label','Rotation',0,'HorizontalAlignment','right')
xlabel(ax(end,1),'X Axis Label','Rotation',90,'HorizontalAlignment','right')

Related

Matlab 2016a - how to get xTickLabels on only some of the xTicks on an imagesc?

I'm trying to create a 24x366 heatmap using imagesc with the x-axis labelled at 13 evenly spaced points as {'jan 15','feb 15',...,'dec 15','jan 16'}, and the y-axis labelled at every row from 1 to 24, like this:
Desired imagesc axes
When I run the script, it displays the y-axis as I want it, but it only displays the first label on the x axis and ignores the others. I can get this to work for a plot, but I can't get it to work for an imagesc. I've included my script below. Does anyone know how to make the imagesc display all 13 labels on the x-axis at evenly spaced intervals?
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
figure
imagesc(rand(24,366))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)
The problem with your code is that your only one of your ticks from testspacing falls in the range of the plot testspacing=[1 43201 ...]. You can check the range of your x-axis by running xlim without any arguments.
You can rescale testspacing to fit your x-axis e.g. like:
xmax = 366;
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
testspacing = testspacing/max(testspacing)*xmax;
figure
imagesc(rand(24,xmax))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)
or you just generate testspacing properly. Since you are putting own labels on the axis anyway you might just choose use testspacing = [0:30.5:366] or testspacing = [0:30:366] depending on what you want. This will also help you debug your own code later on.
On another note you should think about reducing the number of labels in general and decide which of the labels are really helpful. Maybe every 2nd or 3rd month is enough. You can "remove" individual labels by setting them to empty strings ''.

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

How do you accurately set the offset of an axisLabel in CorePlot?

Problem Statement:
A bar plot in created with CorePlot has a Y-axis that features a dynamic range. The minimum value of this range is presumed to be <= 0. The maximum value of the range is presumed to be > 0.
The bar plot features custom X-axis labels which have an offset to distance themselves from the 0 line of the Y-axis. (label.offset)
However, because the exact location of the 0 line may change during runtime, a static value to offset the X-axis labels is not sufficient.
Question:
What is the appropriate way to accurately set the offset of Axis labels, when axis parameters change at runtime?
Screenshot attached to show problem. Labels should be below the ($200,000) line, but because the top end value is dynamic, they move up and down.
http://i.imgur.com/jdwy1c2.png
From the linked picture, it doesn't look like you're drawing the axis line. Use axis constraints to hold the x-axis at the bottom of the plot area. If you do want to draw an axis line, e.g., at y = 0, add a second x-axis. Use one to draw the axis line and the other to position the labels. Set all of the line style properties of the second axis to nil so it doesn't draw any lines and use constraints to hold it at the bottom of the plot area.

how to add additional label on x-axis on the rightmost in matlab figure?

I have the tick label set for the x-axis already and I also have the label set for the x-axis too. I am seeking a way to put an additional x label to the rightmost of the figure next to the x tick labels, is there any way to do so? Thanks
just like the following figure, the x ticks was added automatically and x label was added by the command xlabel. But I want to add one additional label "add'l" (red) in the figure. But I have many plots and/or subplots and the axes might be different, so I need to add that label with problem instead of manually.
I suggest using a uicontrol that will be carefully placed in the relevant area.
uicontrol('style','text',....);
If your UI is resizable, be sure to have the same units, i.e. if the text units are Normalized, then the axes units should be Normalized as well.
Check out this example on the MATPLOTLIB gallery page: http://matplotlib.org/examples/pylab_examples/alignment_test.html
It shows adding all kinds of different text to the axis.

Matlab: Detect peaks above a certain height on bar graph and label just above peak height

I am trying to add labels to a bar chart with many bars. The complicating factor is that I am doing this in a loop (for hundreds of bar-charts) and want to have Matlab do the labeling automatically. Essentially, I only want to label peaks with a height above a certain threshold. One thing which should hopefully make this easier is that I simply want to label the bar with it's x-value.
Here's an illustration of how I want the label placed:
If you still have access to the original data, and assuming you want to label each point that's above the threshold, you should be able to do this by:
loop over each (x, y) in data array for the chart
if y is bigger than the threshold
then call text(x, y, num2str(x))
If you want to label peaks that have consecutive values all above the threshold (like around maybe 115 on your image?) with a single label, you can add some slightly more complicated logic to group those peaks together...if that's what you want, we can help you figure that out.
As mentioned by #Dougal, the text function is what you want. However, there is no need to loop:
%# generate some data
y = poissrnd(5,20,1);
x = 1:20;
%# find where the data is above the threshold
bigIdx = y>6;
%# create a bar plot
bar(x,y)
%# add the text. The alignment setting ensures that the text
%# is directly above the bar. I add 1 here as an y-offset,
%# the ideal value may depend on your data
text(x(bigIdx),y(bigIdx)+1,num2str(x(bigIdx)),'horizontalAlignment','center')
%# you may need to make sure that the y-limit is high enough
%# so that the text is visible
ylim([0 max(y)+2])