I am tinkering with a graph. By third parties I am required to use pgfplots to do so.
(So the real reason for my problems in this matter can be seen in my own social skills: I let myself get into a situation that should be avoided: Getting involved in putting something together quickly and well without having profound knowledge of the tools to be used to do it. That one needs some time to get familiar with TikZ and pgfplots can't be blamed on the developers.
I am grateful for the helpful answers and thankful for the kindness and straightforwardness with which I was met by those who helped me.)
So far I have pieced together the code below.
I would like to optimize the illustration that I have so far.
After I have been working on it for more than two days now—most of the time searching the correct piece of information on usage of pgfplots—I still have the following questions:
- How do I get the (thin, lighter) grid lines of the "minor grid" to be below the thicker/darker grid lines of the "major grid" instead of above them? Also for the extra x ticks and the extra y ticks, where not only horizontal but also vertical lines of the minor grid are above the lines of the major grid?
- How to have ticks in the same color as the axes?
- How to extract data about linestyle/color etc of ticks so that the data can be used outside the axes-environment as well?
- Is there an easier way to label the coordinate origin as in the figure, which also works within the axis environment?
- How to have graphs not stick into the axes?
\documentclass[tikz,border=5mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} % <- Without this strangely the axis label is not placed % correctly; it took four hours to figure this out. \begin{document} \begin{tikzpicture}[x=1cm, y=1cm] % Why are there no pgf-keys whose values could be retrieved for having the length of % the x- and the y-unit-vector. \path(1,1);\pgfgetlastxy{\Ax}{\Ay}% \begin{axis}[% x=\Ax, y=\Ay, axis y line = left, axis x line = bottom, xtick pos=bottom, ytick pos=left, y axis line style = {->}, x axis line style = {->}, xtick distance = 1, ytick distance = 1, tick align=outside, minor tick style={draw=none}, xlabel style={at={(rel axis cs:1,0)}}, xlabel = {\(x\)}, ylabel style={at={(rel axis cs:0,1)},rotate=-90}, ylabel = {\(y\)}, grid=both, minor grid style={very thin, lightgray!25}, minor tick num = 9, major grid style={thin}, xmin = 0, xmax = 15.5, ymin = 0, ymax = 3.5, xtick = {1,...,15.5}, ytick = {1,...,3.5}, extra x ticks={0,1}, extra x tick labels={}, extra x tick style={grid=minor, major tick length=0pt}, extra y ticks={0,1}, extra y tick labels={}, extra y tick style={grid=minor, major tick length=0pt}, ]% % It seems drawing outside the area specified by xmin/xmax/ymin/ymax % is not (easily) possible from inside the axis environment, so let's % calculate the components of the vector of the origin tick now and % save them to macros for usage outside the axis environment. % Probably the line-style of major ticks should be saved for usage % outside also but I don't want to spend a week on finding out how % to do so. %\pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/x}}% %\global\let\scratchx=\pgfmathresult %\pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/y}}% %\global\let\scratchy=\pgfmathresult \pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/x}/sqrt(2)}% \global\let\scratchx=\pgfmathresult \pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/y}/sqrt(2)}% \global\let\scratchy=\pgfmathresult % \addplot[ domain = 0:15.5,% samples = 2,% <- I suppose for a straight line more samples are not needed smooth, thick, blue ]{x/5} node[above,pos=0.5] {\vbox{\hbox{\(f(x)=\frac{x}{5}\)}\kern.1\dimexpr\Ay\relax}}; \end{axis} % Draw the origin: %\draw[very thin] (0,0)--(\scratchx,\scratchy) node [anchor=north east]{\(\mathrm{O}\)}; \draw[very thin] (0,0)--(\scratchx,\scratchy); \coordinate (\scratchx,\scratchy) node [anchor=north east]{\(\mathrm{O}\)}; \end{tikzpicture} \end{document} The entire graph:
Enlargement of the bottom left corner so one can see what my questions are about:
Thanks to dexteritas and Torbjørn T., I have now put together something that works within the axis environment, which makes question 3 obsolete and also solves all other problems:
Here is the code:
\documentclass[tikz,border=5mm]{standalone} \usepackage{pgfplots} \usetikzlibrary{arrows} \pgfplotsset{compat = newest} % <- Without this the axis label is not placed correctly. \begin{document} \begin{tikzpicture}[x=1cm, y=1cm] % Why are there no pgf-keys whose values could be retrieved for having the length of % the x- and the y-unit-vector? \path(1,1);\pgfgetlastxy{\Ax}{\Ay}% \begin{axis}[% % % General settings: % % - Make x and y the same as that of the surrounding tikzpicture environment: % x=\Ax, y=\Ay, % % - When the end of the axis environment is reached redraw the axes to ensure % they are not overlapped by a graph or by components of the grid: % after end axis/.append code={ \pgfplotsset{ axis line style=opaque, ticklabel style=opaque, tick style=opaque, grid=none } \csname pgfplots@draw@axis\endcsname }, % % - Make it possible to draw outside the area denoted by xmin/xmax/ymin/ymax: % clip mode=individual, % % Appearance of the axes: % axis y line = left, axis x line = bottom, y axis line style = {->, >=stealth'}, x axis line style = {->, >=stealth'}, xlabel style={at={(rel axis cs:1,0)}}, xlabel = {\(x\)}, ylabel style={at={(rel axis cs:0,1)},rotate=-90}, ylabel = {\(y\)}, % % - range of the axes: % xmin = 0, xmax = 15.5, ymin = 0, ymax = 3.5, % % Appearance of the ticks on the axes and the tick labels: % xtick pos=bottom, ytick pos=left, xtick distance = 1, ytick distance = 1, tick align=outside, major tick style={thin, black}, minor tick style={very thin, black}, minor tick num = 1, % % As the origin needs extra treatment labels of major ticks shall begin with 1: % xtick = {1,...,15}, ytick = {1,...,3}, % % Draw extra minor ticks at x=0.5 and y=0.5; the pgfplots-manual says that % all extra ticks are considered major ticks, so use extra x tick style/ % extra y tick style for assigning major ticks the parameters of minor ticks % and then draw them without labels: % extra x tick style={major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, major tick style={very thin, black}}, extra y tick style={major tick length=\pgfkeysvalueof{/pgfplots/minor tick length}, major tick style={very thin, black}}, extra x ticks={0.5}, extra y ticks={0.5}, extra x tick labels={}, extra y tick labels={}, grid=none, ]% % % Draw the grids: % \draw[xstep=0.1,ystep=0.1,lightgray!25,ultra thin] (0,0) grid (15.5,3.5); \draw[xstep=0.5,ystep=0.5,lightgray!66,very thin] (0,0) grid (15.5,3.5); \draw[xstep=1,ystep=1,lightgray,thin] (0,0) grid (15.5,3.5); % % Draw the plot(s): % % - The slope of the following plot is 1/5. So let's rotate the node atan(1/5). \pgfmathparse{atan(1/5)}\let\scratch\pgfmathresult \addplot[ domain = 0:15.5,% samples = 2,% <- I suppose for a straight line more samples are not needed smooth, thick, blue ]{x/5} node[above,pos=0.5,rotate={\scratch}] {\footnotesize\(f(x)=\frac{x}{5}\)}; % % Draw the origin: % %\pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/x}}% %\let\scratchx=\pgfmathresult %\pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/y}}% %\let\scratchy=\pgfmathresult %\draw[thin] (0,0)--(\scratchx,\scratchy) node [anchor=north east]{\(\mathrm{O}\)}; \pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/x}/sqrt(2)}% \let\scratchx=\pgfmathresult \pgfmathparse{-\pgfkeysvalueof{/pgfplots/major tick length}/\pgfkeysvalueof{/pgfplots/y}/sqrt(2)}% \let\scratchy=\pgfmathresult \draw[thin] (0,0)--(\scratchx,\scratchy); \coordinate (\scratchx,\scratchy) node [anchor=north east]{\(\mathrm{O}\)}; % \end{axis} \end{tikzpicture} \end{document} Here is the entire plot:
Here the enlargement:





clip=false. Alternatively,clip mode=individualwill clip plot lines, but not\node,\drawetc. Regarding label placement,compat=1.3or higher will improve the positioning. It is actually mentioned in the manual in the section aboutx/y/zlabelbut I won't necessary disagree about it being a bit obscure.\pgfplotsset{every tick/.append style={color=black}}.