Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
    \documentclass[a4paper,12pt]{article}
    \usepackage{pgfplots,pgfplotstable}        % Grafici
    \pgfplotsset{/pgf/number format/use comma,compat=newest}



    %*********************************************************************************
    % Impostazioni Pgfplots
    %*********************************************************************************

    \pgfplotstableread{
    X Y
    0.05 0.17
    0.05 0.041
    0.05 0.023
    0.1 0.332
    0.1 0.089
    0.1 0.041
    0.15 0.5
    0.15 0.132
    0.15 0.06
    }\datatable

    \pgfplotstablecreatecol[linear regression]
    {regression}
    {\datatable}

    \xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
    \xdef\intercept{\pgfplotstableregressionb}



    \begin{document}
    \begin{figure}
    \centering
    \pgfplotsset{width=10cm}
    \begin{tikzpicture}
    \makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
    \pgfplotsset{axis line on top/.style={
     axis on top=false,
     after end axis/.append code={
        \pgfplotsset{axis line style=opaque,
          ticklabel style=opaque,
          tick style=opaque,
          grid=none}
        \pgfplotsdrawaxis}
      }
    }
    \begin{axis}[/pgf/number format/fixed,
    legend pos=outer north east,
    grid=major,
    xmin=0, xmax=0.2,
    ymin=0, ymax=0.6,
    point meta min={0},
    point meta max={0.2},
    yticklabel shift=2pt,
    xticklabel shift=2pt,
    legend pos=north west,
    minor tick num=4,
    axis line on top,
    xticklabel style={text height=1.5ex},
    xtick={0,0.05,...,0.3},
    minor xtick={0,0.01,...,0.2},
    extra x ticks={0,0.05,...,0.3},
    extra x tick style={
        xticklabel pos=right,
        xticklabel style={text depth=0pt}
    },
    extra y ticks={0,0.1,...,0.7},
    extra y tick style={
        yticklabel pos=right
    },
    tick style={thin,black},
    xlabel=\large $m$,
    ylabel=\large $\Delta l$,
    colorbar horizontal,
    colorbar style={
        /pgf/number format/fixed,
        xticklabel shift=2pt,
        xtick={0,0.05,...,0.3},
        xticklabel style={text height=1.5ex}
    },
    colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
    after end axis/.append code={
        \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
    },
    legend cell align=left]
    \addplot [point meta=explicit,
        scatter,
        mark=*,
        draw=none]
        table[meta=X] {\datatable};

    \addplot [mesh,point meta=x,domain=0.025:0.175,very thick,samples=1000] {\slope*x+\intercept};
    \addlegendentry{%
    $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
    \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
    \end{axis}
    \end{tikzpicture}
    \end{figure}
    \end{document}

hi i would to make two plot like this with two different tables of data in the same document how can i do?

Thanks

share|improve this question
2  
You don't have to use the \datatable macro to save your tables. You can choose any macro name you want (within the limits of LaTeX names), so you could for example store your first table into \mytableA and your second table into \mytableB. Is that what you mean? –  Jake May 31 '12 at 21:18
    
Thanks :) ! check the code below, is ok? –  FormlessCloud May 31 '12 at 21:29
1  
Yup, looks good! –  Jake May 31 '12 at 21:30
add comment

1 Answer

up vote 4 down vote accepted
\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots,pgfplotstable}        % Grafici
\pgfplotsset{/pgf/number format/use comma,compat=newest}


%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableA

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableA}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}


%*********************************************************************************
% Impostazioni Pgfplots
%*********************************************************************************

\pgfplotstableread{
X Y
0.05 0.17
0.05 0.041
0.05 0.023
0.1 0.332
0.1 0.089
0.1 0.041
0.15 0.5
0.15 0.132
0.15 0.06
}\tableB

\pgfplotstablecreatecol[linear regression]
{regression}
{\tableB}

\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\xdef\intercept{\pgfplotstableregressionb}

\begin{document}
\begin{figure}
\centering
\pgfplotsset{width=10cm}
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
 axis on top=false,
 after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
minor tick num=4,
axis line on top,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={0,0.01,...,0.2},
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
after end axis/.append code={
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
},
legend cell align=left]
\addplot [point meta=explicit,
    scatter,
    mark=*,
    draw=none]
    table[meta=X] {\tableA};

\addplot [mesh,point meta=x,domain=0.025:0.175,very thick,samples=10] {\slope*x+\intercept};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{figure}



\begin{figure}
\centering
\pgfplotsset{width=10cm}
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
 axis on top=false,
 after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
minor tick num=4,
axis line on top,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
minor xtick={0,0.01,...,0.2},
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={0,0.05,...,0.3},
    xticklabel style={text height=1.5ex}
},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
after end axis/.append code={
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
},
legend cell align=left]
\addplot [point meta=explicit,
    scatter,
    mark=*,
    draw=none]
    table[meta=X] {\tableB};

\addplot [mesh,point meta=x,domain=0.025:0.175,very thick,samples=10] {\slope*x+\intercept};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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