1

A follow up question to my previous question: I am trying to plot a simple bar chart. The data is read and sorted from a csv file. However I couldn't able to change the style of particular labels (nodes near coords) in my plot.

Currently what I am doing:

\documentclass[11pt]{article}

\begin{filecontents*}{data.csv}
ags,uti
Fa,0.7304292882
At,0.7160872752
My,0.7003791749
YX,0.7582041393
Cat,0.7333942111
\textbf{EO15},0.7265476631
\textbf{EC15},0.7587056233
\textbf{DO15},0.7208403111
\textbf{DC15},0.7593885174
\end{filecontents*}

\usepackage{pgfplots}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{data.csv}\results
\pgfplotstablesort[sort key={uti}]{\utilsortedresults}{\results}

\begin{document}

\begin{figure}[]
\centering  
  \begin{center}
  \begin{tikzpicture} 
  \begin{axis}[
  xbar,
  yticklabels from table={\utilsortedresults}{ags},
  xticklabel style={
        /pgf/number format/fixed,
        /pgf/number format/precision=3
  },
  xmin=0.65,
  width=12cm, 
  height=7cm,
  enlarge y limits=0.1, 
  xticklabel style = {rotate=45,anchor=north east},
  ytick=data, nodes near coords, nodes near coords align={horizontal}, 
  every node near coord/.append style={
        /pgf/number format/fixed zerofill,
        /pgf/number format/precision=3
  },
  ] 

  \addplot table[y expr=\coordindex, x={uti}, col sep=comma] \utilsortedresults {};

  \end{axis} 
  \end{tikzpicture}
  \end{center}
\end{figure}
\end{document}

Outputs:

Output

What I am trying to achieve is: making circled labels(as shown below) bold:

Expected

  • 2
    It would be great if you could make a complete example that doesn't rely on other files we don't have (i.e. your data file). – Torbjørn T. Jun 2 '17 at 13:02
  • I changed the example, it should generate the same plot without relying on other files. – tdgunes Jun 2 '17 at 14:33
2

If editing the CSV is a possibility here, then I'd add a new column with the value 1 where the ticklabel and node near coord should be bold, and 0 elsewhere. You can then use that to decide the formatting of both.

enter image description here

\documentclass[11pt]{article}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
ags,uti,bold
Fa,0.7304292882,0
At,0.7160872752,0
My,0.7003791749,0
YX,0.7582041393,0
Cat,0.7333942111,0
EO15,0.7265476631,1
EC15,0.7587056233,1
DO15,0.7208403111,1
DC15,0.7593885174,1
\end{filecontents*}

\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{data.csv}\results
\pgfplotstablesort[sort key={uti}]{\utilsortedresults}{\results}

\begin{document}

\begin{figure}
\centering  
  \begin{tikzpicture} 
  \begin{axis}[
  xbar,
  ytick=data,
  yticklabel={%
   % \ticknum is the tick number, counting from 0
   % \pgfplotstablegetelem gets a value from the given table,
   % using row number and column name, and saves it to \pgfplotsretval
   \pgfplotstablegetelem{\ticknum}{bold}\of\utilsortedresults
   \ifnum \pgfplotsretval = 1
     \pgfplotstablegetelem{\ticknum}{ags}\of\utilsortedresults
     \textbf{\pgfplotsretval}
    \else
     \pgfplotstablegetelem{\ticknum}{ags}\of\utilsortedresults
     \pgfplotsretval
    \fi},
  xmin=0.65,
  xmax=0.775,
  width=12cm, 
  height=7cm,
  enlarge y limits=0.1, 
  xticklabel style = {
        /pgf/number format/fixed,
        /pgf/number format/fixed zerofill, % assuming you want that
        /pgf/number format/precision=3,
        rotate=45,
        anchor=north east},
  nodes near coords={%
    % same as above, only using \coordindex instead of \ticknum
    \pgfplotstablegetelem{\coordindex}{bold}\of\utilsortedresults
    \ifnum \pgfplotsretval = 1
       {\boldmath\pgfmathprintnumber[precision=3]{\pgfplotspointmeta}}
    \else
            \pgfmathprintnumber[precision=3]{\pgfplotspointmeta}
    \fi
},
  nodes near coords align={horizontal}, 
  every node near coord/.append style={
        /pgf/number format/fixed zerofill,
        /pgf/number format/precision=3
  },
  ] 

  \addplot table[y expr=\coordindex, x={uti}, col sep=comma] \utilsortedresults {};

  \end{axis} 
  \end{tikzpicture}
\end{figure}
\end{document}
  • Is there a way to achieve this with using key names, for instance giving a list ["Fa", "EO15"]? Another thought is: It'd be very helpful if this can be done by directly from the csv file. For example, marking the key with star perhaps. – tdgunes Jun 3 '17 at 4:19
  • @tdgunes You said in your question that you would prefer to do it without editing the csv file ... But see edited answer for a suggestion that does involve editing the file. – Torbjørn T. Jun 3 '17 at 5:47
  • Sorry, solution didn't feel elegant enough. From csv file, it seems much more convenient now. – tdgunes Jun 3 '17 at 14:02

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.