为条形组添加背景

为条形组添加背景

由于我的图表很大,我想为每个条形组添加变化的背景。该怎么做?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[table,xcdraw,rgb,svgnames]{xcolor}
\usepackage{tikz, pgfplots}
\usetikzlibrary{shapes,shapes.multipart,arrows,positioning,matrix,fit,backgrounds,calc}

\begin{document}

\begin{tikzpicture}
    \tikzset{font=\tiny}
    \pgfplotstableread{%
    x   chromepc-rust   chromepc-go firefoxpc-rust  firefoxpc-go    chromemobile-rust
    Base64      92.85   80.45       87.91   47.39       84.82
    Sort        59.08   -63.30      76.24   32.35       45.22
    SHA256      81.58   -17.53      83.74   25.81       92.75
    SHA512      97.98   88.61       97.91   92.92       94.74
    AES     -12.85  30.10       35.62   67.23       24.54
    #Deflate        47.73   -85.00      11.92   -167.55     32.38
    #Bild       11.79   -228.30     9.89    -234.62     16.07
    #Video      29.20   -73.89      62.39   -71.62      72.52
    #DOM        -159.84 -752.46     -138.10 -1734.92        -91.05
    }\mytableBaseA
\begin{axis}[
    axis line style={draw=none},% hide lines
    tick style={draw=none},% hide tick lines
    hide x axis,
    xmin = -100,
    xmax = 120,
    xbar, % bar chart
    y dir=reverse,%reverse y data
    y = 25mm,
    /pgf/number format/.cd,%change context to number format
    use comma,%use comma instead of point
    1000 sep={.},%use point instead of comma
    fixed,
    ytick = data,% x-labels = x-column
    enlarge y limits = {abs=16mm}, % space between border and outer bars origin
    symbolic y coords = {Base64, Sort, SHA256, SHA512, AES, Deflate, Bild, Video, DOM},% Use as x values
    nodes near coords={\pgfmathprintnumber[fixed,fixed zerofill,precision=0,print sign]\pgfplotspointmeta~\%}, % values above barss = outer north east,
    legend cell align={left},
    reverse legend,
]
    \addplot[draw=none, fill=black!33, area legend] table[y=x,x=chromemobile-rust] {\mytableBaseA};
    \addplot[draw=none, fill=MediumTurquoise!50, area legend] table[y=x,x=firefoxpc-go] {\mytableBaseA};
    \addplot[draw=none, fill=black!66, area legend] table[y=x,x=firefoxpc-rust] {\mytableBaseA};
    \addplot[draw=none, fill=MediumTurquoise, area legend] table[y=x,x=chromepc-go] {\mytableBaseA};
    \addplot[draw=none, fill=black, area legend] table[y=x,x=chromepc-rust] {\mytableBaseA};
    \legend{ChromePC Rust, ChromePC Go, FirefoxPC Rust, FirefoxPC Go, ChromeMobile Rust}
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我找到了一种解决方法,即使用网格线作为背景。这样它就不会改变背景 - 它始终是灰色的。不是很好,但到目前为止看起来还不错。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[table,xcdraw,rgb,svgnames]{xcolor}
\usepackage{tikz, pgfplots}
\usetikzlibrary{shapes,shapes.multipart,arrows,positioning,matrix,fit,backgrounds,calc}

\begin{document}

    \begin{tikzpicture}
        \tikzset{font=\tiny}
        \pgfplotstableread{%
        x   chromepc-rust   chromepc-go firefoxpc-rust  firefoxpc-go    chromemobile-rust
        Base64      92.85   80.45       87.91   47.39       84.82
        Sort        59.08   -63.30      76.24   32.35       45.22
        SHA256      81.58   -17.53      83.74   25.81       92.75
        SHA512      97.98   88.61       97.91   92.92       94.74
        AES     -12.85  30.10       35.62   67.23       24.54
        Deflate     47.73   -85.00      11.92   -167.55     32.38
        Bild        11.79   -228.30     9.89    -234.62     16.07
        Video       29.20   -73.89      62.39   -71.62      72.52
        DOM     -159.84 -752.46     -138.10 -1734.92        -91.05
        }\mytableBaseA
    \begin{axis}[
        axis line style={draw=none},% hide lines
        tick style={draw=none},% hide tick lines
        xmax = 160,
        xmin = -220,
        xbar, % bar chart
        y dir=reverse,%reverse y data
        y = 18mm,
        bar width = 2mm,
        /pgf/number format/.cd,%change context to number format
        use comma,%use comma instead of point
        1000 sep={.},%use point instead of comma
        fixed,
        ytick = data,% x-labels = x-column
        extra x ticks = {0},% tick line at position
        extra x tick labels = {},
        extra x tick style = {grid = major},% add grid line for x tick
        enlarge y limits = {abs = 10mm}, % space between border and outer bars origin
        symbolic y coords = {Base64, Sort, SHA256, SHA512, AES, Deflate, Bild, Video, DOM},% Use as x values
        ymajorgrids = true,
        y grid style={% workaround for bar groups background
            line width = 50pt,
            draw = black!3,
        },
        restrict x to domain*=-150:150,
        visualization depends on=rawx\as\rawx, % Save the unclipped values
        nodes near coords={%
            \pgfmathprintnumber[fixed,fixed zerofill,precision=0,print sign]{\rawx}~\%
        },
        legend pos = outer north east,
        legend cell align={left},
        reverse legend,
    ]
        \addplot[draw=none, fill=MediumTurquoise!50, area legend] table[x=firefoxpc-go, y=x] {\mytableBaseA};
        \addplot[draw=none, fill=MediumTurquoise, area legend] table[x=chromepc-go, y=x] {\mytableBaseA};
        \addplot[draw=none, fill=black!33, area legend] table[x=chromemobile-rust, y=x] {\mytableBaseA};
        \addplot[draw=none, fill=black!66, area legend] table[x=firefoxpc-rust, y=x] {\mytableBaseA};
        \addplot[draw=none, fill=black, area legend] table[x=chromepc-rust, y=x] {\mytableBaseA};
        \legend{FirefoxPC Go, ChromePC Go, ChromeMobile Rust, FirefoxPC Rust, ChromePC Rust}
        \end{axis}
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容