2

I am a newbie with pgfplots so, after searching on internet and on pdf manuals, I am asking here. I need to reproduce with LaTeX some graphics like this one I made with Excel, can anyone help me with the code? graphic excel-like

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Moreover, there are many posts about histogram here, have you searched? – CarLaTeX Mar 19 '17 at 10:48
  • 1
    You will find a lot of answers on this site for your problem when you search for [pgfplots][bar-chart] ... which brings up a result like tex.stackexchange.com/questions/101320/grouped-bar-chart which should pretty much answer all your needs, right? – Stefan Pinnow Mar 19 '17 at 11:11
2

You can find many plots here. For your example you have to set some option. (The used percent sign solution for ticklabel you can find here.)

Modifications:

yticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
xtick = data,
ytick={0,10,...,60},
enlarge y limits=0.0,
ymax=60,
legend style={at={(0.5,-0.15)}, 
anchor=north,legend columns=-1}, 
ybar=5pt,
bar width=10pt, 
symbolic x coords={PC,PML,RR,PN},
ymajorgrids,
ticklabel style = {color=gray},% or opacity=0.5
axis line style={draw=none},
tick style={draw=none},
legend style={draw=none,text=gray}
 ]

Redefinition of the legend image code for a single ybar legend:

\pgfplotsset{compat=1.14,
    /pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
       \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},
   },
}

Solution enter image description here

MWE:

 \documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
%http://pgfplots.sourceforge.net/gallery.html

\pgfplotsset{compat=1.14,
    /pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
       \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},
   },
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
yticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
xtick = data,
ytick={0,10,...,60},
enlarge y limits=0.0,
ymax=60,
legend style={at={(0.5,-0.15)}, 
anchor=north,legend columns=-1}, 
ybar=5pt,
bar width=10pt, 
symbolic x coords={PC,PML,RR,PN},
ymajorgrids,
ticklabel style = {color=gray},% or opacity=0.5
axis line style={draw=none},
tick style={draw=none},
legend style={draw=none,text=gray}
 ]
 \addplot[blue,fill=blue] coordinates {(PC,52) (PML,5) (RR,0) (PN,42) };
 \addplot[yellow,fill=yellow] coordinates {(PC,38) (PML,8) (RR,2) (PN,52) }; 
 \legend{A,B} 
 \end{axis} 
 \end{tikzpicture}
\end{document}

UPDATE: You can use width and long text like PC personal computer.Mabe you will rotate (rotate=0 or rotate=45) the labels. Then you have to change the anchor=north east. Maybe optional xshift... :

symbolic x coords={PC personal computer,PML,RR,PN},
xticklabels   = {PC personal computer, PML, RP, PN},
x tick label style={rotate=0, anchor=north, inner sep=0mm,text width=1.5cm,align=center}
 ]
 \addplot[blue,fill=blue] coordinates {(PC personal computer,52) (PML,5) (RR,0) (PN,42) };

Result:

    \documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14,
    /pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
       \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},
   },
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
yticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
xtick = data,
ytick={0,10,...,60},
enlarge y limits=0.0,
ymax=60,
legend style={at={(0.5,-0.15)}, 
anchor=north,legend columns=-1}, 
ybar=5pt,
bar width=10pt, 
symbolic x coords={PC personal computer,PML,RR,PN},
ymajorgrids,
ticklabel style = {color=gray},% or opacity=0.5
axis line style={draw=none},
tick style={draw=none},
legend style={draw=none,text=gray},
xticklabels   = {PC personal computer, PML, RP, PN},
x tick label style={rotate=0, anchor=north, inner sep=0mm,text width=1.5cm,align=center}
 ]
 \addplot[blue,fill=blue] coordinates {(PC personal computer,52) (PML,5) (RR,0) (PN,42) };
 \addplot[yellow,fill=yellow] coordinates {(PC personal computer,38) (PML,8) (RR,2) (PN,52) }; 
 \legend{A,B} 
 \end{axis} 
 \end{tikzpicture}
\end{document}

enter image description here

Or with x tick label style={rotate=45, anchor=north east, inner sep=0mm,text width=1.5cm,align=center}.

|improve this answer|||||
  • Thanks a lot, this worked perfectly for me. Now I'd like to explain those abbreviations (PC, PML, RR, etc), can you suggest me a way to do it and the related code? For example PC=personal computer, etc. I kind of second legend... I can't use directly the complete descriptions under the x-axis in the graphic because they are too long. – Enrico Mar 21 '17 at 18:05
  • Maybe something like this (with linebreak?) tex.stackexchange.com/a/51030/124842 or tex.stackexchange.com/a/35886/124842 – Bobyandbob Mar 21 '17 at 19:32
  • I prefer the first link you gave me, but I have two "items" (yellow and blue ones) for each value, so I don't understand how to do that. I have tried in different ways, but it doesn't work. Can you help me with the code? Thanks – Enrico Mar 22 '17 at 10:59
  • I added a update. I think you have to find your solution for your own... I hope it will help. – Bobyandbob Mar 22 '17 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.