Log plots in MATLAB only label the axes at positions 10^x, where x is an integer (e.g., 10^4, 10^5, 10^6). Sometimes, one may want labels at intermediate sites or minor ticks (e.g., 5*10^4, 5*10^5).
In order to place such labels, I have resorted to using the text command with appropriate x and y coordinates. However, the font of the superscript in the text command differs from that in the default axis label. This is true even if the font for axis label and text is set to be identical by the following:
set(0,'DefaultAxesFontName','Helvetica');
set(0,'DefaultTextFontName','Helvetica');
set(0,'DefaultTextFontSize',15);
set(0,'DefaultAxesFontSize',15);
In particular, the superscript font size appears to be smaller in the default axis label compared to the text box. Is there a way to resolve this discrepancy so that the font in the text box and the font in the axis label are identical (including superscripts)?
You can set the x and y axis points like this:
figure
set(gca,'xtick',10.^[0.5:0.5:3])
set(gca,'ytick',10.^[0.5:0.5:3])
gives you steps in 0.5 log 10. There is also an attribute called xticklabel
EDIT: Here's a complete example using arbitrary labels, scientific notation:
semilogx([2:100:10e4],[2:100:10e4])
axis([2 2e4 2 10000])
xticks=10.^[0.5:0.5:10]';
al={};
for i = 1:length(xticks)
tmps=sprintf("%1.1e}",xticks(i));
tmps=strrep(tmps,"e","x10^{"); # replace e with x10^{
tmps=strrep(tmps,"+0",""); # +0 does not add any info
tmps=strrep(tmps,"-0","-"); # -0123 into -123
tmps=strrep(tmps,"+",""); # + does not add any info
al(i)=tmps;
end
set(gca,'xtick',xticks);
set(gca,'xticklabel',al)
Related
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')
I am generating a number of plots in MATLAB. The axis labels and the tick labels are currently too small when included in my final document. I would like to change the font size for both axis labels and tick labels while also setting a new default typeface. I have tried the FontSize and FontName name value pair but strangely see no effect on the exported .eps files; also this method is somewhat impractical because I am generating a large number of plots.
Any suggestions would be greatly appreciated.
You can use findobj to programmatically edit all figures.
For example:
ah = findobj(,'Type','axes'); % get all axes
set(ah,'FontSize',Whatever); %this will change all the tick labels
for m=1:numel(ah) % go over all axes
xlabel_handle = get(ah(m),'xlabel');
set(xlabel_handle,'FontSize',Whatever); % this will change only the label
%repeat for other labels if you wish
end
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.
Is there a way to have a new line in an axis tick label in Matlab to produce a multiline tick label?
The two suggestions from here for other text elements don't seem to work:
set(gca,'xticklabel',{{'line1','line2'}})
fails, and
set(gca,'xticklabel',{['line1' 10 'line2']})
or
set(gca,'xticklabel',{['line1' 13 'line2']})
ignore the newline or carriage return. Any ideas?
I am not sure how long it has been around, but at least in R2015b the axes objects have a 'TickLabelInterpreter' property, which can be specified to set how the tick labels are interpreted. If you choose a LaTeX interpreter, it is possible to have multiline ticklabels quite easily by wrapping them in a tabular environment.
Example:
figure;
plot(rand(10,1));
%// Tick label with arbitrary number of lines in tabular environment
xtl = '\begin{tabular}{c} line 1 \\ line 2 \\ line 3\\ line 4\end{tabular}';
%// use the tick label at location 5 on the x axis
set(gca,'xtick', 5, 'XTickLabel', xtl, 'TickLabelInterpreter', 'latex');
Output:
Of course, the drawback here is that you need to use the LaTeX interpreter which somewhat changes the appearance of the figure. But I believe some people (such as me) actually prefer the way the LaTeX interpreted figure annotations look! As an added bonus, you can use whatever other LaTeX markup you desire (equations, etc.) in the labels.
I suggest you use fix_xticklabels() by Mikhail Erofeev. You can pad your tick labels with space character i.e. " " to adjust the output.
I can get the colorbar ticks as
figure;
hbar=colorbar;
ticks=get(hbar,'ytick');
Now how do I set the tick labels at tick(end) to be ∞?
This is tricky. Normally, for axes labels and titles you can use TeX or LaTeX formatting since they are text objects and thus have an 'Interpreter' property:
xlabel('\infty'); %# Label the x axis with an infinity
However, axes objects themselves don't appear to have a way to use Tex or LaTeX formatting for their tick labels. One solution is to download the submission Format Tick Labels from Alexander Hayes on the MathWorks File Exchange, which will replace the axes tick labels with formatted text objects.
Another solution is to change the 'FontName' property of the axes to the 'Symbol' font, the 165th character of which is the infinity symbol. Here's an example:
hBar = colorbar; %# Create the colorbar
labels = cellstr(get(hBar,'YTickLabel')); %# Get the current y-axis tick labels
labels{end} = char(165); %# Change the last tick label
set(hBar,'FontName','Symbol',... %# Change the colorbar axes font
'YTickLabel',labels); %# and update the tick labels
And here's what the colorbar will look like: