5

I am currently learning TikZ and PgfPlots to create diagrams and I was wondering if it is possible to apply custom scaling to individual axis. For example take this markup:

\begin{tikzpicture}
    \begin{axis}[
        width=\textwidth,
        domain=128:4096,
        xmin=128, xmax=4096,
        xtick={256,512,1024,2048,4096},
        ymin=4, ymax=4096,
        ytick={64,256,1024,4096},
        grid]
        \addplot[mark=x] coordinates {
            (128, 4)
            (256, 16)
            (512, 64)
            (1024, 256)
            (2048, 1024)
            (4096, 4096)
        };
    \end{axis}
\end{tikzpicture}

This generates this beautiful diagram:

quadratic function

So my question is, if it is possible to scale the x-axis in a way, that the space between individual ticks is equally distributed, using plain TikZ or PgfPlots. I tried to look for something adequate within the respective manuals of both packages, but did not find anything that fits my need.

So does anybody know if this is possible?

Thanks in advance.

4
  • 1
    There is a way in \datavisualization, custom axis scaling, section 77.2.3; pg 781 in the manual describes how to change the x axis to a square-root scaling. You probably won't be able to copy the code directly but it may give you a hint on how to start. Sadly, it is not well-documented. Manual: ctan.org/pkg/pgf
    – Huang_d
    Jun 27, 2017 at 13:31
  • 1
    That is exponential, not quadratic. Jun 27, 2017 at 15:34
  • I suggest the a change in the title after @BenjaminMcKay comment. Quadratic plot is something different and searches misleads to this question.
    – alfC
    Jan 10, 2018 at 22:00
  • @alfC Done... and thanks for the clearification!
    – Carsten
    Jan 12, 2018 at 14:13

2 Answers 2

3

A possibility might be to plot at equally spaced values, and replace the ticklabels instead. For example:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
% read data into a table
\pgfplotstableread{
128 4
256 16
512 64
1024 256
2048 1024
4096 4096
}\datatable
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=\textwidth,
        % have xticks only on datapoints
        xtick=data,
        % get ticklabels from the first datacolumn (counting starts at zero)
        xticklabels from table={\datatable}{[index]0},
        ymin=4, ymax=4096,
        ytick={64,256,1024,4096},
        grid
]
        \addplot[mark=x] table[
                            % use the coordinate index, i.e. 0,1,... as x-value
                            x expr=\coordindex,
                            % and use the second column of the table as the y-value
                            y index=1] {\datatable};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks, that's what I was looking for! The MWE you provided works fine, however I was not able to use this directly within my document, since "TeX apparently screwed up my end-of-lines". I corrected this by setting the row and column seperator: \pgfplotstableread[col sep=&,row sep=\\]{ 128 & 4 \\ ... }\datatable, in case anybody else runs into this problem.
    – Carsten
    Jun 27, 2017 at 16:29
3

While Torbjørn T. answer would show equally spaced ticks regardless of the numbers given in the data table, here I present a solution that shows equally spaced ticks in a logarithmic plot with the basis 2. That means that you can also add numbers in between the log2 numbers and still the ticks won't be "disturbed" (see the added coordinate).

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    % use a logarithmic x axis
    \begin{semilogxaxis}[
        width=\textwidth,
        ymin=4,
        ymax=4096,
        % ---------------------------------------------------------------------
        % set the log basis to $2$ ...
        log basis x=2,
        % ... and change the format to show fixed numbers
        log number format basis/.code 2 args={
            \pgfmathparse{2^\tick}
            \pgfmathprintnumber[fixed]{\pgfmathresult}
        },
        % ---------------------------------------------------------------------
        ytick={64,256,1024,4096},
        grid,
    ]
        \addplot [mark=x] coordinates {
            (128, 4)
            (172, 8)    % <-- added coordinate
            (256, 16)
            (512, 64)
            (1024, 256)
            (2048, 1024)
            (4096, 4096)
        };
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .