Simply put suppose I have 2 lists:
A -> Has the list of names ['A','B','C','D','E','F','G','H']
B -> Has the list of values [5,7,3,8,2,9,1,3]
A will be the names of the X-Axis labels and the corresponding values in B will be the height of the graph ( i.e. the Y-Axis ).
%matplotlib inline
import pandas as pd
from matplotlib import rcParams
import matplotlib.pyplot as plt
from operator import itemgetter
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(14,9), dpi=600)
reso_names = ['A','B','C','D','E','F','G','H']
reso_values = [5,7,3,8,2,9,1,3]
plt.bar(range(len(reso_values)), reso_values, align='center')
plt.xticks(range(len(reso_names)), list(reso_names), rotation='vertical')
plt.margins(0.075)
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Graph', {'family' : 'Arial Black',
'weight' : 'bold',
'size' : 22})
plt.show()
This code gives the following output :
However I want it such that it makes subgraphs for every 2 values. In this case there should be 4 subgraphs:
- 1st Graph has 'A' and 'B'
- 2nd Graph has 'C' and 'D'
- 3rd Graph has 'E' and 'F'
- 4th Graph has 'G' and 'H'
This splitting should be done dynamically (not 4 different loops, it should break the graph into units of 2 each depending on the size of the input, if list A has 10 values then it should give 5 subgraphs).
I figured out how to split the graph into two with half each but I need to achieve it using steps of N per graph (N in this example being 2).
The code I have for breaking the graph into 2 equal subgraphs is :
%matplotlib inline
import pandas as pd
from matplotlib import rcParams
import matplotlib.pyplot as plt
from operator import itemgetter
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(14,9), dpi=600)
reso_names = ['A','B','C','D','E','F','G','H']
reso_values = [5,7,3,8,2,9,1,3]
fig, axs = plt.subplots(nrows=2, sharey=True, figsize=(14,18), dpi=50)
size = int(len(reso_values))
half = int( size/2 )
fig.suptitle('Graph',
**{'family': 'Arial Black', 'size': 22, 'weight': 'bold'})
for ax, start, end in zip(axs, (0, half), (half, size)):
names, values = list(reso_names[start:end]), reso_values[start:end]
ax.bar(range(len(values)), values, align='center')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_xticks(range(len(names)))
ax.set_xticklabels(names, rotation='vertical')
ax.set_xlim(0, len(names))
fig.subplots_adjust(bottom=0.05, top=0.95)
plt.show()
I just want the program to dynamically split the graphs into subgraphs based on the splitting number N.


