matlab2tikz / matlab2tikz

This program converts MATLAB®/Octave figures to TikZ/pgfplots figures for smooth integration into LaTeX.
http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
BSD 2-Clause "Simplified" License
1.56k stars 320 forks source link

Position of annotation in loglog plots #986

Open helicities opened 7 years ago

helicities commented 7 years ago

The coordinates of annotations (like text) are incorrectly truncated in log-log plots. Let's consider the following example with two annotations A and B:

x=linspace(1,1e3,2);
y=1./x.^3;

figure;
loglog(x,y)
text(1,1e-6,'$A$')
text(1e2,1e-2,'$B$')
xlim([1e-1 1e4])
ylim([1e-10 1e1])

The produced tikz-code (only for the text part) reads:

\node[right, align=left]
at (axis cs:1,0) {$A$};
\node[right, align=left]
at (axis cs:100,0.01) {$B$};

where the y-coordinate of A has been set to 0 (instead of 1e-6), while the coordinate of B was interpreted correctly (1e-2). This results of course in a wrong placement of the annotation in the final figure. The rounding to zero should only be performed in lin-lin plots but not in loglog plots.

egeerardyn commented 7 years ago

Thank you for reporting this bug. The truncation is indeed not the right thing to do with log-scaled axes.

Code-wise, I think this boils down to adding 2 options to formatDim to indicate whether the environment we're in is log-scaled and in that case don't apply truncation.

jankap commented 5 years ago

Any updates on this? I'm encountering the same error here. Thanks.

jankap commented 5 years ago

https://github.com/matlab2tikz/matlab2tikz/blob/62a038d9833d4d48581acb630da550e40799338f/src/matlab2tikz.m#L6708 could be changed to a different truncation instead of 3 decimal places for log plots. I'll try to make a PR tomorrow.

ronaldremmerswaal commented 4 years ago

It seems this is not yet fixed. Why not use str = sprintf('%.3e', value); to keep the three significant digits? Also rounding with the tolerance, as is done here https://github.com/matlab2tikz/matlab2tikz/blob/62a038d9833d4d48581acb630da550e40799338f/src/matlab2tikz.m#L6700 results in problems since round(1E-8 / 1E-7)*1E-7 = 0.