TransWikia.com

Is there a simpler way to define a style for line and bar graphs and activate them together in pgfplots?

TeX - LaTeX Asked on December 31, 2021

I want to define different styles for line plots and bar graphs in pgfplots, that can later be used independently or together, depending on the users choice. I therefore defined two cycle lists for each style, called *Bar and *Line. To load both at the same time, I then define another style called * and "automated" this step already through the defineStyle macro.

Is there are more elegant way, to create the style, because as you see, I am using all the same settings (marks, filling, etc.) for every bar, so is there a neat way to iterate over a list of colors and create all the three styles with a single call to a macro? Something like createStyle{color}{blue,orange} and createStyle{mono}{black,gray} that works with an arbitrary number of colors that creates the mono and correspondig monoBar and monoLine styles? Maybe there is a more PGF-like way of doing it, that I am overlooking?

documentclass[aspectratio=169]{beamer}

usepackage{pgfplots}
pgfplotsset{width=7cm,compat=1.15}

newcommand{defineStyle}[1]{pgfplotsset{#1/.style={/pgfplots/bar cycle list/.style={/pgfplots/cycle list name=#1Bar}, cycle list name=#1Line}}}

pgfplotscreateplotcyclelist{colorLine}{blue,orange}
pgfplotscreateplotcyclelist{colorBar}{{fill=blue,draw=none,mark=none},{fill=orange,draw=none,mark=none}}
defineStyle{color}


pgfplotscreateplotcyclelist{monoLine}{black,gray}
pgfplotscreateplotcyclelist{monoBar}{{{fill=black,draw=none,mark=none},{fill=gray,draw=none,mark=none}}}
defineStyle{mono}


begin{document}
    pgfplotsset{color}
    
    begin{frame}<1>[label=pgfplot]
    frametitle<1>{color}
    frametitle<2>{mono}
    centering
    begin{tikzpicture}
    begin{axis}[xmin=0,xmax=11,ymin=0,ymax=9,samples=2,domain=0:10,stack plots=y]
    addplot {0.1*x};
    addplot {0.1*x};
    addplot {0.1*x};
    addplot {0.1*x};
    addplot {0.1*x};
    addplot {0.1*x};
    addplot {0.1*x};
    end{axis}
    end{tikzpicture}
    hfill
    begin{tikzpicture}
    begin{axis}[ybar,xmin=-0.5,xmax=2.75,ymin=0,ymax=11,xtick={0,1,...,2},xticklabels={test1,test2,test3},bar width=4pt]
    addplot coordinates{(0,3) (1,10) (2,9)};
    addplot coordinates{(0,7) (1,9) (2,7)};
    addplot coordinates{(0,5) (1,1) (2,4)};
    addplot coordinates{(0,7) (1,2) (2,3)};
    addplot coordinates{(0,4) (1,7) (2,7)};
    addplot coordinates{(0,2) (1,5) (2,6)};
    addplot coordinates{(0,3) (1,4) (2,8)};
    end{axis}
    end{tikzpicture}
end{frame}

pgfplotsset{mono}
againframe<2>{pgfplot}
end{document}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP