1

I have a CSV file. The CSV file contains several columns.

I want to plot a histogram that displays:

  1. As the x axis: bins for the percentage of rows, that can be changed;
  2. As the y axis: the cell values.

The end result would be a plot as such (This is just an example, the important point here is not the style of the plot, but how it operates. The data is also just an example. I have drawn ellipses to save me from drawing the entire plot by hand):

Plot

I have absolutely no idea where to even begin with this, save for providing the below MWE which is just a standard histogram.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
A,B
2,1
3,3
4,6
2,2
3,1
4,3
5,3
6,1
9,5
9,1
1,2
\end{filecontents*}
\usepackage{pgfplotstable}
\begin{document}



\begin{tikzpicture}
\begin{axis}[
ybar,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Percentage of Rows Containing Values},
xlabel={Percentage of Rows},
ylabel={Values},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
ytick distance=2,
width=\textwidth,
height=6cm,
ymin=0,
xticklabel interval boundaries
]

\addplot +[
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};

\end{axis}
\end{tikzpicture}
\end{document}
  • I'm not sure I understand what you want to do. What do you mean by "percentage of rows"? I first thought you wanted how many rows (in % of total rows) a given value (e.g. 2) occurs, but that doesn't seem to fit with your sketch. – Torbjørn T. Jan 23 at 17:34
  • You're exactly right. My data is contained in a column. Each row is an occurrence of an event. Each cell within that row records the number of times something happened in that event. For example, each row could be an episode of a TV show. Each cell might be the number of times someone says a swear word in that episode. The histogram would show the percentage of TV shows where someone says x number of swear words. – Craig Jan 23 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.