双面条形图

双面条形图

我努力制作一个条形图来显示支持和反对不同主题的意见。

虚构示例民意调查

我尝试使用 pgfplots 从条形图和间隔条形图开始执行此操作。可以使用负值和选项存档两侧的条形图forget plot。剩下的主要问题是分割 x 轴,以便标签位于负条形图和正条形图之间。长标签的自动换行将是一个不错的额外功能。

上面的图像模型是我使用 TikZ 制作的。这是代码,也许这会给任何人带来启发。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, snakes, scopes}

\begin{document}
\begin{tikzpicture}
  { [align=center]  % labels
    \node (a) at (0, 0) {short l.};
    \node (b) at (0, 1) {medium labels};
    \node (c) at (0, 2) {very enormously \\ excessively long labels};
  }

  % invisible node surrounding all labels
  \node [fit={(a) (b) (c)}] (center) {};

  % axis labels
  \node (title) at (0, -1.5) {Opinions};
  \node at (3, -1) {likes};
  \node at (-3, -1) {dislikes};

  { [yshift=-.25cm]  % named coordinates below as anchors
    \coordinate (east) at (center.south east) {};
    \coordinate (west) at (center.south west) {};
  }

  { [minimum height=5ex]  % bars
    { [every node/.style={fill=teal, anchor=west}]  % positives
      \node [minimum width=2cm] at (a-|east) {};
      \node [minimum width=6cm] at (b-|east) {};
      \node [minimum width=4cm] at (c-|east) {};
    }
    { [every node/.style={fill=purple, anchor=east}]   % negatives
      \node [minimum width=4cm] at (a-|west) {};
      \node [minimum width=3cm] at (b-|west) {};
      \node [minimum width=1cm] at (c-|west) {};
    }
  }

  % axes
  \draw[->] (east) -- ++(6.25, 0);
  \draw[snake=ticks,segment length=1cm, segment amplitude=1.75] (east) -- ++(6, 0);
  \draw[->] (west) -- ++(-4.25, 0);
  \draw[snake=ticks,segment length=1cm, segment amplitude=1.75] (west) -- ++(-4, 0);

  { [every node/.style={yshift=-2ex}] % axis labels
    \node at (east) {0};
    \node[xshift=2cm] at (east) {2};
    \node[xshift=4cm] at (east) {4};
    \node[xshift=6cm] at (east) {6};
    \node at (west) {0};
    \node[xshift=-2cm] at (west) {2};
    \node[xshift=-4cm] at (west) {4};
  }
\end{tikzpicture}
\end{document}

可以使用 pgfplots 制作这样的图表吗?或者有其他可以做到这一点的软件包吗?

答案1

pgfplots可以使用类似这样的方法(虽然不是自动化的,但仍然是一个解决方案)

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=like,
scale only axis,
xbar, xmin=0, xmax=40,
%xbar=2pt,
xlabel={likes},
width=5cm, height= 3cm,
ytick={1,2,3,4,5},
yticklabels={sa,safda,afa,afa,af},
y tick label style={text width=3cm,align=center},
axis x line=left,
axis y line=none,
clip=false
]
\addplot[green,fill=green] coordinates {
    (36,1) 
    (17,2) 
    (26,3)};
\node[xshift=-1.5cm,align=center] at (axis cs:0,3) {very enormously \\ excessively long\\ labels}; 
\node[xshift=-1.5cm,align=center] at (axis cs:0,2) {medium labels};
\node[xshift=-1.5cm,align=center] at (axis cs:0,1) {short l};
\end{axis}
\begin{axis}[ 
at={(like.north west)},anchor=north east, xshift=-3cm,
scale only axis,
xbar, xmin=0,xmax=40,
xlabel={dislikes},
ytick={1,2,3,4,5},
yticklabels={},
width=5cm, height= 3cm,
 x dir=reverse,
axis x line=left,
axis y line=none,
]
\addplot[red,fill=red] coordinates {
    (7,1) 
    (30,2) 
    (14,3)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是我使用的解决方案tikz。有些事情是自动化的,例如,制作一个酒吧就像输入一样简单。\like{1}{2cm};where一样简单喜欢(或不喜欢)是命令的名称,{1}指的是条形图的“高度”,1 表示最低,2 表示其上方的第二个条形图,依此类推。最后一个表示条形图将延伸多少。数字对应于图表上的实际节点(2cm = 最多到节点 2)。

条形图

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, snakes, scopes}

\tikzset{
  label/.style={align=center,text width=4.5cm,inner sep=1mm,outer sep=0mm,font=\footnotesize},
  }

\newcommand*{\like}[2]{
        \node[fill=teal, anchor=west, xshift=2cm, minimum width={#2}, minimum height=8mm] at (0,{#1}) {};
}

\newcommand*{\dislike}[2]{
        \node[fill=purple, anchor=east, xshift=-2cm, minimum width={#2}, minimum height=8mm] at (0,{#1}) {};
}

\begin{document}
\begin{tikzpicture}[y=1cm]

    \draw[|->, -latex, draw] (2,0) -- (10,0)  node[align=center,midway,yshift=-1cm]{Like};
    \draw[|->, -latex, draw] (-2,0) -- (-10,0)node[align=center,midway,yshift=-1cm]{Dislike};

        \foreach \x [evaluate=\x as \degree using int(0+\x)] in {0,2,...,6}{ 
            \draw (\x,0) node[below=7pt,anchor=north,xshift=2cm,font=\scriptsize] {$\degree$}; 
            \draw[xshift=2cm] (\x,-0.1) -- (\x,0.1); \draw[xshift=2cm] (\x+1,0) -- (\x+1,0.1);
            }

        \foreach \x [evaluate=\x as \degree using int(0-\x)] in {0,-2,...,-6}{ 
            \draw (\x,0) node[below=7pt,anchor=north,xshift=-2cm,font=\scriptsize] {$\degree$}; 
            \draw[xshift=-2cm] (\x,-0.1) -- (\x,0.1); \draw[xshift=-2cm] (\x-1,0) -- (\x-1,0.1);
            }
% labels

\node[label] at (0, 1) {short l.};
\node[label] at (0, 2) {medium labels};
\node[label] at (0, 3) {very very very enormously excessively long labels};

% bars      

\like{1}{2cm};
\like{2}{6cm};
\like{3}{4cm};

\dislike{1}{4cm};
\dislike{2}{5cm};
\dislike{3}{2cm};

\end{tikzpicture}
\end{document}

相关内容