3

I want to create a curve using tikz. As can be seen, the curve is made of connected lines with no rounded connection points and I would like to round off the edges. Following is my code:

\documentclass[11pt, a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} 
\usepackage{bm}
\usepackage{nomencl}
\makenomenclature
\usepackage{float}
\usepackage[comma]{natbib}
\usepackage{pgfplots}
\usepackage[super]{nth}
\usepackage{textcomp}
\usepackage{multirow}
\usepackage[version=3]{mhchem}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}
\usepackage[hidelinks]{hyperref}
\usepackage{siunitx}
\usepackage{tikz}
\renewcommand{\sectionautorefname}{Section}
\renewcommand{\chapterautorefname}{Chapter}
\parskip=0.05in

\begin{document}
\begin{tikzpicture}
\label{sim}
\begin{axis}[
xlabel={Hydration time (h)},
ylabel={Hydration heat evolution rate (J/gh)},
xmin=0, xmax=100,
ymin=0, ymax=20,
legend pos=north east,
ymajorgrids=true,
grid style=dashed,
]

\addplot[
color=blue,
]
    coordinates 
{   (0,0) (0.5,3.59) (1.5,8.65) (2.5,15.026) (3.5,18.63) (4,18.07) (4.5,15.91) (6.5,9.81) (11.5,6.06) (15,5.04) (15.5,4.788) (20.5,3.32) (26,2.58) (27.5,2.33) (30,2.12) (40,1.12) (50,0.85) (80,0.6972) (99,0.648)
};
\legend{Layers 1 to 5}

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Welcome to TeX.SE. Where are into your code \begin{document} and \end{document}? – Sebastiano Sep 5 '18 at 10:46
  • Sorry forgot to add that. I edited it now :) – user157109 Sep 5 '18 at 10:49
  • 1
    @user157109 I don't understand what you want, but I guess that the option line join = round or the option smooth do what you want. Also, there's something wrong with the data point (27,5,2.33), which is causing the error Sebastiano mentioned. – Phelype Oleinik Sep 5 '18 at 11:07
  • 1
    @Sebastiano this is a point plot, there are no samples to increase. smooth might help. But not much – daleif Sep 5 '18 at 11:10
  • 2
    @Sebastiano I don't know for sure, I'm not an expert here. But I think that the samples option is valid when you have an analytical function, not a discrete data set. – Phelype Oleinik Sep 5 '18 at 11:11
2

As daleif said PGFPlots can't do much about the data you give it to plot. If it was an analytical (and smooth) function you could simply increase the number of samples and the plot would look smoother. With a discrete dataset there is not much that can be done; any graphing software won't do much more than connecting the dots.

PGFPlots however offers you some work-arounds to smoothen your plot without giving it more data.

Here's your original picture (with a few spy lenses):

enter image description here

If I add the smooth option PGFPlots will, instead of a straight line, try to join your points with a smooth line. Smooth meaning that the tangent to the line is the same to the left and to the right of the point. This can make some weird output if the data points are too close and can't be drawn with a smooth line without overshooting. Your plot with smooth:

enter image description here

The peak of the plot looks OK, but the second spy lens highlights a slight overshoot. That happened because the data points are too close and the tangent of the line changes too drastically between them.

The smooth option has a default “tension” for the line, which does more or less as the name says: the tension at the data points, which will say how much of the line will be smoothed. As Mr. marmot suggested you can reduce that tension (the default is 0.5 to have a better looking curve (picture with tension=0.3):

enter image description here

Unfortunately you can't go much further without changing the underlying data.

Here's the code I used for the pictures:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{spy}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[spy using outlines]
  \begin{axis}[
      xlabel={Hydration time (h)},
      ylabel={Hydration heat evolution rate (J/gh)},
      xmin=0, xmax=100,
      ymin=0, ymax=20,
      legend pos=north east,
      ymajorgrids=true,
      grid style=dashed,
    ]
    \addplot[
      color=blue,
      smooth,
      tension=0.3,
    ]
      coordinates
      {
        (0,0) (0.5,3.59) (1.5,8.65) (2.5,15.026)
        (3.5,18.63) (4,18.07) (4.5,15.91) (6.5,9.81)
        (11.5,6.06) (15,5.04) (15.5,4.788) (20.5,3.32)
        (26,2.58) (27.5,2.33) (30,2.12) (40,1.12)
        (50,0.85) (80,0.6972) (99,0.648)
      };
    \legend{Layers 1 to 5}
    \coordinate (peak) at (axis cs:3.5,18.63);
    \coordinate (midd) at (axis cs:15,5.04);
  \end{axis}
  \spy [circle, magnification = 4, size = 1.5cm, connect spies] on (peak) in node at ($(peak)+(1.2,-1)$);
  \spy [circle, magnification = 4, size = 1.5cm, connect spies] on (midd) in node at ($(midd)+(1.2, 1)$);
\end{tikzpicture}
\end{document}

Where to put smooth and tension=0.3?

PGFPlots runs on TeX, and one of TeX's features is scoping. This coping applies to PGFPlots as well. There are four places where you can put these options and they will have different scopes:

  1. Somewhere in the preamble put \tikzset{smooth,tension=0.3}: this will affect all tikzpictures from that point on; this has a global effect and will be used in any drawing you have (not really recommended).

  2. As an option to tikzpicture (\begin{tikzpicture}[smooth,tension=0.3]): this will limit the scope of these instructions to the current tikzpicture and will affect any drawing inside it. Once the tikzpicture ends, the effect ends as well.

  3. As an option to axis (\begin{axis}[smooth,tension=0.3]): same as above, but the effect will be limited to that axis.

  4. As an option to \addplot (\addplot[smooth,tension=0.3]): the scope of the options will be limited to that plot only. Any other plot, even in the same axis, will have the default options. Since you want to smoothen this specific curve, this is the way to go, as the scope is exactly the one you want.

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.