How to draw stacking Diagram by Python+matplotlib
This article mainly introduces Python+matplotlib how to draw stacking diagram, the article is very detailed, has a certain reference value, interested friends must read it!
I. horizontal stacking diagram
A stacked chart is actually a special form of a bar chart.
From matplotlib import pyplot as plt plt.style.use ('seaborn') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.bar (cnbodfgbsort.index,cnbodfgbsort.PERSONS) plt.bar (cnbodfgbsort.index,cnbodfgbsort.PRICE) plt.bar (cnbodfgbsort.index,cnbodfgbsort.points) plt.show ()
Stacking diagram effect
You can see that some of the blue data are obscured. If we want to show all of them, we can:
Index_x=np.arange (len (cnbodfgbsort.index)) index_xw=0.15from matplotlib import pyplot as plt plt.style.use ('classic') plt.figure (figsize= (152.9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.bar (index_x,cnbodfgbsort.PERSONS,width=w) plt.bar (index_x+w,cnbodfgbsort.PRICE,width=w) plt.bar (index_x+2*w,cnbodfgbsort.points,width=w) plt.show ()
You can see that there is too much difference between BO, PRICE and PERSONS in the data source of Excel. If you make a stack chart, BO will overwrite all the others and cannot show a good effect:
Because the data are so different, we can directly divide BO by 1000:
From matplotlib import pyplot as plt plt.style.use ('classic') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.bar (cnbodfgbsort.index,cnbodfgbsort.PERSONS) plt.bar (cnbodfgbsort.index,cnbodfgbsort.PRICE) plt.bar (cnbodfgbsort.index,cnbodfgbsort.BO/1000) plt.bar (cnbodfgbsort.index,cnbodfgbsort.points) plt.show ()
From matplotlib import pyplot as plt plt.style.use ('classic') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.bar (index_x-w,cnbodfgbsort.BO/1000,width=w) # directly divides BO by 1000plt.bar (index_x,cnbodfgbsort.PERSONS,width=w) plt.bar (index_x+w,cnbodfgbsort.PRICE,width=w) plt.bar (index_x+2*w,cnbodfgbsort.points) Width=w) plt.show ()
2. Labels= ['war', 'love', 'animation', 'action', 'thriller', 'plot'] colors= ['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot (cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
Labels= ['war', 'love', 'animation', 'action', 'thriller', 'plot'] colors= ['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot (cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
Add the data tag plt.legend () labels= ['box office', 'ticket price', 'person-times', 'score'] colors= ['tan','violet','turquoise','tomato','teal','steelblue'] plt.stackplot (cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors) plt.legend ()
These are all the contents of the article "how to draw a stacking Diagram for Python+matplotlib". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!