I'm using pgfplots
to auto-plot a number of datasets. The x-axis is in this case not to be enlarged. I'm using enlarge y limits, to get a clearer view of the data, but I would like it to scale to the nearest tick instead of a predefined percentage or value, without looking at the data manually first and using ymin
,ymax
.
This is what I have currently with the sample code
And this is the desired plot (made with manually setting ymin
and ymax
)
Thanks in advance.
Here is the sample code
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[compat=newest,enlarge x limits=false,
title=plot example,
xlabel=x-axis,
ylabel=y-axis,
legend columns=-1,
major grid style={black},
minor tick num=1,
grid=both,
legend style={thick},
enlarge y limits]
\addplot[red,ultra thick] table[x=input,y=output]%
{input output
108 14.56
110 16.25
112 17.95
114 19.68
116 21.43
118 22.99
120 23.66
122 24.3
124 24.92
126 25.56
128 26.18
130 26.8
132 27.41
};
\addlegendentry{data}
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
It would be really cool to modify the routine for plot range calculations and make the results snap to steps of a given width. I tried reading some of the
pgfplots
source to hijack it but it got too complicated, too quickly. That's beyond me, and I'll leave that endeavour to someone morepgfplots
-savvy.Here I take the external route instead: using
pgfplotstable
I find the extrema of a dataset and use them pre-visualization to do the calculations to set the right keys.Main disadvantage: what I wrote relies on the data range of a single dataset. You seem to be plotting just one at a time, so that's not a problem. If one were to plot many, using their union for the calculation would be a reasonable workaround.
Main advantage: the snapping procedure I define is not limited to the plot range. In fact the first parameter of
snap hi
andsnap lo
could be any key accepting an assigment. Maybe there are some neat uses for that.Here are the code and the predictable result: