As already mentioned in the comment to the question -- in PGFPlots v1.13 the key axis line shift
has been introduced which allows to easily produce Tufte like axis (have a look at section 4.9.10 on page 255 in the manual). However, v1.13 does not take into account the axis labels, etc. when shifting the axis. This has been reported on the PGFPlots bug tracker and has been fixed in v1.14 released on 2016-08-10.
Said that it remains the problem of adding the xmin
and xmax
values as labels to the axis. I think at present there is no other way then setting the xmin
and xmax
values as options by hand to the axis and then you can add them as xticks
or extra x ticks
using the \pgfkeysvalueof
command. This is supported by the answers given here.
(A bit strange though is, that when given xmin=9e-4
the value stored is -7.01
as can be seen by the added gray node. Jake has pointed out in the comment that this is because the numbers are always stored in natural log, so exp(-7.01)=0.0009
. This explains why the number is not shown as 10^(-3.05)=0.0009
.)
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% use Tufte like axis
axis line shift=10pt,
% because of a bug in v1.13 one has to shift the labels, etc.
% by hand. This is not necessary in v1.14!
xlabel style={
yshift=-10pt,
},
ylabel style={
yshift=10pt,
},
%
% to set some extra ticks at the beginning and the end of the
% axis you have to give the values explicitly
xmin=9e-4,
xmax=4e-3,
% now you can add them as extra ticks
extra x ticks={
\pgfkeysvalueof{/pgfplots/xmin},
\pgfkeysvalueof{/pgfplots/xmax}
},
%
% % if you want to change the tick label style to `i \cdot 10^j'
% % (where `i' has to be an integer, e.g. $2 \cdot 10^{-3}$)
% xticklabel style={
% log identify minor tick positions=true,
% },
%
xlabel={$L$ [H]},
ylabel={$\hat{I}_{DM}$ [A]},
axis lines*=left,
%
ymin=0.41,
ytick={0.7,0.9,...,1.5},
xtick={1.5e-3,2e-3,2.5e-3},
%
xmode=log,
]
\addplot +[black, mark options=fill=black] coordinates {(948e-6,1.61981) (1.5e-3,1.02377) (2e-3,0.769047) (2.5e-3,0.614994) (3.2e-3,0.503511)};
% for debugging show the value of `xmin'
% (--> why does it show a value of -7.01?)
\node [fill=black!25,anchor=south west] at (rel axis cs:0,0)
{xmin = \pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmin}}};
\end{axis}
\end{tikzpicture}
\end{document}
Solution with extracting the min and max values from a table
To "extract" the min and max value of the data you could put them into a table and then using the pgfplotstable
package to sort and extract the data.
For more details have a look at the comments in the code.
% first put the data to an external file or a table,
% so it can be handled by the `pgfplotstable' package
\begin{filecontents}{data.txt}
948e-6 1.61981
1.5e-3 1.02377
2e-3 0.769047
2.5e-3 0.614994
3.2e-3 0.503511
\end{filecontents}
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{
fpu,
positioning, % <-- for debugging nodes only
}
\pgfplotsset{
compat=1.13,
}
\begin{document}
\begin{tikzpicture}
% to be sure that no numbers are interpreted as zero, activate `fpu'
% library
\tikzset{
fpu=true,
}
%%% find `xmin' and `xmax'
% for that first sort the table after the x values
\pgfplotstablesort{\SortedTable}{data.txt}
\tikzset{
fpu=false,
}
% then one can directly extract `xmin'
\pgfplotstablegetelem{0}{[index] 0}\of\SortedTable
\let\xmin=\pgfplotsretval
%% now extract `xmax'
% for that we first need to extract the number of rows in the table
\pgfplotstablegetrowsof{\SortedTable}
\pgfmathsetmacro{\NoOfRows}{\pgfplotsretval-1}
% then we can extract the last entry in the sorted table
\pgfplotstablegetelem{\NoOfRows}{[index] 0}\of\SortedTable
\let\xmax=\pgfplotsretval
\begin{axis}[
% use Tufte like axis
axis line shift=10pt,
% because of a bug in v1.13 one has to shift the labels, etc.
% by hand
xlabel style={
yshift=-10pt,
},
ylabel style={
yshift=10pt,
},
%
% add the extra ticks from the extracted data
extra x ticks={
\xmin,
\xmax
},
% just to make sure these are the extra ticks, draw them red
extra x tick style={
text=red,
},
%
% % if you want to change the tick label style to `i \cdot 10^j'
% % (where `i' has to be an integer, e.g. $2 \cdot 10^{-3}$)
% xticklabel style={
% log identify minor tick positions=true,
% },
%
xlabel={$L$ [H]},
ylabel={$\hat{I}_{DM}$ [A]},
axis lines*=left,
%
ymin=0.41,
ytick={0.7,0.9,...,1.5},
xtick={1.5e-3,2e-3,2.5e-3},
%
xmode=log,
]
\addplot +[black, mark options=fill=black] table {data.txt};
% for debugging show the value of `xmax'
\node [fill=black!25,anchor=south west] (a) at (rel axis cs:0,0)
{xmax = \pgfmathprintnumber{\xmax}};
\node [fill=black!25,above=1pt of a.north west,anchor=south west]
{Rows = \pgfmathprintnumber{\NoOfRows}};
\end{axis}
\end{tikzpicture}
\end{document}
axis line shift
key introduced in PGFPlots v1.13? I think this works fine also for logarithmic axis.tufte extra ticks
to work and I have to see how to shift the labels together with the axis.extra ticks
. Could you explain this in more detail, please.