Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Stacked Bars | Bar Charts
Visualization in Python with matplotlib

bookStacked Bars

Stacked bar charts are popular in cases when we want to expand our vision to multiple categories. For example, in the previous task, we may compare two Indian cities by placing the second bar right above the first one. In this case, the bar height will be the sum of two bars values.

To build a stacked bar chart we need to call the .bar() function as many times as many categories we consider. Each next .bar() function should have a bottom parameter with the y-axis values of the lower bar assigned. For example, let's represent countries' GDP by sector composition.

1234567891011121314151617181920
# Import library import matplotlib.pyplot as plt # Create data for chart countries = ['United States', 'India', 'Brazil'] agricultural = [333600, 1458996, 214368] industrial = [3722590, 2179020, 672336] services = [15592000, 5826510, 2361296] # Create Axes and Figure objects fig, ax = plt.subplots() # Initialize bar chart ax.bar(countries, agricultural, label = 'Agricultural') ax.bar(countries, industrial, label = 'Industrial', bottom = agricultural) ax.bar(countries, services, label = 'Services', bottom = industrial) # Display the plot plt.legend() plt.show()
copy

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 2.94

bookStacked Bars

Glissez pour afficher le menu

Stacked bar charts are popular in cases when we want to expand our vision to multiple categories. For example, in the previous task, we may compare two Indian cities by placing the second bar right above the first one. In this case, the bar height will be the sum of two bars values.

To build a stacked bar chart we need to call the .bar() function as many times as many categories we consider. Each next .bar() function should have a bottom parameter with the y-axis values of the lower bar assigned. For example, let's represent countries' GDP by sector composition.

1234567891011121314151617181920
# Import library import matplotlib.pyplot as plt # Create data for chart countries = ['United States', 'India', 'Brazil'] agricultural = [333600, 1458996, 214368] industrial = [3722590, 2179020, 672336] services = [15592000, 5826510, 2361296] # Create Axes and Figure objects fig, ax = plt.subplots() # Initialize bar chart ax.bar(countries, agricultural, label = 'Agricultural') ax.bar(countries, industrial, label = 'Industrial', bottom = agricultural) ax.bar(countries, services, label = 'Services', bottom = industrial) # Display the plot plt.legend() plt.show()
copy

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt