Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Bar Chart | Creating Commonly Used Plots
Ultimate Visualization with Python

Swipe to show menu

book
Bar Chart

A bar chart, also known as a bar graph, is a visual representation where categorical data is displayed using rectangular bars. The height or length of these bars is directly proportional to the values they depict.

123456
import matplotlib.pyplot as plt programming_languages = ['Java', 'Python', 'C#', 'C++'] shares = [30, 40, 17, 13] # Creating a bar chart with separate colors for each bar plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow']) plt.show()
copy

Bar Chart Parameters

To create a bar chart, the bar() function from pyplot requires two main arguments:

  • a list of labels for the x-axis;

  • and the corresponding heights of the bars.

You can customize the chart using optional parameters like:

  • color β€” sets the color(s) of the bars (default is blue);

  • width β€” defines the width of the bars (default is 0.8). This can be a single number or a list of values for individual bars.

123456
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Specify the bars colors and widths plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow'], width=[0.9, 0.7, 0.5, 0.3]) plt.show()
copy

The plot demonstrates a consistent decrease in bar width from left to right, aligning with the specified values.

Note
Definition

As with other plots, you can always refer to the documentation for a quick lookup of the bar() function syntax and all of its parameters.

Task

Swipe to start coding

  1. Use the correct function to create a bar chart.
  2. Pass countries and gdp_list in this function in the correct order.
  3. Use the proper keyword argument to specify the width of the bars.
  4. Fill the list for this keyword argument with the following values 0.6, 0.45, 0.9, 0.2 from left to right.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Bar Chart

A bar chart, also known as a bar graph, is a visual representation where categorical data is displayed using rectangular bars. The height or length of these bars is directly proportional to the values they depict.

123456
import matplotlib.pyplot as plt programming_languages = ['Java', 'Python', 'C#', 'C++'] shares = [30, 40, 17, 13] # Creating a bar chart with separate colors for each bar plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow']) plt.show()
copy

Bar Chart Parameters

To create a bar chart, the bar() function from pyplot requires two main arguments:

  • a list of labels for the x-axis;

  • and the corresponding heights of the bars.

You can customize the chart using optional parameters like:

  • color β€” sets the color(s) of the bars (default is blue);

  • width β€” defines the width of the bars (default is 0.8). This can be a single number or a list of values for individual bars.

123456
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Specify the bars colors and widths plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow'], width=[0.9, 0.7, 0.5, 0.3]) plt.show()
copy

The plot demonstrates a consistent decrease in bar width from left to right, aligning with the specified values.

Note
Definition

As with other plots, you can always refer to the documentation for a quick lookup of the bar() function syntax and all of its parameters.

Task

Swipe to start coding

  1. Use the correct function to create a bar chart.
  2. Pass countries and gdp_list in this function in the correct order.
  3. Use the proper keyword argument to specify the width of the bars.
  4. Fill the list for this keyword argument with the following values 0.6, 0.45, 0.9, 0.2 from left to right.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt