1

I am trying to plot multiple histograms on the same window using a list of tuples. I have managed to get it to sketch only 1 tuple at a time and I just can't seem to get it to work with all of them.

import numpy as np
import matplotlib.pyplot as plt

a = [(1, 2, 0, 0, 0, 3, 3, 1, 2, 2), (0, 2, 3, 3, 0, 1, 1, 1, 2, 2), (1, 2, 0, 3, 0, 1, 2, 1, 2, 2),(2, 0, 0, 3, 3, 1, 2, 1, 2, 2),(3,1,2,3,0,0,1,2,3,1)] #my list of tuples

q1,q2,q3,q4,q5,q6,q7,q8,q9,q10 = zip(*a) #split into [(1,0,1,2,3) ,(2,2,2,0,1),..etc] where q1=(1,0,1,2,3)

labels, counts = np.unique(q1,return_counts=True) #labels = 0,1,2,3 and counts the occurence of 0,1,2,3

ticks = range(len(counts))
plt.bar(ticks,counts, align='center')
plt.xticks(ticks, labels)
plt.show()

As you can see from the above code, I can plot one tuple at a time say q1,q2 etc but how do I generalise it so that it plots all of them.

I've tried to mimic this python plot multiple histograms, which is exactly what I want however I had no luck.

Thank you for your time :)

yatu
  • 86,083
  • 12
  • 84
  • 139
Alan Jones
  • 452
  • 1
  • 8
  • 19

1 Answers1

1

You need to define a grid of axes with plt.subplots taking into account the amount of tuples in the list, and how many you want per row. Then iterate over the returned axes, and plot the histograms in the corresponding axis. You could use Axes.hist, but I've always preferred to use ax.bar, from the result of np.unique, which also can return the counts of unique values:

from matplotlib import pyplot as plt
import numpy as np

l = list(zip(*a))
n_cols = 2
fig, axes = plt.subplots(nrows=int(np.ceil(len(l)/n_cols)), 
                         ncols=n_cols, 
                         figsize=(15,15))

for i, (t, ax) in enumerate(zip(l, axes.flatten())):
    labels, counts = np.unique(t, return_counts=True)
    ax.bar(labels, counts, align='center', color='blue', alpha=.3)
    ax.title.set_text(f'Tuple {i}')

plt.tight_layout()  
plt.show()

enter image description here

You can customise the above to whatever amount of rows/cols you prefer, for 3 rows for instance:

l = list(zip(*a))
n_cols = 3
fig, axes = plt.subplots(nrows=int(np.ceil(len(l)/n_cols)), 
                         ncols=n_cols, 
                         figsize=(15,15))

for i, (t, ax) in enumerate(zip(l, axes.flatten())):
    labels, counts = np.unique(t, return_counts=True)
    ax.bar(labels, counts, align='center', color='blue', alpha=.3)
    ax.title.set_text(f'Tuple {i}')

plt.tight_layout()  
plt.show()

enter image description here

yatu
  • 86,083
  • 12
  • 84
  • 139
  • I should be able to see 10 histograms.I see in the following line ```for i, (t, ax) in enumerate(zip(a, axes.flatten())): ``` the tuples are not the tuples I need, I need (1,0,1,2,3), (2,2,2,0,1) etc not (1, 2, 0, 0, 0, 3, 3, 1, 2, 2) (0, 2, 3, 3, 0, 1, 1, 1, 2, 2) (1, 2, 0, 3, 0, 1, 2, 1, 2, 2) (2, 0, 0, 3, 3, 1, 2, 1, 2, 2) (3, 1, 2, 3, 0, 0, 1, 2, 3, 1) – Alan Jones Apr 05 '20 at 10:10
  • Basically I need the first/second/third... element of every tuple to be a tuple and then plot that in its own histogram. – Alan Jones Apr 05 '20 at 10:15
  • 1
    Gotcha, check now @alan – yatu Apr 05 '20 at 11:01
  • 1
    Thanks @yatu. Stay safe :) – Alan Jones Apr 05 '20 at 11:06
  • Just a quick quest @yatu, it's not possible to change the labels to some string like 0->A 1-->B etc since its a tuple? Sorry for all the questions, I am not good with matplot module. – Alan Jones Apr 05 '20 at 11:12
  • 1
    Just letters from the alphabet like that? @alan – yatu Apr 05 '20 at 11:13
  • 1
    All good, I figured it out.I went back to see where I got my tuples from and changed the values to A,B etc. So now my tuples are in strings rather than integers. Thanks for your help! – Alan Jones Apr 05 '20 at 11:44