# How to plot a customized bell shape using tikz

I would like to ask if it is possible to reproduce this figure on LateX using tikz ? If so could you help me to give some advice to do so ?

EDIT : This is my current script.

\documentclass{article}
\begin{document}

\usepackage{tikz}

\begin{tikzpicture}[domain=0:5]
\draw[very thin,color=gray] (-0.1,-1.1) grid (8.9, 4.9);
\draw[->] (-0.2,0) -- (5.2,0) node[right] {$e_x$};
\draw[->] (0,-1.2) -- (0,5.2) node[above] {$e_z$};
\draw[color=red]    plot (\x, {(1-((\x^2)/16))^2})  node[above] {$h_N = h_N+1 = 0$};
plot[only marks,mark=ball] coordinates{(0,0),(4,0)};
\end{tikzpicture}

\end{document}


But there are many things that I want to achieve but I don't know how.

1). My function is defined on [0, 10] by parts such as :

But I don't know how to define this condition with tikz.

2). I want to put two dots : one on the origin (0,0), another on the value x=4 (4,0) but haven't succeeded with this line :

plot[only marks,mark=ball] coordinates{(0,0),(4,0)};


3). How is it possible to add the text h_0 on the left such as in the image ? I have tried : \pgftext[left] {$h_0$}

But I am not able to put it exactly in the beginning of the curve

Thank you very much.

• Do you have the formula? If yes use pgfplots or something, otherwise use ..controls. Jun 12 at 9:47
• I see you already know how to use node to put text inside TikZ figure? Just use same method for the formula Jun 12 at 9:54
• Whether using data visualization of pgfplots, you may need to specify coordinates in terms of the data values. In pgfplots this is done using (axis cs: x,y). In tikz it uses (visualization cs: x,y). Jun 12 at 14:15

Edit: Your question is not entirely clear. So I only guess that you may like to obtain the following images:

In image is used given equation for your function. In drawing are used TikZ libraries arrows.meta for arrows and patterns.meta for patterns. For some images' elements are defined styles that image code is shorter and more clear:

\documentclass[margin=3.14mm]{standalone}
\usepackage{nccmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
patterns.meta
}

\begin{document}
\begin{tikzpicture}[
x = 5mm,
> = Straight Barb,
dot/.style = {circle, fill, inner sep=1.6pt, node contents={}},
every label/.append style = {label distance =2pt, inner sep=1pt, fill=white},
samples = 101,
domain = 0:4 % after 4 function is drawn is by followed line
]
% axis
\draw[-stealth] (-0.5, 0) -- (11,0) node[right] {$\underline{e}_x$}; % x-axis
\draw[-stealth] (0, -0.5) -- ( 0,2) node[above] {$\underline{e}_z$}; % y-axis
\foreach \i in {0,2, ...,10}
\draw   (\i, 1mm) -- ++ (0,-2mm);
% function
\draw[very thick, blue]
plot    (\x, {(1-(\x^2)/16)^2}) -- (10,0);
\path[pattern={Lines[angle=45,distance={3pt},
line width=0.2pt]},
pattern color=blue]
plot    (\x, {(1-(\x^2)/16)^2}) -- (0,0);
\node[right] at (1,0.4) {$V$};
\node[right, font=\footnotesize]
at (4,1.0) {$\underline{e}_z\begin{cases} \Bigl(1 - \mfrac{x^2}{16}\Bigr)^2 & \text{if } x \leq 4 \\ 0 & \text{if } x > 4 \end{cases}$
};
% domain
\path   (0,0) node[dot,label=below:$0$] (4,0) node[dot,label=below:$L$];
\end{tikzpicture}
\end{document}


Note: I took a liberty and remove error in your equation.

• Thank you for your help : compared to the previous answer the filled region is coded differently : \path[pattern={Lines[angle=45,distance={3pt},line width=0.2pt]},pattern color=blue] plot (\x, {(1-(\x^2)/16)^2}) |- (0,0); But could you explain why does the line plot (\x, {(1-(\x^2)/16)^2}) |- (0,0); is written like this ? I mean what is the purpose of |- (0,0); ?
– Wiss
Jun 13 at 15:48
• @Wiss, I use different library and use pattern as you indicate in your sketch. The |- )o0r -|) is orthogonal coordinates which remain from the first version of answer. It can be now replaced by -- )now donne in edited answer). It serves for to close area for pattern. However, does answer provide what you like to get? Jun 13 at 16:31
• Yes thank you I've marked the post as answered. However, I have still some issues understanding this line. For instance, to draw the function you have closed at the point (10,0) (which is logical because it's the end of the domain) but why the fill pattern is closed on (0,0) ? -- (0,0); It is still the same function ({(1-(\x^2)/16)^2}) defined ! I hope my question is clear
– Wiss
Jun 13 at 17:22
• @Wiss, the best way to learn is learning-by-doing. Make a experiment and in this code line omit  -- (0,0)˙ and observe result! You will see, that the pattern is between curve and (not drawn) line between start and end of curve. So you need to move end of curve to the same horizonal position as is its start, i.e. to (0,0). Even better, this par of code should be  -| (0,0), i.e that we get closed curve. For details and more thorough explanation please consult tikz package documentation (section 62.3 User-Defined Patterns, page 731). Jun 13 at 18:12

Here is a sample output using the function 1.5^(-x^3). Note that your formula includes x=4 in both cases. To have a more of a bell shape, use the function 1.25^(-x^2) but you will have to change the domain as well as adjust the other components.

\documentclass[margin=3.14mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{accents} % for bar under character
\usetikzlibrary{patterns, matrix, positioning}

\begin{document}
\begin{tikzpicture}
\draw[-stealth] (-0.5, 0) -- (6, 0) node[right] {$\underaccent{\bar}{e}_x$}; % x-axis
\draw[-stealth] (0, -0.5) -- (0, 4) node[above] {$\underaccent{\bar}{e}_z$}; % y-axis
% horizontal ticks
\foreach \i in {0, 0.4, ..., 5} \draw[draw=blue] (\i, 0.05) -- ++ (0,-0.1);
% the filled region plotted which is 1.5^(-x^3)
\fill[scale=2, domain=0:3, smooth, variable=\x,
% remove next line to have a solid colour fill
pattern=crosshatch, pattern color=red,
preaction={fill, gray, fill opacity=0.1},% pattern background colour
draw=none,
fill opacity=0.25
] (0, 0) -- plot ({\x}, {1.5*exp(-\x*\x*\x)});
% curve outline
\draw[scale=2, gray, domain=0:2, smooth, variable=\x] plot ({\x}, {1.5*exp(-\x*\x*\x)});
% coordinates and the label in the shaded area
\draw[black,fill=black] (0, 3) circle (1.5pt) node[left, black]{$h_0$};
\draw[black,fill=black] (4, 0) circle (1.5pt) node[below, black]{$L$};
\draw[black,fill=black] (0, 0) circle (1.5pt) node[below left, black]{$0$};
\node[black] at (0.75, 1.25) {$V$};
% function as text is denoted as a 2 * 2 matrix with a left delimiter
\matrix[matrix of math nodes,
left delimiter=\lbrace,
inner sep=0pt, row sep=10pt,
nodes={font=\normalsize}
] (m) at (5.5, 2)
{
\left(1 - \dfrac{x^2}{16}\right)^2 & \text{if } x \leq 4 \\
0 & \text{if } x \geq 4 \\
};
% the label of the matix
\node [left=8pt of m, yshift=1.5pt] {$f(x) =$};
\end{tikzpicture}
\end{document}

• @Jumaily Thank you for your help : I think it goes beyond my expectations ! But I might have some questions : - What is the aim to define the text as a matrix with a delimiter ? Is it the simplest way to plot a text in a customized position ? - Also : how does the \fill line work ? I mean you specify domain =0:3 but in the figure the area seems larger than that. Thank you very much
– Wiss
Jun 12 at 18:13
• The usage of the matrix is to write the function with two cases on the graph using TikZ. The two columns are there for align the text. You can simply use \node[] at (x, y) {Text}; if you wanted to add simple text but yours required a brace. The fill option follows exactly what it drew, the tick on the x-axis are not drawn to scale. I just created them based on the ticks you had. Jun 13 at 1:02
• @Wiss, I strongly recommend spending some time on the introduction first, and then get adicted to the manual at: ctan.org/pkg/pgf . Jun 13 at 8:17