1

I would like to be able to order the data in an ascendingly. In the code below I have commented out what the order I would like to achieve. I have done this buy sorting using a speardsheet. But what I would like to do is sorting using a command in pgfplots.

\documentclass[border=10mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{pgfplotstable}

\begin{filecontents}{data.csv}
Manufacturer,BEV
Tesla Inc.,1314319
BYD, 913866
VW Group,578231
GM,568647
Stellantis,301577
Hyundai Motor,372092
BMW Group,218882
Geely Auto Group,265811
Mercedes-Benz Group,162909
R-N-M Alliance,259164
GAC,287599
SAIC,203205
Geely-Volvo Car Group,118029
Chery Automobile,235533
Changan Automobile Group,201589
Dongfeng Motor,222841
Ford,105544
Hozon Auto,149285
\end{filecontents}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
    ybar,
        width=18cm,
    height=12cm,
    bar width=0.5cm,
    xlabel={Manufacturer},
    ylabel={Number of vehicles},
    % symbolic x coords={Tesla Inc., BYD, VW Group, GM, Hyundai Motor, Stellantis, GAC, Geely Auto Group, R-N-M Alliance, Chery Automobile, Dongfeng Motor, BMW Group, SAIC, Changan Automobile Group, Mercedes-Benz Group, Hozon Auto, Geely-Volvo Car Group,  Ford},
    symbolic x coords={BYD, Tesla Inc., VW Group, GM, Stellantis, Hyundai Motor, BMW Group, Geely Auto Group, Mercedes-Benz Group, R-N-M Alliance, GAC, SAIC, Geely-Volvo Car Group, Chery Automobile, Changan Automobile Group, Other, Dongfeng Motor, Ford, Hozon Auto, CHJ Automotive},
    xtick=data,
    xticklabel style={rotate=45, anchor=east},
    nodes near coords,
    nodes near coords={},
    ymin=0,
]

\addplot table[col sep=comma, x=Manufacturer, y=BEV] {data.csv};

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

vs

enter image description here

3
  • By hand (as you already do for the second image) ...
    – Zarko
    Sep 1, 2023 at 12:05
  • Have you tried this option? tex.stackexchange.com/questions/198461/…
    – Jes
    Sep 1, 2023 at 14:22
  • I have tried the option in the link supplied by Jes, but doesn't seem to work using characters instead of numbers. Sorting by hand seems like the only option as @Zarko points out. Sep 1, 2023 at 16:11

1 Answer 1

3

I managed to achieve want I was trying to do with a slightly different approach. Sorting is perfect, I don't have to do it by hand anymore. Creating a single diagram manually is not an issue, but when you need to generate multiple diagrams, it becomes highly convenient to automate the process.

\documentclass[border=10mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.18} 

\pgfplotstableread[col sep=comma]{
Manufacturer,BEV
Tesla Inc.,1314319
BYD, 913866
VW Group,578231
GM,568647
Stellantis,301577
Hyundai Motor,372092
BMW Group,218882
Geely Auto Group,265811
Mercedes-Benz Group,162909
R-N-M Alliance,259164
GAC,287599
SAIC,203205
Geely-Volvo Car Group,118029
Chery Automobile,235533
Changan Automobile Group,201589
Dongfeng Motor,222841
Ford,105544
Hozon Auto,149285
}\data

\begin{document}

\pgfplotstablesort[sort key={BEV},sort cmp={int >}]\sorteddata{\data}

\begin{tikzpicture}
  \begin{axis}[
    ybar,
    width=18cm,
    height=12cm,
    bar width=0.5cm,
    xlabel={EV Manufacturer},
    ylabel={Number of vehicles},
    xtick=data,
    xticklabels from table={\sorteddata}{Manufacturer},
    xticklabel style={rotate=45,anchor=north east},
    nodes near coords,
    nodes near coords={},
    ymin=0,
    ]
    \addplot table[x expr=\coordindex, y=BEV] {\sorteddata};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1
  • 1
    Congratulation! +1 of course!
    – Zarko
    Sep 1, 2023 at 18:59

You must log in to answer this question.

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