2

I want to fill some color in the line graph and code like below:

\begin{tikzpicture}
\begin{axis}[
        xmin=0.09,
        xmax=0.32,
        ymin=0,
        ymax=230,
        ylabel=$num$,
        xlabel=$ratio$,
        height=6cm,
        width=8cm,
        grid=major,]
\addplot [black, fill=red!20] plot [smooth] 
    coordinates {
    (0.09, 196)
    (0.1 , 215)
    (0.11, 207)
    (0.12, 203)
    (0.13, 205)
    (0.14, 191)
    (0.15, 194)
    (0.16, 193)
    (0.17, 180)
    (0.18, 159)
    (0.19, 130)
    (0.2 , 78)
    (0.21, 71)
    (0.22, 59)
    (0.23, 70)
    (0.24, 55)
    (0.25, 54)
    (0.26, 48)
    (0.27, 46)
    (0.28, 48)
    (0.29, 54)
    (0.3 , 45)}
     -- (0.09,0)-- (0.3,0) -- cycle;
\end{axis}
\end{tikzpicture} 

But I get a figure like this?

enter image description here

Does someone know why the area is not closed? thank you.

3

One can tell tikz to use horizontal and vertical lines instead of straight ones with |-:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
        xmin=0.09,
        xmax=0.32,
        ymin=0,
        ymax=230,
        ylabel=$num$,
        xlabel=$ratio$,
        height=6cm,
        width=8cm,
        grid=major,]
\addplot [black, fill=red!20] plot [smooth] 
    coordinates {
    (0.09, 196)
    (0.1 , 215)
    (0.11, 207)
    (0.12, 203)
    (0.13, 205)
    (0.14, 191)
    (0.15, 194)
    (0.16, 193)
    (0.17, 180)
    (0.18, 159)
    (0.19, 130)
    (0.2 , 78)
    (0.21, 71)
    (0.22, 59)
    (0.23, 70)
    (0.24, 55)
    (0.25, 54)
    (0.26, 48)
    (0.27, 46)
    (0.28, 48)
    (0.29, 54)
    (0.3 , 45)}
      |- (0,0) |- cycle;
\end{axis}
\end{tikzpicture} 
\end{document}

enter image description here

3
  • Great job, do you know how to add a vertical line at x=0.19, something like \addplot [dashed] (0.19,0) |- (0.19,100); – Gao Yuze Jul 18 '19 at 6:10
  • @GaoYuze please ask a new question – Johannes D Jul 18 '19 at 6:39
  • Thanks, I found an answer when creating a new question,😺 Have a nice day. – Gao Yuze Jul 18 '19 at 6:53

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.