3

I want to fit this data with a straight line and both the straight line that the points have to follow the color of the colorbar horizontal. Another problem is that I can not adapt the grid scale..

   \documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\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}
  }
}
\pgfdeclarehorizontalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.4,
point meta min={0},
point meta max={0.2},
axis line on top,
xticklabel style={text height=1.5ex},
xticklabels={%
$0$,
$0.05$,
$0.1$,
$0.15$,
$0.2$,
$0.25$,
$0.3$
},
extra x ticks={0,0.05,...,0.3},
extra x tick labels={%
$0$,
$0.05$,
$0.1$,
$0.15$,
$0.2$,
$0.25$,
$0.3$
},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.2,...,1},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
minor tick num=3,
xlabel=\large $\mbox{s}$,
ylabel=\large $f_{\mu,\sigma^2}(\mbox{s})$,
colorbar horizontal,
colorbar style={
    xticklabels={%
        $-0.4$,
        $-0.3$,
        $-0.2$,
        $-0.1$,
        $\mu$,
        $0.1$,
        $0.2$,
        $0.3$,
        $0.4$
    },
    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 [
    mark=*,
    draw=none]
    table[row sep=\\] {% plot X versus Y. This is original data.
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.05\\
0.15 0.132\\
0.15 0.06\\
};
\addplot [no marks] table[row sep=\\,
y={create col/linear regression={y=Y}}] % compute a linear regression from the input table
{
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.05\\
0.15 0.132\\
0.15 0.06\\
};
%\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

I would like a color of the line like this

i possible have this legend? enter image description here

David Carlisle
  • 757,742
Aurelius
  • 7,653
  • 10
  • 49
  • 103
  • 1
    Minimal working example please. Also, to make the code appear as it should, indent with four spaces. Aternatively, select the code and click on the {} button in the editor. – qubyte May 17 '12 at 19:45
  • As Mark said, you should post a complete compilable minimal document (starting from \documentclass) here. I can't get your code to compile without removing the scatter src line. Does it actually compile for you? Also, you should try to simplify your example (remove the axis line on top code, for instance) to make it easier to solve the actual problem. – Jake May 17 '12 at 23:42
  • hi guys i'm sorry, i have edited my answer ! extra x ticks={0,0.05,...,0.3} i need this command also for the normal xticks ! – Aurelius May 18 '12 at 09:46
  • I would like the color of the line and the points follow the colors of the colorbar for the values ​​of 'x-axis. I also want to adjust the values ​​of all axes using this command "extra x ticks = {0,0.05, ..., 0.3}" thanks – Aurelius May 18 '12 at 19:15
  • As for the font have a look at this question: What package allows Elvish in TeX? – cgnieder May 18 '12 at 21:47
  • You should only ask one question per post, otherwise the question and answer won't be valuable to anyone but you, because it makes it too hard to find the information. – Jake May 20 '12 at 13:29

1 Answers1

7

You'll have to set point meta=explicit and meta=X. To get a smoother colour transition and to be able to set define over what domain the regression line is drawn, you'll have to calculate the parameters of the regression line first using

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

\xdef\slope{\pgfplotstableregressiona}
\xdef\intercept{\pgfplotstableregressionb}

and then drawing the line using \addplot [mesh,point meta=x,domain=0:0.2] {\slope*x+\intercept};.

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\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},
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 $\mbox{m}$,
ylabel=\large $\Delta\mbox{l}$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    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:0.2] {\slope*x+\intercept};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
Jake
  • 232,450
  • very good ! is possible obtain a more nuanced coloration of the straight line (similar to the colorbar)? Another thing, there a way to decide the length of the line? – Aurelius May 19 '12 at 13:16
  • also the "minor tick" on the x axis can reappear ? – Aurelius May 19 '12 at 13:32
  • and the darker contour of the dots can be removed? – Aurelius May 19 '12 at 13:39
  • @Jake: The op suggested an edit. – Marco Daniel May 19 '12 at 14:08
  • @FormlessCloud: About the nuanced colouring: So you don't want the colours to follow the mapping of the colour bar (which would assign purple to 0, green to 0.1, and red to 0.2), but create a different mapping? – Jake May 19 '12 at 17:54
  • I wish that the line follow the colors of the color bar but in a gradual manner by eliminating the step of color which occurs at 0.1 for example. Watch the colorbar, see how to switch from one color to another in a gradual manner? I added an example of gradual coloring of a straight line with the same color of my colorbar... is also possible space out the ticks so they do not touch? – Aurelius May 19 '12 at 22:21
  • 1
    @FormlessCloud: I've edited my answer. – Jake May 20 '12 at 10:31
  • ok, to distance the ticks so that do not touch how can i do? and to eliminate the dark outline of the dots? why you put always 4 minor ticks ? instead of 3? – Aurelius May 20 '12 at 13:02
  • xticklabel style={text height=2.0ex} i have try this command but I wish all the ticks are distanced equally and that the distance from it and the axis titles will remain the same – Aurelius May 20 '12 at 13:10
  • i have add "thick" and "samples=1000" to this: "\addplot [mesh,point meta=x,domain=0.025:0.175,thick,samples=1000] {\slope*x+\intercept};" – Aurelius May 20 '12 at 13:15
  • 1
    @FormlessCloud: You can set xticklabel shift=3pt, yticklabel shift=3pt to move the tick labels. Using four minor tick labels has the reason that they represent values that I find more useful: 0, 0.01, 0.02, 0.03, 0.04, 0.05. If you only used three, they would represent 0, 0.0125, 0.025, 0.0375, 0.05. By the way, 1000 samples is way too much. Try 50 instead, you won't see the difference. – Jake May 20 '12 at 14:03
  • ok good, for move the axis labels and maintain the same distance between it and ticks instead? – Aurelius May 20 '12 at 19:14
  • @FormlessCloud: I don't understand the question. You should probably post this as a new question, with a minimal example and a detailed explanation of what you want to achieve, what you tried, and why it didn't work. – Jake May 21 '12 at 09:07