Plot a Data Set According to Counts of Categories of a Variable
<p>I have a dataset which has 14 columns (I had to only use 4 columns: travelling class, gender, age, and fare price) that I have split into train and test data sets. I need to create a vertical bar chart from the train data set for the distribution of the passengers by travelling class (1, 2, and 3 are the classes). I am not allowed to use NumPy, Pandas, SciPy, and SciKit-Learn.</p>

<p>I am very new to Python, and I know how to plot very simple graphs, but when it comes to more complicated graphs, I get a bit lost.</p>

<p>This is my code (I know there is a lot wrong):</p>

<pre><code>travelling_class = defaultdict(list)
for row in data:
    travelling_class[row[0]]

travelling_class = {key: len(val) for key, val in travelling_class.items()}

keys = travelling_class()
vals = [travelling_class[key] for key in keys]
ind  = range(min(travelling_class.keys()), max(travelling_class.keys()) + 1)
width = 0.6

plt.xticks([i + width/2 for i in ind], ind, ha='center')
plt.xlabel('Tracelling Class') 
plt.ylabel('Counts of Passengers')
plt.title('Number of Passengers per Travelling Class')
plt.ylim(0, 1000)
plt.bar(keys, vals, width)
plt.show()
</code></pre>

<hr>

<pre><code>import matplotlib.pyplot as plt

classes = travelling_class[1, 2, 3]

plt.hist(classes)
plt.show()
</code></pre>

<p>@TrakJohnson This is the original asker of the question - sorry I accidentally somehow deleted my profile so had to make a new one. Thank you so much for your help. The problem is that my data set is 1045 rows, so it might be difficult to list all of them. Does the above seem reasonable?</p>

<p>Use <code>plt.hist</code>, which will plot a histogram <a href="https://plot.ly/matplotlib/histograms/" rel="nofollow">(more info here)</a></p>

<p>Example:</p>

<pre><code>import matplotlib.pyplot as plt

classes = [1, 2, 1, 1, 3, 3]

plt.hist(classes)
plt.show()
</code></pre>

<p>And this is the result:</p>

<p><a href="http://i.stack.imgur.com/cGHWC.png" rel="nofollow"><img src="http://i.stack.imgur.com/cGHWC.png" alt="Histogram"></a></p>

python bar-chart categories training-data test-data