# How to draw bar chart from a function

I'd like to know how to draw a bar (step) chart from a given function. The code for a (continuous) chart is:

\documentclass[preview,border=2pt,2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{arrows.meta}
\tikzset{decorate sep/.style 2 args=
{decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}
\tikzset{>={Latex[width=1mm,length=3mm]}}

\begin{document}
\begin{tikzpicture}
\tikzstyle{line} = [arrows=<->,line width=0.7pt]
\tikzstyle{line2} = [line width=0.7pt]

\def\r{0.6pt} %define the radius of spot
\def\ax{3.3} %define the length of x-axis
\def\ay{4} %define the length of y-axis
\def\y{3} %define the height of T=1
\def\b{2} %define the postion of b

\tikzset{
declare function={
normpdf(\x,\m,\s,\r)=\r*exp(-((\x-\m)/\s)^4);
}
}

%first chart
\draw[line] (0,\ay) coordinate node [below left] {$T$} -- (0,0) coordinate node[below] {$0$} -- (\ax, 0) coordinate node [below left] {$x$};
\draw[scale=1, domain=-2:2, smooth, variable=\x] plot ({\x}, {normpdf(\x,0,1.3,\y)});
\draw[line2] (0,0) -- (-\ax+0.5, 0);

\filldraw (0,\y) circle (\r) node[left] at (0,\y+0.2) {$1$};
\filldraw (\b,0) circle (\r) node[below] at (\b,0) {$b$};
\filldraw (-\b,0) circle (\r) node[below] at (-\b,0) {$-b$};

\end{tikzpicture}
\end{document}


I want to know how to draw a bar chart like a digitized one from the function given above. The number of steps should be specified. Thanks.

## 2 Answers

I came with something that could be a good start.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[yscale=3]

\def\r{0.6pt} %define the radius of spot
\def\b{3} %define the postion of b
\def\s{1.3}
\def\m{0}

\def\n{50} % number of bars
\def\xmin{-\b} \def\xmax{\b}

% compute the bar length
\pgfmathparse{(\xmax-\xmin+1)/\n}\edef\barlen{\pgfmathresult}%
\pgfmathparse{(\xmin+\barlen)}\edef\xminb{\pgfmathresult}%

\draw (\xmin-0.2,0)--(\xmax+0.2,0) (0,-.2) --(0,1);
\foreach \x in {\xmin,\xminb,...,\xmax}
{
\pgfmathparse{\r*2.718^(-((\x-\m)/\s)^4)}\edef\y{\pgfmathresult}%
\pgfmathparse{\x-(\barlen)/2}\edef\xstart{\pgfmathresult}%
\draw (\xstart,0) rectangle++ (\barlen,\y);
}

\end{tikzpicture}

\end{document}


You can play with variable n which is the number of bars you want.
Here, I tried to center the bars on the exploited \x value. It might not be what you are looking for, just ask.

\n=150

\n=50

\n=21

\n=14

\n=7

Here's another solution, maybe way better than my previous one (which has some inaccuracies in calculations for the bar length), now with pgfplots.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\def\r{0.6pt} %define the radius of spot
\def\ax{3.3} %define the length of x-axis
\def\ay{4} %define the length of y-axis
\def\y{3} %define the height of T=1
\def\b{2} %define the postion of b

\def\s{1.3}
\def\m{0}

\def\n{50} % number of bars

\begin{axis}[
declare function = {normpdf(\x,\m,\s,\r)=\r*exp(-((\x-\m)/\s)^4);},
axis lines=middle,
ymin=0, ymax=.7,  ylabel=$y$,
xmin=-3, xmax=3,  xlabel=$x$,
ytick=\empty,
xtick={-3,...,3},
ticklabel style = {font=\footnotesize},
no marks
]
\addplot [domain=-3:3, thick, blue,samples=100] {normpdf(\x,\m,\s,\r)};
\addplot [domain=-3:3,  ybar interval=1, fill=orange, fill opacity=0.3, samples=\n]{normpdf(\x,\m,\s,\r)};

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


20 bars

50 bars

100 bars