pfgplot 長條圖的變化

pfgplot 長條圖的變化

我在 tex.stackexchange 上為 pgfplots 找到了很多很多有用的東西 - 謝謝!

但我不知道如何:

  • 在“without”或“with”後插入新行
  • 隱藏值 0 或整條
  • 更改 x 刻度,這樣就沒有小數次冪 ([10^..])
  • 防止圖表外的 beeinplottet 座標附近的節點

兩個圖表:

堆積長條圖的變化

和原始碼:

\documentclass[a4paper]{report}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\title{Tests}

\pgfplotsset{counter_barchart/.style={
  width=0.85\textwidth,
  height=5cm,
  xbar,
  xmin=0,
  xmajorgrids = true,
  tick align = outside, xtick pos = left,
  x tick label style={ /pgf/number format/1000 sep=},
  enlarge y limits=0.4,
  symbolic y coords={Ass. Optimization,with Optimization,without Optimization},
  ytick=data,
  yticklabel style={text width=0.2\textwidth},
  every node near coord/.append style={/pgf/number format/1000 sep=},
  nodes near coords,
  nodes near coords align={horizontal},
  legend style={at={(0.5,-0.35)},anchor=north,legend columns=-1},
  reverse legend
}}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(0,Ass. Optimization)(19243,with Optimization) (8898,without Optimization)};
  \addplot coordinates {(7854,Ass. Optimization) (6652,with Optimization) (6548,without Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}
\\

\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(3985,without Optimization) (5456,with Optimization)};
  \addplot coordinates {(5223,without Optimization) (11054,with Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\end{document}

答案1

為了避免 中的連字號yticklabel

yticklabel style={text width=0.2\textwidth,align=flush left},

將值 0 隱藏為座標附近的節點:

nodes near coords={%
  \pgfmathtruncatemacro\NNC{\pgfkeysvalueof{/data point/x}}%
  \ifnumequal{\NNC}{0}{}{\NNC}% needs package etoolbox
},

為了避免 xtick 縮放:

scaled x ticks=false,

為了防止座標附近的節點被繪製在圖表之外:

enlarge x limits={0.15,upper},

在此輸入影像描述

代碼:

\documentclass[a4paper]{report}

\usepackage{etoolbox}
\usepackage{pgfplots}

\pgfplotsset{
  counter_barchart/.style={
    width=0.85\textwidth,
    height=5cm,
    xbar,
    xmin=0,
    xmajorgrids = true,
    tick align = outside, xtick pos = left,
    x tick label style={/pgf/number format/1000 sep=},
    scaled x ticks=false,
    enlarge y limits=0.4,
    enlarge x limits={0.15,upper},
    symbolic y coords={Ass. Optimization,with Optimization,without Optimization},
    ytick=data,
    yticklabel style={text width=0.2\textwidth,align=flush left},
    nodes near coords={%
      \pgfmathtruncatemacro\NNC{\pgfkeysvalueof{/data point/x}}%
      \ifnumequal{\NNC}{0}{}{\NNC}%
    },
    nodes near coords align={horizontal},
    legend style={at={(0.5,-0.35)},anchor=north,legend columns=-1},
    reverse legend
  }
}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[counter_barchart,xtick={0,4000,8000,12000,16000,20000}]
  \addplot coordinates {(0,Ass. Optimization)(19243,with Optimization) (8898,without Optimization)};
  \addplot coordinates {(7854,Ass. Optimization) (6652,with Optimization) (6548,without Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\vspace{1cm}
\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(3985,without Optimization) (5456,with Optimization)};
  \addplot coordinates {(5223,without Optimization) (11054,with Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\end{document}

相關內容