使用 Tikz 绘制直方图

使用 Tikz 绘制直方图

我是 TeX 新手。我需要使用 Tikz 复制此直方图

在此处输入图片描述

当然,我可以制作类似的东西。不幸的是,我需要一个复制品(90%)。有人能帮我吗?

我拥有的:

\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={excellent, good, average, bad, awful},
        ylabel = {probability},
        xlabel = {Quality},
    xtick=data]
    \addplot[ybar,fill=white] coordinates {
        (excellent,5)
        (good,10)
        (average,50)
    (bad, 20)
    (awful,15)
    };
\end{axis}
\end{tikzpicture}

在此处输入图片描述

答案1

也许是这样的。

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      bar width=20pt,
      xlabel={Quality},
      ylabel={Probability},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={excellent,good,average,bad,awful},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]
      \addplot[fill=white] coordinates {
        (excellent,5)
        (good,10)
        (average,50)
        (bad,20)
        (awful,15)
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

以下是一些可以修复微小细节的设置。您可以注释掉它们以查看它们的作用。箭头来自 TikZ 中的 DIN 符合箭头和阴影线?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\makeatletter
% https://tex.stackexchange.com/questions/52786/din-conform-arrowheads-and-hatching-in-tikz
\pgfarrowsdeclare{DIN}{DIN}
{
  \pgfutil@tempdima=0.5pt%
  \advance\pgfutil@tempdima by.25\pgflinewidth%
  \pgfutil@tempdimb=7.29\pgfutil@tempdima\advance\pgfutil@tempdimb by.5\pgflinewidth%
  \pgfarrowsleftextend{+-\pgfutil@tempdimb}
  \pgfutil@tempdimb=.5\pgfutil@tempdima\advance\pgfutil@tempdimb by1.6\pgflinewidth%
  \pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
  \pgfutil@tempdima=0.5pt%
  \advance\pgfutil@tempdima by.25\pgflinewidth%
  \pgfsetdash{}{+0pt}
  \pgfsetmiterjoin
  \pgfpathmoveto{\pgfpointadd{\pgfqpoint{0.5\pgfutil@tempdima}{0pt}}{\pgfqpoint{-4mm}{0.5mm}}}
  \pgfpathlineto{\pgfqpoint{0.5\pgfutil@tempdima}{0\pgfutil@tempdima}}
  \pgfpathlineto{\pgfpointadd{\pgfqpoint{0.5\pgfutil@tempdima}{0pt}}{\pgfqpoint{-4mm}{-0.5mm}}}
  \pgfpathclose
  \pgfusepathqfillstroke
}
\pgfarrowsdeclarereversed{DIN reversed}{DIN reversed}{DIN}{DIN}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={excellent, good, average, bad, awful},
        ylabel = {probability},
        xlabel = {Quality},
        ytick=\empty,ymin=0,
        axis x line=bottom,
        axis y line=left,
        enlarge x limits=0.2,
        axis line style={-DIN,ultra thin},
        xtick=data,
        nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
        ylabel near ticks,
        xticklabel style={anchor=base,yshift=-3mm},
        xtick style={draw=none},
        every axis x label/.style={at={(current axis.south east)},anchor=north west}
    ]
    \addplot[ybar,fill=white] coordinates {
        (excellent,5)
        (good,10)
        (average,50)
    (bad, 20)
    (awful,15)
    };
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容