TransWikia.com

pgfplots: change axis scaling from linear to log within plot

TeX - LaTeX Asked on August 13, 2020

I would like to plot data with pgfplots that has two interessting x-ranges:

  1. 0 <= x <= 300
  2. limit for x -> inf (or some defined upper boundary)

So, to show both ranges appropriately, my idea is to have a single plot which has a linear scale from x = 0 up to x = 300 and a logarithmic scale for the x-axis above.
Additionally, since the first range (linear scale) is more important than the second one (logarithmic scale), it should have a greater width. Let the width-ratio be 3:1.

Here a MWE with a sample of the data to plot. It produces two plots, one with linear scale and another one with logarithmic scale:

documentclass[crop, tikz]{standalone}
usepackage{pgfplots}
usepackage{filecontents}

begin{filecontents*}{sample.csv}
    1,      2.42
    100,    2.54
    200,    2.66
    300,    2.75
    400,    2.81
    1000,   2.94
    2000,   2.97
    3000,   2.98
    5000,   2.99
    10000,  3.00
end{filecontents*}

begin{document}
begin{tikzpicture}
    begin{axis}[
    xlabel=x,
    ylabel=y,
    xmin = 0,
    xmax = 10000,
    grid = both],
    addplot[line width=1pt,solid,color=cyan, solid] table[col sep=comma]{sample.csv};
    end{axis}
end{tikzpicture}
begin{tikzpicture}
    begin{semilogxaxis}[
    xlabel=x,
    ylabel=y,
    xmin = 0,
    xmax = 10000,
    grid = both],
    addplot[line width=1pt,solid,color=cyan, solid] table[col sep=comma]{sample.csv};
    end{semilogxaxis}
end{tikzpicture}
end{document}

MWE output

One Answer

It feels a bit nasty but with groupplots two plots can be aligned side-by-side with different scales. Using the option horizontal sep = 0 while taking care for the y-axis with axis y line* = left / axis y line* = right leads to a smooth transition to the second plot.

documentclass[crop, tikz]{standalone}
usepackage{pgfplots}
usepackage{filecontents}
usepgfplotslibrary{groupplots}

begin{filecontents*}{sample.csv}
    1,      2.42
    100,    2.54
    200,    2.66
    300,    2.75
    400,    2.81
    1000,   2.94
    2000,   2.97
    3000,   2.98
    5000,   2.99
    10000,  3.00
end{filecontents*}

begin{document}
begin{tikzpicture}
    begin{groupplot}[group style = {
        group size = 2 by 1,
        horizontal sep = 0},
        width = linewidth,
        tick label style={font=footnotesize},
        typeset ticklabels with strut,
        enlarge x limits=false]
    nextgroupplot[
        xmin = 0, xmax = 300,
        ymin = 2.4, ymax = 3.1,
        width = 0.75linewidth, height = 0.75linewidth,
        axis y line* = left,
        grid = both]
        addplot[line width = 1pt, solid, color = cyan] table[col sep = comma] {sample.csv};
    nextgroupplot[
        xmin = 300, xmax = 10000,
        ymin = 2.4, ymax = 3.1,
        width = 0.25linewidth, height = 0.75linewidth,
        xmode = log,
        axis y line* = right,
        grid = both,
        xtick = {1000, 10000}, yticklabels = {,,}]
        addplot[line width = 1pt, solid, color = cyan] table[col sep = comma] {sample.csv};
    end{groupplot}
end{tikzpicture}
end{document}

enter image description here

Answered by jonas on August 13, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP