1

I plotted a line using 10 coordinates from between 0 to 1 in 0.1 step size. Each Y-coordinate is an average value. I have the variance for each average value and want to plot a filled area around the function that depicts the variance. My code so far:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document} \begin{figure}[h!] \begin{subfigure}[t]{0.82\textwidth} \resizebox{1\textwidth}{!}{% \begin{tikzpicture} \begin{axis}[ xmin=0, xmax=1.1, ymin=16.9, ymax=17.3, xlabel={x}, ylabel={y}, axis lines=left, xtick={0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}, xticklabels={0, , , , , 0.5, , , , , 1}, ytick distance={0.1}, xticklabel style={rotate=-0, yshift=-0.4ex}, xlabel style={anchor=north}, xmajorgrids=true, grid style=dashed, legend pos=outer north east, height=6cm, ] \addplot coordinates { (0, 17.2) (0.1, 16.91) (0.2, 17.04) (0.3, 17.26) (0.4, 17.11) (0.5, 17.08) (0.6, 17.11) (0.7, 17.24) (0.8, 17.23) (0.9, 17.16) (1, 17.22) }; \end{axis} \end{tikzpicture} } \caption{my cap} \end{subfigure}% \end{figure} \end{document}

For example each Y-coordinate should have a variance of 0.1 to 0.3, visualized as filling around the function/line. I want to specify the variance, thus the size of the "tube" around the function for each of the 10 points individually. How to accomplish this? My variance values are:

(0,   0.03)
(0.1, 0.03)
(0.2, 0.06)
(0.3, 0.01)
(0.4, 0.02)
(0.5, 0.03)
(0.6, 0.05)
(0.7, 0.11)
(0.8, 0.04)
(0.9, 0.09)
(1,   0.05)

The variance should look something like the gray filled area in this example: Example

Qrrbrbirlbel
  • 119,821
  • 1
    Welcome to TeX.SE! Could you edit into your question code that begins with your \documentclass{} and ends with \end{document} that we can compile please? It helps people help you if we can copy and paste immediately and trial solutions, please include whatever minimal amount of packages we need to compile it on our computers :) – JamesT Aug 30 '23 at 20:39
  • 1
    @trinity420, you can't expect serious answers when your code can't compile ... – MS-SPO Aug 30 '23 at 20:46
  • 1
    @JamesT Thank you, I fixed the code, it is now compilable. – trinity420 Aug 30 '23 at 21:09
  • 2
    @trinity420 +1, thank you, in the future always help us like that, it makes it so much easier to help out! A single package can change an entire answer, LaTeX is different to other programming languages :) – JamesT Aug 30 '23 at 21:40
  • 1
    @JamesT Yeah, sure! Sorry, I did not consider some packages are needed for this to compile. I think it's an easy problem, curious if someone solves it... – trinity420 Aug 30 '23 at 21:48
  • 1
    @trinity420, just to clarify, do you want variance bars around each value of your plotted points or? If so, we need your var values. Also do not worry about semantics for the first few posts, we were all new here once upon a time. TeX.SE is probably the friendliest site on these sites, we want to help and enjoy it with no judgement :) – JamesT Aug 30 '23 at 22:00
  • @JamesT I don't know if bars is the correct term, like an area filled around the line which depicts the variance. I have updated with an example image. Okay, thank you, great! :) – trinity420 Aug 30 '23 at 22:12
  • Look for these keywords in the pgfplots manual: error bar, variance list, area plots. The first adds error bars (those T-shapes), the second curves with added var-value, the last filled areas. However, perhaps you should reformulate your data from coordinates to table, where you could include the missing columns to make application easier to do. – MS-SPO Sep 01 '23 at 11:04
  • 1
    @MS-SPO I was using tables but that caused some package issues in the template I have to use. So I switched to coordinates. Thanks, I solved it now with two new lines that are filled in between. – trinity420 Sep 01 '23 at 12:13

1 Answers1

1

I solved it. When using coordinates, one has to draw two new lines and give them names. One for the upper limit of variance/sd and one for the lower limit. Then fill the area in between those named lines. Two more command are needed after the package loading for this functionality.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}   % added (!)
\usepgfplotslibrary{fillbetween} % added (!)

\begin{document} \begin{figure}[h!] \begin{subfigure}[t]{0.82\textwidth} \resizebox{1\textwidth}{!}{% \begin{tikzpicture} \begin{axis}[ xmin=0, xmax=1.1, ymin=16.9, ymax=17.3, xlabel={x}, ylabel={y}, axis lines=left, xtick={0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}, xticklabels={0, , , , , 0.5, , , , , 1}, ytick distance={0.1}, xticklabel style={rotate=-0, yshift=-0.4ex}, xlabel style={anchor=north}, xmajorgrids=true, grid style=dashed, legend pos=outer north east, height=6cm, ] \addplot coordinates { (0, 17.2) (0.1, 16.91) (0.2, 17.04) (0.3, 17.26) (0.4, 17.11) (0.5, 17.08) (0.6, 17.11) (0.7, 17.24) (0.8, 17.23) (0.9, 17.16) (1, 17.22) }; \addplot[name path=down,color=blue!70] coordinates { (0, 17.1) (0.1, 16.81) (0.2, 17.00) (0.3, 17.16) (0.4, 17.01) (0.5, 17.00) (0.6, 16.90) (0.7, 17.14) (0.8, 17.13) (0.9, 17.06) (1, 17.00) }; \addplot[name path=up,color=blue!70] coordinates { (0, 17.3) (0.1, 17.05) (0.2, 17.14) (0.3, 17.35) (0.4, 17.23) (0.5, 17.2) (0.6, 17.21) (0.7, 17.34) (0.8, 17.33) (0.9, 17.26) (1, 17.30) }; \addplot[blue!50,fill opacity=0.4] fill between[of=down and up]; \end{axis} \end{tikzpicture} } \caption{my cap} \end{subfigure}% \end{figure} \end{document}

One drawback is, that this solution is not concise at all. However, it gives a nice drawing without any bars. I build the solution from following questions: Q1 and Q2.

Result, with slightly adapted range:

result

MS-SPO
  • 11,519