matlab2tikz library question

I am trying to use high quality matlab figures in tikz format. Currently I have added customized labels as seen in Matlab figure below:
Below is the matlab code
x=[ 512*512 1024*1024 2048*2048 3072*3072 4096*4096 5120*5120];
y1=[27.66 29.82 30.41 30.43 30.55 30.6]
y2=[28.93 33.30 36.13 36.48 37.50 38.28];
y3=[29.28 34.49 39.23 40.17 43.01 46.80];
figure
set(gca,'xTick', [ 512*512 1024*1024, 2048*2048, 3072*3072, 4096*4096, 5120*5120]);
set(gca,'xTickLabel', {'512x512', '1024x1024', '2048x2048', '3072x3072', '4096x4096', '5120x5120'});
set(gca,'XTickLabelRotation',45)
hold on
plot(x,y1);
hold on
plot(x,y2);
hold on
plot(x,y3)
hold on
However, I export it to .tikz file using matlab2tikz library and use it in latex. I am facing three issues that are :
  1. The labels '512x512', '1024x1024' are shifted forwards as seen in below snapshot taken from latex, hence it points to a wrong value
  2. The above labels overlap with 'Atlas image resolution' label of x axis.
  3. The value 10 to the power of 7 appear from nowhere in latex. I checked the tikz file it does not have 10 to the power 7 value.
Please help me with this. I have attached the tikz file content into a text file.
ga59siv @ matlab . rbg . tum . de

Answers (1)

The ‘10^7’ is coming from the way you define ‘x’. The actual values of ‘x’ are:
x =
262.1440e+003 1.0486e+006 4.1943e+006 9.4372e+006 16.7772e+006 26.2144e+006
For the x-label, experiment with different values for 'FontSize', for example:
set(gca,'XTickLabelRotation',45, 'FontSize',8)

4 Comments

The labels are still shifted, now with small fonts....
Actually, they’re not shifted. They’re aligned by the centres of the labels. If you want them aligned by the right border of the object, you have to use the text function.
The Code:
x=[ 512*512 1024*1024 2048*2048 3072*3072 4096*4096 5120*5120]
y1=[27.66 29.82 30.41 30.43 30.55 30.6];
y2=[28.93 33.30 36.13 36.48 37.50 38.28];
y3=[29.28 34.49 39.23 40.17 43.01 46.80];
figure
hold on
semilogx(x,y1);
hold on
plot(x,y2);
plot(x,y3)
hold off
set(gca, 'XTickLabel',[])
set(gca,'xTick', [ 512*512 1024*1024, 2048*2048, 3072*3072, 4096*4096, 5120*5120]);
xloc = [ 512*512 1024*1024, 2048*2048, 3072*3072, 4096*4096, 5120*5120];
text(xloc, min(ylim)-0.5*ones(size(xloc)), {'512x512', '1024x1024', '2048x2048', '3072x3072', '4096x4096', '5120x5120'}, 'Rotation',45, 'HorizontalAlignment','right', 'FontSize',8)
Experiment with that to get the exact result you want.
yes, your code is working. But this customized labels still overlap with x-axis label. In matlab figure I see no overlap. But the moment I convert it to tikz format and use in latex the x-axis label comes back to its original position. I use below code to move x-axis label 'Atlas Image Resolution' downwards. What should I modify here, why it's reverting back?
xlabel('Atlas Image Resolution');
xlabh = get(gca,'XLabel');
set(xlabh,'Position',get(xlabh,'Position') - [0 2.5 0])
EDIT (28 Dec 2016 00:25 UCT)
With a bit of help from MathWorks Tech Support (and some tweaking of the solution the provided in order to get it to work correctly), I can provide code that now works.
The Code:
x=[ 512*512 1024*1024 2048*2048 3072*3072 4096*4096 5120*5120];
y1=[27.66 29.82 30.41 30.43 30.55 30.6];
y2=[28.93 33.30 36.13 36.48 37.50 38.28];
y3=[29.28 34.49 39.23 40.17 43.01 46.80];
figure
hold on
semilogx(x,y1);
hold on
plot(x,y2);
plot(x,y3)
hold off
set(gca, 'XTickLabel',[])
set(gca,'xTick', [ 512*512 1024*1024, 2048*2048, 3072*3072, 4096*4096, 5120*5120]);
xloc = [ 512*512 1024*1024, 2048*2048, 3072*3072, 4096*4096, 5120*5120];
text(xloc, min(ylim)-0.5*ones(size(xloc)), {'512x512', '1024x1024', '2048x2048', '3072x3072', '4096x4096', '5120x5120'}, 'Rotation',45, 'HorizontalAlignment','right', 'FontSize',8)
xlabel('Atlas Image Resolution')
opos = get(gca, 'OuterPosition');
set(gca, 'OuterPosition', [0 0.15 1 0.85], 'Units','pixel')
xlbl = get(gca, 'XLabel');
set(xlbl,'Position',[15E+6 20 0], 'String','Atlas Image Resolution')
My error was in believing this was figure property rather than an axis property. Experiment with it to get the result you want.
The Plot:

Sign in to comment.

Asked:

on 26 Dec 2016

Edited:

on 29 Dec 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!