# Stacked Bar Charts in Matplotlib (With Examples)

stacked bar chart is a type of chart that uses bars to display the frequencies of different categories. We can create this type of chart in Matplotlib by using the matplotlib.pyplot.bar() function.

This tutorial shows how to use this function in practice.

### Create a Basic Stacked Bar Chart

The following code shows how to create a stacked bar chart to display the total sales of two products during four different sales quarters:

```import numpy as np
import matplotlib.pyplot as plt

#create data
quarter = ['Q1', 'Q2', 'Q3', 'Q4']
product_A = [14, 17, 12, 9]
product_B = [7, 15, 24, 18]

#define chart parameters
N = 4
barWidth = .5
xloc = np.arange(N)

#display stacked bar chart
p1 = plt.bar(xloc, product_A, width=barWidth)
p2 = plt.bar(xloc, product_B, bottom=product_A, width=barWidth)
plt.show()
``` ### Add a Title, Labels, and Legend

We can also add a title, labels, tick marks, and a legend to make the chart easier to read:

```import numpy as np
import matplotlib.pyplot as plt

#create data for two teams
quarter = ['Q1', 'Q2', 'Q3', 'Q4']
product_A = [14, 17, 12, 9]
product_B = [7, 15, 24, 18]

#define chart parameters
N = 4
barWidth = .5
xloc = np.arange(N)

#create stacked bar chart
p1 = plt.bar(xloc, product_A, width=barWidth)
p2 = plt.bar(xloc, product_B, bottom=product_A, width=barWidth)

#add labels, title, tick marks, and legend
plt.ylabel('Sales')
plt.xlabel('Quarter')
plt.title('Sales by Product & Quarter')
plt.xticks(xloc, ('Q1', 'Q2', 'Q3', 'Q4'))
plt.yticks(np.arange(0, 41, 5))
plt.legend((p1, p2), ('A', 'B'))

#display chart
plt.show()``` ### Customize Chart Colors

Lastly, we can customize the colors used in the chart with the colors() argument within plt.bar():

```import numpy as np
import matplotlib.pyplot as plt

#create data for two teams
quarter = ['Q1', 'Q2', 'Q3', 'Q4']
product_A = [14, 17, 12, 9]
product_B = [7, 15, 24, 18]

#define chart parameters
N = 4
barWidth = .5
xloc = np.arange(N)

#create stacked bar chart
p1 = plt.bar(xloc, product_A, width=barWidth, color='springgreen')
p2 = plt.bar(xloc, product_B, bottom=product_A, width=barWidth, color='coral')

#add labels, title, tick marks, and legend
plt.ylabel('Sales')
plt.xlabel('Quarter')
plt.title('Sales by Product & Quarter')
plt.xticks(xloc, ('Q1', 'Q2', 'Q3', 'Q4'))
plt.yticks(np.arange(0, 41, 5))
plt.legend((p1, p2), ('A', 'B'))

#display chart
plt.show()``` You can find a complete list of available colors within the Matplotlib documentation.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5491784,"math_prob":0.98834026,"size":2587,"snap":"2021-31-2021-39","text_gpt3_token_len":755,"char_repetition_ratio":0.13782424,"word_repetition_ratio":0.5065274,"special_character_ratio":0.31580982,"punctuation_ratio":0.21206582,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99466115,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,5,null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T22:11:46Z\",\"WARC-Record-ID\":\"<urn:uuid:e95ada8b-53a4-476a-ac72-4e3edde51069>\",\"Content-Length\":\"35449\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8e47654a-2b02-46bc-b03d-bf664e07f328>\",\"WARC-Concurrent-To\":\"<urn:uuid:84276caf-b38e-445d-93ec-bd67cb642001>\",\"WARC-IP-Address\":\"35.206.100.4\",\"WARC-Target-URI\":\"https://www.statology.org/matplotlib-stacked-bar-chart/\",\"WARC-Payload-Digest\":\"sha1:TY3LBGQKSKLCM5RXFQWDYW43A5FZORJL\",\"WARC-Block-Digest\":\"sha1:LSQD5655DZLSFPWXREKFGNWC2EW5J6GI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056578.5_warc_CC-MAIN-20210918214805-20210919004805-00026.warc.gz\"}"}