4

Is there a way to shift the blue curve to the right by a factor of 10 without creating a new data set but rather using the math operations with the blue plot?

enter image description here

Here is the code.

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgfplotstable}

%\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\fontseries{ul}\selectfont}% used with mathpazo 

\pgfdeclareplotmark{fat-}
{%
    \pgfsetlinewidth{1}
    \pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{0pt}}%
    \pgfpathlineto{\pgfqpoint{-\pgfplotmarksize}{0pt}}%
    \pgfusepathqstroke
}%

\pgfplotsset{/pgfplots/error bars/error bar style={very thick,blue,mark size=2.5},/pgfplots/error bars/error mark={fat-}}
%\pgfplotsset{/pgfplots/label shift={0pt}}

\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    ymode=log,
    enable tick line clipping=false,
    width=9cm,
    height=7cm,
    axis line style=semithick,
    legend style={at={(0.95,0.3)},draw=none},
    legend cell align={right},
    x tick style={black,semithick},
    x label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    xlabel={$x$},
    xmin=1,xmax=1000,
    xtick={1,10,100,1000},
    xtick pos=bottom,
    minor x tick num=9,
    xtick align=outside,
    y tick style={black,semithick},
    y label style=
        {at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel={$y$},
    ymin=0.5,ymax=5,
    ytick={0.1,0.5,1,3,5,10},
    ytick pos=left,
    minor y tick num=9,
    ytick align=outside,
]        
    \addplot [blue,mark=*,mark options={scale=1.5,blue}]
         table [x=x,y=y] {
         x      y
         2      0.8
         10     1
         50     3
    };

    \addplot [red,mark=*,mark options={scale=1.5,red}] table [x=x,y=y] {
        x       y
        20      0.8
        100     1
        500     3
    };

\end{axis}
\end{tikzpicture}
\end{figure}


\end{document}
4

You can save your table once using \pgfplotstableread and reuse it, changing the x expression with x expr:

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgfplotstable}

%\pgfkeys{/pgf/number format/.cd,1000 sep={}}


\newcommand{\myfont}{\fontfamily{cmss}\fontseries{ul}\selectfont}% used with mathpazo 

\pgfdeclareplotmark{fat-}
{%
    \pgfsetlinewidth{1}
    \pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{0pt}}%
    \pgfpathlineto{\pgfqpoint{-\pgfplotmarksize}{0pt}}%
    \pgfusepathqstroke
}%

\pgfplotsset{/pgfplots/error bars/error bar style={very thick,blue,mark size=2.5},/pgfplots/error bars/error mark={fat-}}
%\pgfplotsset{/pgfplots/label shift={0pt}}

\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\Large}}

\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
}

\begin{document}

\begin{figure}    
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xmode=log,
            ymode=log,
            enable tick line clipping=false,
            width=9cm,
            height=7cm,
            axis line style=semithick,
            legend style={at={(0.95,0.3)},draw=none},
            legend cell align={right},
            x tick style={black,semithick},
            x label style= {at={(ticklabel cs:0.5)},anchor=near ticklabel},
            xlabel={$x$},
            xmin=1,xmax=1000,
            xtick={1,10,100,1000},
            xtick pos=bottom,
            minor x tick num=9,
            xtick align=outside,
            y tick style={black,semithick},
            y label style= {at={(ticklabel cs:0.5)},anchor=near ticklabel},
            ylabel={$y$},
            ymin=0.5,ymax=5,
            ytick={0.1,0.5,1,3,5,10},
            ytick pos=left,
            minor y tick num=9,
            ytick align=outside,
        ]        
        \pgfplotstableread{
            x      y
            2      0.8
            10     1
            50     3
        }{\mytable};
        \addplot [blue,mark=*,mark options={scale=1.5,blue}] 
             table [x=x,y=y] {\mytable};
        \addplot [red,mark=*,mark options={scale=1.5,red}] 
             table [x expr=10*\thisrow{x},y=y] {\mytable};

        \end{axis}
    \end{tikzpicture}
\end{figure}


\end{document}
  • Does it work for external data? If I have the same data like table {data.txt} ? – Gagik Feb 19 '19 at 7:12
  • 1
    Of course, put the name of your file instead of the datas in \pgfplotstableread, or directly in the \addplot instead of \mytable. – JPG Feb 19 '19 at 7:15
  • I used the \addplot directly but it didn't shift the curve. Here is what I used \addplot [blue,mark=*,mark options={scale=1.5,blue}] file [x=x,y=y] {Workbook1.txt}; \addplot [red,mark=*,mark options={scale=1.5,red}] file [x expr=10*\thisrow{x},y=y] {Workbook1.txt}; Did I do something wrong? – Gagik Feb 19 '19 at 18:36
  • Keep table instead of file and I think I will be okay. – JPG Feb 19 '19 at 20:47
  • It didn't work. I would be very grateful if you could provide an example. – Gagik Feb 20 '19 at 16:07
0

An addition to the comments to the accepted answer.

In case you do not want to bother with the column name, you can use \thisrowno{<idx>} and, for data that does not change (both coordinates for the first curve, only the y-axis for the second), x index and y index

\begin{tikzpicture}
\begin{axis}[...]

\pgfplotstableread{
    foo    bar
    2      0.8
    10     1
    50     3
}{\mytable};
\addplot [blue,mark=*,mark options={scale=1.5,blue}] 
     table [x index=0,y index=1] {\mytable};
\addplot [red,mark=*,mark options={scale=1.5,red}] 
     table [x expr=10*\thisrowno{0},y index=1] {\mytable};

\end{axis}
\end{tikzpicture}

Having foo and bar or not having them at all gives the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.