如何在 LaTeX 中水平绘制条形图

如何在 LaTeX 中水平绘制条形图

我想水平绘制条形图。我尝试使用以下代码,但它打印的是垂直图:

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

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, calc}

\begin{document}
    \newcommand\shift{1.5}

    \begin{tikzpicture}[scale=1]
        \begin{axis}[
            ybar,
            ylabel={vaule},
            ylabel style={at={(0.02,0.5)}, color=red},
            xlabel={items},
            legend style={at={(0.5,-0.15)},
            anchor=north,legend columns=-1},
            width=0.8*\textwidth,
            height=9cm,
            bar width=7pt,
            symbolic x coords={
                $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$,
                $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
                $x_{14}$, $x_{15}$
            },
            xtick=data,
            scaled y ticks=base 10:-3,
            axis y line*=left,
            %nodes near coords,
            %nodes near coords align={vertical},          
         ]
            \addplot[red, fill=red!15, shift={(-\shift,0)}] coordinates {
                ($x_1$,96000) ($x_2$,126000) ($x_3$,115000)
                ($x_4$,125000) ($x_5$,123000) ($x_6$, 123000)
                ($x_7$, 112000) ($x_8$, 111000) ($x_9$, 110000)
                ($x_{10}$, 110000) ($x_{11}$, 120000)
                ($x_{12}$, 98000) ($x_{13}$, 130000)
                ($x_{14}$, 87000) ($x_{15}$, 97000)};
          \end{axis}
        \begin{axis}[
            ybar,
            ylabel={weight},
            ylabel style={at={(1.25,0.5)}, color=blue},
            legend style={at={(0.7,-0.15)},
            anchor=north,legend columns=-1},
            width=0.8*\textwidth,
            height=9cm,
            bar width=7pt,
             symbolic x coords={
                $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$,
                $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
                 $x_{14}$, $x_{15}$
            },
            xtick=data,
            %nodes near coords,
            %nodes near coords align={vertical},
            axis y line*=right,
             axis x line=none,
         ]
             \addplot +[shift={(\shift,0)}] coordinates {($x_1$,27)
                 ($x_2$,21) ($x_3$,27) ($x_4$,15) ($x_5$,19)
                 ($x_6$, 46) ($x_7$, 47) ($x_8$, 32) ($x_9$, 14)
                 ($x_{10}$, 20) ($x_{11}$, 50) ($x_{12}$, 19)
                 ($x_{13}$, 22) ($x_{14}$, 50) ($x_{15}$, 46)};
        \end{axis}
     \end{tikzpicture}
    \end{document}

谢谢。

答案1

这是否接近您的预期?此处的解决方案对后续内容做了一些更改。也许x_i应该更改为y_i,但此解决方案保留了它们。

  1. ybar 到 xbar。
  2. 交换(x,y)坐标。
  3. symbolic x coordinatessymbolic y coordinate
  4. 形成。xtick=dataytick=data
  5. 形成。shift=(x,y)shift=(y,x)

在此处输入图片描述

代码

\documentclass[varwidth=true, border=10pt]{standalone}

\usepackage{pgfplots}
\usepackage{tikz,graphicx}
\usetikzlibrary{arrows, positioning, calc,patterns}

\begin{document}
\newcommand\shift{1.5}
\begin{tikzpicture}[scale=1]
    \begin{axis}[
        xbar,enlarge x limits=0.15,
        xlabel={value},
        xlabel style={at={(0.5,1.12)}, anchor=south,color=red},
        ylabel={items},
        legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},
        width=0.8*\textwidth,
        height=9cm,
        bar width=7pt,
        symbolic y coords={
        $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$, $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
            $x_{14}$, $x_{15}$
        },
        ytick=data,
        scaled x ticks=base 10:-3,
        axis y line*=left,
        axis x line*=top,
        %nodes near coords,
        %nodes near coords align={vertical},          
     ]


 \addplot[red, fill=red!15,shift={(0,-\shift)}] coordinates {
                (96000,$x_1$) (126000,$x_2$) (115000,$x_3$)
                (125000,$x_4$) (123000,$x_5$) (123000,$x_6$)
                (112000,$x_7$) (111000,$x_8$) (110000,$x_9$)
                (110000,$x_{10}$) (120000,$x_{11}$)
                (98000,$x_{12}$) (130000,$x_{13}$)
                (87000,$x_{14}$) (97000,$x_{15}$)};
  \end{axis}

  \begin{axis}[
            xbar,
            xlabel={weight},
            xlabel style={at={(0.5,-0.01)}, color=blue},
            legend style={at={(0.7,0.15)},
            anchor=south,legend columns=-1},
            width=0.8*\textwidth,
            height=9cm,
            bar width=7pt,
            symbolic y coords={
                $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$, $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
                 $x_{14}$, $x_{15}$
            },
            ytick=data,
            %nodes near coords,
            %nodes near coords align={vertical},
            axis y line*=right,
            axis x line*=none,
     ]
 \addplot +[shift={(0,\shift)}] coordinates {(27,$x_1$)
                 (21,$x_2$) (27,$x_3$) (15,$x_4$) (19,$x_5$)
                 (46,$x_6$) (47,$x_7$) (32,$x_8$) (14,$x_9$)
                 (20,$x_{10}$) (50,$x_{11}$) (19,$x_{12}$)
                 (22,$x_{13}$) (50,$x_{14}$) (46,$x_{15}$)};
 \end{axis}
 \end{tikzpicture}
 \end{document}

相关内容