Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using a tikzpicture for plotting. the values I want to plot are typical (X,Y) pairs. The x-value is an uninteresting id-value. I want to order the bins ascending/descending according to the y-value, but how do I do that? The figure seems to automatically take the x-values for ordering, how can I override that? (I am not particularly fixed to tikzpicture - its just the first thing that actually worked and displayed the plot....) Following minimal-working example, how could I achieve a reordering according to the y-value?

\documentclass{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{relsize}
\usepackage{times}
\usepackage{url}
\usepackage{latexsym}
\usepackage{graphicx}
\usepackage{colortbl}
\usepackage{color}
\usepackage{caption}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{arrows}
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{booktabs}

\usepackage{filecontents}
\definecolor{OgAns}{rgb}{0, 0.8, 0.4} 
\begin{filecontents}{testdata.dat}
1   30
2   44
4   26
3   39
\end{filecontents}

\begin{document}
\begin{figure}
\begin{tikzpicture}
        \begin{axis}[   
            ybar stacked,
            ymin=0,
            ymax=100,
            bar width=5pt,
            legend style={at={(0.35, -0.4)},anchor=south west},
            legend columns=-1            
          ]
          \addplot[ybar,fill=OgAns]  file {testdata.dat}; 
        \end{axis}
    \end{tikzpicture}
\end{figure}


\end{document}
share|improve this question

1 Answer 1

up vote 7 down vote accepted

You can sort the table using the pgfplotstable package. It provides a command \pgfplotstablesort[<options>]{<output table name>}{<input table>}. To read the testdata.dat file and sort it in descending order according to the second column, storing the sorted table in a macro called \datatablesorted, you'd use the following command:

\pgfplotstablesort[sort key=1, sort cmp=int >]{\datatablesorted}{testdata.dat}

To plot the sorted table, you would then use the following command:

\addplot [fill=green]  table [x expr=\coordindex] {\datatablesorted};

The x expr=\coordindex is necessary to make sure that the bars are plotted in the order they appear in the sorted table, otherwise they'd be plotted according to the value in the first column.

To get the right labels for the bars, include xticklabels from table={\datatablesorted}{0} in the axis options, which instructs PGFPlots to take the labels from the first column of the \datatablesorted table.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}

\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
1   30
2   44
4   26
3   39
\end{filecontents}

\pgfplotstablesort[sort key=1, sort cmp=int >]{\datatablesorted}{testdata.dat}

\begin{document}
\begin{tikzpicture}
        \begin{axis}[   
            ybar,
            ymin=0,
            ymax=100,
            xtick=data,
            xticklabels from table={\datatablesorted}{0}
          ]
          \addplot [fill=green]  table [x expr=\coordindex] {\datatablesorted}; 
        \end{axis}
    \end{tikzpicture}
\end{document}

share|improve this answer
    
Ah! Thank you very much! –  toobee Aug 29 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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