如何在 Word 中绘制带有标签 x 轴而不是数字的图表?

如何在 Word 中绘制带有标签 x 轴而不是数字的图表?

我实际上想绘制如下图所示的图表:

在此处输入图片描述

基本上,我想使用以下数据绘制图表:

Testing1     9   3
Testing2    4   5
Testing3    4  5
Testing4   1    4
Testing5   1     5
Testing6   8      7
Testing7   1     0
Testing8   1     0
Testing9   1     0

我尝试了我的代码:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
%\usepgfplotslibrary{external}
%\tikzexternalize

\begin{document}

Bar chart:

\begin{tikzpicture}
\begin{axis}
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Number,
    enlargelimits=0.05,
    legend style={at={(0.5,-0.2)},
    anchor=north,legend columns=-1},
    ybar interval=.7,
]
\addplot 
    coordinates {(Testing1,9) (Testing2,4)
         (Testing3,4) (Testing4,1) (Testing5,1) (Testing6,8) (Testing7,1) (Testing8,1) (Testing9,1)};
\addplot 
    coordinates {(Testing1,3) (Testing2,5)
         (Testing3,5) (Testing4,4) (Testing5,5) (Testing6,7) (Testing7,0) (Testing8,0) (Testing9,0)};
\legend{Series 1, Series2}
\end{axis}
\end{tikzpicture}

\end{document}

结果如下: 在此处输入图片描述

答案1

你想使用symbolic x coordinates ybar代替ybar interval

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}%<- 1.16 would be better

\begin{document}

Bar chart:

\begin{tikzpicture}
\begin{axis}[symbolic x coords={Testing1,Testing2,Testing3,Testing4,Testing5,
Testing6,Testing7,Testing8,Testing9},
    x tick label style={anchor=north west,rotate=-30},
    ylabel=Number,
    enlargelimits=0.05,
    legend style={at={(0.5,-0.2)},
    anchor=north,legend columns=-1},
    ybar 
]
\addplot 
    coordinates {(Testing1,9) (Testing2,4)
         (Testing3,4) (Testing4,1) (Testing5,1) (Testing6,8) (Testing7,1) (Testing8,1) (Testing9,1)};
\addplot 
    coordinates {(Testing1,3) (Testing2,5)
         (Testing3,5) (Testing4,4) (Testing5,5) (Testing6,7) (Testing7,0) (Testing8,0) (Testing9,0)};
\legend{Series 1, Series2}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是一个更自动化的版本。

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.16}

\begin{document}

\begin{tikzpicture}
\pgfplotsforeachungrouped \X in {1,...,9}
{\ifnum\X=1
\edef\mylst{Testing1}
\else
\edef\mylst{\mylst,Testing\X}
\fi}
\begin{axis}[symbolic x coords/.expanded=\mylst,
    ylabel=Number,
    enlargelimits=0.05,
    x tick label style={anchor=north west,rotate=-30},
    legend style={at={(0.5,-0.2)},
    anchor=north,legend columns=-1},
    ybar,
]
\addplot 
    coordinates {(Testing1,9) (Testing2,4)
         (Testing3,4) (Testing4,1) (Testing5,1) (Testing6,8) (Testing7,1) (Testing8,1) (Testing9,1)};
\addplot 
    coordinates {(Testing1,3) (Testing2,5)
         (Testing3,5) (Testing4,4) (Testing5,5) (Testing6,7) (Testing7,0) (Testing8,0) (Testing9,0)};
\legend{Series 1, Series2}
\end{axis}
\end{tikzpicture}
\end{document}

答案2

一个 pstricks解决方案,带有pst-bar包,它将.csv文件作为输入,其第一行带有标签:

 \documentclass[svgnames, border=8pt]{standalone}
\usepackage{amssymb} 
\usepackage[svgnames]{xcolor}
\usepackage{pst-bar}
\usepackage{multido} 
\usepackage{auto-pst-pdf}

\newpsbarstyle{colora}{fillcolor=CornflowerBlue!90, fillstyle=solid , framearc =0 }
\newpsbarstyle{colorb}{fillcolor=SandyBrown!75!IndianRed, fillstyle=solid , framearc =0 }

\begin{document}

\sffamily
\psset{xunit=2cm,yunit=0.75cm, }
\begin{pspicture}(-0.3,-2.4)(9,10)%
\multido{\i=0+1}{11}{\psline[linecolor=Gainsboro!50](0,\i)(9, \i)\uput[l](0,\i){\i}}
\psaxes[axesstyle=frame, Ox=0, Dx=1, labels=none, ticks=none, linestyle=none](0,0)(9,10)%
\readpsbardata{\data}{testdata.csv}%
\psset{barsep=0.06, barcolsep = 0.4, barstyle={colora, colorb},linestyle=none}%
\psbarchart{\data}%
\rput(4.5,-2){\textcolor{CornflowerBlue!90}{$ \blacksquare $} Series 1\qquad\textcolor{SandyBrown!75!IndianRed}{$ \blacksquare $} Series 2}
\end{pspicture}

\end{document}

在此处输入图片描述

编辑:内容testdata.csv(仅 3 行):

Testing~1,  Testing~2,   Testing~3, Testing~4, Testing~5, Testing~6, testing~7, Testing~8, Testing~9
9,  4,  4, 1,  1, 8, 1, 1, 1
3, 5, 5, 4, 5,  7,  0, 0, 0

相关内容