Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (12.7k points)

I'm creating bar plots utilizing matplotlib and it would seem that there is a bug with the stacked bar plot. The total for every vertical stack ought to be 100. Notwithstanding, for X-AXIS ticks 65, 70, 75, and 80 we get totally self-assertive outcomes that don't bode well. I don't comprehend what the issue is. Kindly discover the MWE beneath.

import numpy as np

import matplotlib.pyplot as plt

import matplotlib

header = ['a','b','c','d']

dataset= [('60.0', '65.0', '70.0', '75.0', '80.0', '85.0', '90.0', '95.0', '100.0', '105.0', '110.0', '115.0', '120.0', '125.0', '130.0', '135.0', '140.0', '145.0', '150.0', '155.0', '160.0', '165.0', '170.0', '175.0', '180.0', '185.0', '190.0', '195.0', '200.0'), (0.0, 25.0, 48.93617021276596, 83.01886792452831, 66.66666666666666, 66.66666666666666, 70.96774193548387, 84.61538461538461, 93.33333333333333, 85.0, 92.85714285714286, 93.75, 95.0, 100.0, 100.0, 100.0, 100.0, 80.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0), (0.0, 50.0, 36.17021276595745, 11.320754716981133, 26.666666666666668, 33.33333333333333, 29.03225806451613, 15.384615384615385, 6.666666666666667, 15.0, 7.142857142857142, 6.25, 5.0, 0.0, 0.0, 0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 12.5, 10.638297872340425, 3.7735849056603774, 4.444444444444445, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (100.0, 12.5, 4.25531914893617, 1.8867924528301887, 2.2222222222222223, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)]

X_AXIS = dataset[0]

matplotlib.rc('font', serif='Helvetica Neue')

matplotlib.rc('text', usetex='false')

matplotlib.rcParams.update({'font.size': 40})

fig = matplotlib.pyplot.gcf()

fig.set_size_inches(18.5, 10.5)

configs = dataset[0]

N = len(configs)

ind = np.arange(N)

width = 0.4

p1 = plt.bar(ind, dataset[1], width, color='r')

p2 = plt.bar(ind, dataset[2], width, bottom=dataset[1], color='b')

p3 = plt.bar(ind, dataset[3], width, bottom=dataset[2], color='g')

p4 = plt.bar(ind, dataset[4], width, bottom=dataset[3], color='c')

plt.ylim([0,120])

plt.yticks(fontsize=12)

plt.ylabel(output, fontsize=12)

plt.xticks(ind, X_AXIS, fontsize=12, rotation=90)

plt.xlabel('test', fontsize=12)

plt.legend((p1[0], p2[0], p3[0], p4[0]), (header[0], header[1], header[2], header[3]), fontsize=12, ncol=4, framealpha=0, fancybox=True)

plt.show()

1 Answer

0 votes
by (26.4k points)

You need the lower part of each dataset to be the amount of all the datasets that preceded. you may likewise have to change over the datasets to NumPy arrays to add them together.

p1 = plt.bar(ind, dataset[1], width, color='r')

p2 = plt.bar(ind, dataset[2], width, bottom=dataset[1], color='b')

p3 = plt.bar(ind, dataset[3], width, 

             bottom=np.array(dataset[1])+np.array(dataset[2]), color='g')

p4 = plt.bar(ind, dataset[4], width,

             bottom=np.array(dataset[1])+np.array(dataset[2])+np.array(dataset[3]),

             color='c')

Then again, you could change them over to NumPy arrays before you begin plotting. 

dataset1 = np.array(dataset[1])

dataset2 = np.array(dataset[2])

dataset3 = np.array(dataset[3])

dataset4 = np.array(dataset[4])

p1 = plt.bar(ind, dataset1, width, color='r')

p2 = plt.bar(ind, dataset2, width, bottom=dataset1, color='b')

p3 = plt.bar(ind, dataset3, width, bottom=dataset1+dataset2, color='g')

p4 = plt.bar(ind, dataset4, width, bottom=dataset1+dataset2+dataset3,

             color='c')

Or then again at long last on the off chance that you need to try not to change over to NumPy arrays, you could utilize a list comprehensions:

p1 = plt.bar(ind, dataset[1], width, color='r')

p2 = plt.bar(ind, dataset[2], width, bottom=dataset[1], color='b')

p3 = plt.bar(ind, dataset[3], width,

             bottom=[sum(x) for x in zip(dataset[1],dataset[2])], color='g')

p4 = plt.bar(ind, dataset[4], width,

             bottom=[sum(x) for x in zip(dataset[1],dataset[2],dataset[3])],

             color='c')

Want to become a expert in Python? Join the python course fast!

Related questions

0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer
asked Nov 30, 2020 in Python by laddulakshana (12.7k points)

Browse Categories

...