0

I want to have label each bar of a horizontal bar chart with its corresponding y value. However, I am only able to get the numeric representation but not the actual symbolic value.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[ xbar, axis y line=center, axis x line=center, bar width=4pt, bar shift=0pt, %% <-- added
y=4pt, visualization depends on={y \as \rawy}, nodes near coords={\rawy},, nodes near coords align={horizontal}, nodes near coords style={font=\tiny}, symbolic y coords={A,B,C,D,E,F}, yticklabels=\empty, ] \addplot[fill=blue] coordinates { (-58,A) (-45,B) (-43,C) };

  \addplot[fill=red] coordinates {
    (19,D)
    (35,E)
    (65,F)
  };
\end{axis}

\end{tikzpicture} \end{document}

This is the result with the presumably internal representations for the different y ticks used as the labels instead of the intended symbolic values.

enter image description here

Sim
  • 240

1 Answers1

1

Edit (1):

  • If I correctly understood your question, in nodes near coordinate nodes like to have symbolic coordinates. Do I'm right?

Edit (2):

  • Your attempt to read data from rows doesn't work since your data are not organized as table (with rows and columns).
  • With collecting data in simple table (instead writing them by coordinates), the solution is quite simple.
  • Use of tables I see as an advantages and not as a disadvantages. I'm confident, that tables is simpler to automatic generate than collect coordinates.
\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis x line=left, axis y line=center, enlargelimits={lower, 0.2}, % xbar=11pt, bar shift=0pt,
% xtick={-80,-60,...,60}, ytick=\empty, y=11pt, % symbolic y coords={A,B,C,D,E,F}, point meta=explicit symbolic, nodes near coords, nodes near coords align={horizontal}, ] \addplot[fill=blue!70, nodes near coords style={font=\small, anchor=east}, ] table[meta=Y] { X Y -58 A -45 B -43 C }; \addplot[fill=red, nodes near coords style={font=\small, anchor=west}, ] table[meta=Y] {
X Y
19 D 35 E 56 F }; \end{axis} \end{tikzpicture} \end{document}

enter image description here

Zarko
  • 296,517
  • Hey, this does not answer my question. I want to have the symbolic values, i.e., A,B,C as the label of the bars. – Sim Feb 18 '22 at 14:38
  • However, you solved another question of mine by fixing those pesky margins. – Sim Feb 18 '22 at 14:38
  • aha, i just looking image in question. Coordinates name I will correct ASAP (late evening, now I'm going on a way now), for now I left answer as it is, and copy it to your other question. – Zarko Feb 18 '22 at 14:41
  • @Sim, see edited answer. Do I now correctly figured out what you after? – Zarko Feb 19 '22 at 00:09
  • yes you did (+1). However, is there a simpler solution than to define a table? I would like keep working with coordinates as it is easier to auto generate the required tex code. – Sim Feb 19 '22 at 13:02
  • @Simm see edit 2 of answer. What you after is not possible (at least with my knowledge) achieve by using of coordinates. – Zarko Feb 19 '22 at 14:27
  • you are probably right about the tables. However, my current code uses coordinates. I will have to bite that bullet I guess. – Sim Feb 19 '22 at 14:59