# Using PGFPlots to draw lines with semilogyaxis with negative y values

I'm trying to draw lines with log y mode using PGFPlots.

I have a table looking like the following:

0.00097656 -56126
0.0019531 -53585
0.0039062 -33705
0.0078125 -25956
0.015625 -18886
0.03125 -10538
0.0625 -12273
0.125 -8532.3
0.25 -7443.9
0.5 -7443.9
1 -7407.8
2 -5200.3
4 -21963
8 -21963
16 -21963
32 -21963
64 -21963


which has negative y values. When I try to use log y axis, it gives me nothing as the range of y axis is from 10^0 to 10^1. How can I scale the y axis and get the image?

I can use matlab to get the wanted image like the following: • log function is not defined for negative numbers – percusse Oct 5 '15 at 5:44
• yeah, for a number like -56126, we can process it as -5.6126*10^4, which did the trick. Matlab actually handles it this way, but I don't know if pgfplot supports such feature. – wangjingyi Oct 5 '15 at 5:46
• how does it handle? 5.6e-4 is a different thing that log axis. I don't think matlab does anything like that. You can cook up something like that but it is not clear which one you want. – percusse Oct 5 '15 at 5:59
• Thanks for your editing. :) I attached the matlab plot here. It puts negative before the power. – wangjingyi Oct 5 '15 at 6:14
• Or you can use log(|y|) with no hack on the log function – Romain Picot Oct 5 '15 at 6:16

You can do this by first negating the y values to get positive numbers, and then again negate the labels of the y values.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
log basis x=2,
ymin=1e3,
ymax=1e5,
% show the yticklabels' as negative values
log base 10 number format code/.code={
$-10^{\pgfmathprintnumber{#1}}$
},
]
x index=0,
% because negative log values are not defined,
% make them positive
y expr=-\thisrowno{1},
] {
0.00097656 -56126
0.0019531 -53585
0.0039062 -33705
0.0078125 -25956
0.015625 -18886
0.03125 -10538
0.0625 -12273
0.125 -8532.3
0.25 -7443.9
0.5 -7443.9
1 -7407.8
2 -5200.3
4 -21963
8 -21963
16 -21963
32 -21963
64 -21963
};
\end{loglogaxis}
\end{tikzpicture}
\end{document} • Would it be possible to apply this trick if both axes have the same logarithmic base? – aaragon Nov 7 '17 at 13:24
• @aaragon, without testing it I cannot image why this shouldn't work for loglogaxis` as well. Just give it a try. If you cannot make it work, don't hesitate to ask a new question ... Good luck! – Stefan Pinnow Nov 7 '17 at 13:38