如何根据函数绘制条形图

如何根据函数绘制条形图

我想知道如何根据给定函数绘制条形图(阶梯图)。(连续)图表的代码是:

\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}

我想知道如何通过上面给出的函数绘制像数字化的条形图。应指定步骤数。谢谢。

答案1

我带来了一些可以作为良好开始的东西。

\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}

你可以使用变量来设置n你想要的条形数量。
在这里,我尝试将条形置于利用\x值的中心。这可能不是你想要的,只需询问即可。

\n=150 n=150

\n=50 n=50

\n=21 n=21

\n=14 n=14

\n=7 n=7

答案2

这是另一个解决方案,可能比我之前的解决方案(在计算条长度时有一些不准确)好得多,现在有了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

n=20

50 bars 50 酒吧

100 bars 100 酒吧

相关内容