2

I need decimal values (they are in the code but in the plot. they are getting as integers) for the values above the bars. Also, the x-axis legend is overlapping so I need it in 2 lines. I mean each x legend in two lines. For example, for "total time", time should come in next line

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
  \centering
  \begin{axis}[
        ybar, axis on top,
        title={cb},
        height=8cm, width=15.5cm,
        bar width=0.35cm,
        ymajorgrids, tick align=inside,
        major grid style={draw=white},
        enlarge y limits={value=.1,upper},
        ymin=0, ymax=30,
        axis x line*=bottom,
        axis y line*=right,
        y axis line style={opacity=0},
        tickwidth=0pt,
        enlarge x limits=true,
        legend style={
            at={(0.5,0.6)},
            anchor=north,
            legend columns=-1,
            /tikz/every even column/.append style={column sep=0.5cm}
        },
        ylabel={Computational time ($\mu$ s )},
        symbolic x coords={
           ramesh india, ramu england, rama america, raman poland, ram ukrain, ramani nedherlands, total time},
       xtick=data,
       nodes near coords={
        \pgfmathprintnumber[precision=0]{\pgfplotspointmeta}
       }
    ]
    \addplot [draw=none, fill=blue!30] coordinates {
      (ramesh india,8)
      (ramu england, 1) 
      (rama america,5.5)
      (raman poland,0) 
      (ram ukrain,0) 
      (ramani nedherlands,2)
      (total time,16.5) };
   \addplot [draw=none,fill=red!30] coordinates {
    (ramesh india,8)
    (ramu england, 1) 
    (rama america,5)
    (raman poland,7.5) 
    (ram ukrain,3) 
    (ramani nedherlands,2)
    (total time,25.5) };
   \addplot [draw=none, fill=brown!30] coordinates {
    (ramesh india,8)
    (ramu england, 1) 
    (rama america,5)
    (raman poland,7.5) 
    (ram ukrain,3) 
    (ramani nedherlands,2)
    (total time,28.5) };
    \legend{C, M, P}
  \end{axis}
  \end{tikzpicture}
\end{document}
6
  • 3
    In addition to the code, can you describe what the problem is? Commented Feb 13, 2022 at 10:32
  • thanks a lot. I need decimal values (they are in the code but in the plot. they are getting as integers) for the plot. Also, the x-axis legend is overlapping so I need it in 2 rows. I mean each x legend in two rows. For example, for total time, time should come in next line.
    – Siva Kumar
    Commented Feb 13, 2022 at 10:37
  • Please edit the question to include that information, don't just write it in a comment :) However, do you mean the numbers on top of the bars? You have explicitly told pgfplots to use integers there with precision=0 ... Commented Feb 13, 2022 at 10:41
  • yes, I need decimal values on top of the bars. please guide me to change the precision command
    – Siva Kumar
    Commented Feb 13, 2022 at 10:42
  • I repeat, please edit the question to include a description of the changes you want. There should be an edit link just above these comments. I don't understand what guide you need, the number after precision= says how many decimals to add, so start by trying to change that value. Commented Feb 13, 2022 at 10:45

2 Answers 2

3

With \pgfmathprintnumber[precision=0]{\pgfplotspointmeta} you're saying "print this number with zero decimals". If you say precision=1, you'll get one decimal. As you found, you'll get the same result by removing [precision=0] altogether.

By the way, if you want e.g. 8.0 instead of 8, you'll need \pgfmathprintnumber[zerofill, precision=1]{\pgfplotspointmeta}, but it seems you didn't want that.

Regarding the xticklabels, you can add xticklabel style={align=center} to the axis options, and then add \\ in the symbolic coordinats where you want line breaks.

I also made the bars a bit wider to make room for the labels.

Unrelated note: having a \centering inside a tikzpicture doesn't make much sense. The size of a tikzpicture is determined by the content, the \centering should be before \begin{tikzpicture}.

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
        ybar, axis on top,
        title={cb},
        height=8cm, width=15.5cm,
        bar width=0.45cm, % <-- modified
        ymajorgrids, tick align=inside,
        major grid style={draw=white},
        enlarge y limits={value=.1,upper},
        ymin=0, ymax=30,
        axis x line*=bottom,
        axis y line*=right,
        y axis line style={opacity=0},
        tickwidth=0pt,
        enlarge x limits=true,
        xticklabel style={align=center}, % <-- added
        legend style={
            at={(0.5,0.6)},
            anchor=north,
            legend columns=-1,
            /tikz/every even column/.append style={column sep=0.5cm}
        },
        ylabel={Computational time ($\mu$ s )},
        symbolic x coords={
           ramesh\\india, ramu\\england, rama\\america, raman\\poland, ram\\ukrain, ramani\\nedherlands, total time},
       xtick=data,
       nodes near coords={
        \pgfmathprintnumber{\pgfplotspointmeta}
       }
    ]
    \addplot [draw=none, fill=blue!30] coordinates {
      (ramesh\\india,8)
      (ramu\\england, 1) 
      (rama\\america,5.5)
      (raman\\poland,0) 
      (ram\\ukrain,0) 
      (ramani\\nedherlands,2)
      (total time,16.5) };
   \addplot [draw=none,fill=red!30] coordinates {
    (ramesh\\india,8)
    (ramu\\england, 1) 
    (rama\\america,5)
    (raman\\poland,7.5) 
    (ram\\ukrain,3) 
    (ramani\\nedherlands,2)
    (total time,25.5) };
   \addplot [draw=none, fill=brown!30] coordinates {
    (ramesh\\india,8)
    (ramu\\england, 1) 
    (rama\\america,5)
    (raman\\poland,7.5) 
    (ram\\ukrain,3) 
    (ramani\\nedherlands,2)
    (total time,28.5) };

    \legend{C, M, P}
  \end{axis}
  \end{tikzpicture}
  
\end{document}
2
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
  \centering
\begin{axis}[
    height=8cm, width=15.5cm,
    ybar=1mm, axis on top,
    title={cb},
    bar width=5mm,
    tick align=inside,
    enlarge y limits={value=.1,upper},
    ymin=0, ymax=30,
    axis x line*=bottom,
    axis y line*=right,
    enlarge x limits=true,
    legend style={
        at={(0.5,0.6)},
        anchor=north,
        legend columns=-1,
    /tikz/every even column/.append style={column sep=0.5cm}
    },
    ylabel={Computational time ($\mu$ s )},
    symbolic x coords={
       ramesh india, ramu england, rama america, raman poland, ram ukrain, ramani nedherlands, total time},
   xtick=data,
x tick label style = {rotate=30, anchor=east, yshift=-2ex,
                      align=right, font=\footnotesize},
   nodes near coords={
   \pgfmathprintnumber[fixed zerofill, precision=1] % <---
   {\pgfplotspointmeta}
   },
   nodes near coords style = {font=\footnotesize}
]
\addplot [draw=none, fill=blue!30] coordinates {
  (ramesh india,8)
  (ramu england, 1)
  (rama america,5.5)
  (raman poland,0)
  (ram ukrain,0)
  (ramani nedherlands,2)
  (total time,16.5) };
\addplot [draw=none,fill=red!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,25.5) };
\addplot [draw=none, fill=brown!30] coordinates {
(ramesh india,8)
(ramu england, 1)
(rama america,5)
(raman poland,7.5)
(ram ukrain,3)
(ramani nedherlands,2)
(total time,28.5) };
\legend{C, M, P}
\end{axis}
  \end{tikzpicture}
\end{document}

produce:

enter image description here

2
  • And you can add fixed zerofill to get all these number as decimals Commented Feb 13, 2022 at 11:52
  • @AndrewSwann, I overlooked this requirement. Thank you very much for pointing me on this. Now is added to answer.
    – Zarko
    Commented Feb 13, 2022 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .