Зміст курсу
Ultimate Visualization with Python
Ultimate Visualization with Python
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. Without further ado, let’s have a look at the bar chart implementation in matplotlib
:
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Creating a bar chart with separate colors for each bar plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow']) plt.show()
Required Parameters
In this example, we utilize the bar()
function from pyplot
to create a bar chart. We provide a list of programming language names for the x-axis as the first argument (x
) and the respective bar heights representing popularity percentages as the second argument (height
).
Optional Parameters
Using the color
keyword argument, we speicfy the list of color names. By default, all bars will have a bluish color.
Another important optional parameter is width
and stands for the width(s) of the bars, its default value is 0.8. It can either be a single float number, meaning equal width for each bar, or an array of floats, in which each element corresponds to the width of the respective bar. Let’s change this parameter for our example:
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()
We can clearly see that the width decreases from left to right, that’s exactly what we expected.
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.
Swipe to show code editor
- Use the correct function to create a bar chart.
- Pass
countries
andgdp_list
in this function in the correct order. - Use the proper keyword argument to specify the width of the bars.
- Fill the list for this keyword argument with the following values
0.6
,0.9
,0.45
,0.2
from left to right.
Дякуємо за ваш відгук!
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. Without further ado, let’s have a look at the bar chart implementation in matplotlib
:
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Creating a bar chart with separate colors for each bar plt.bar(programming_languages, shares, color=['blue', 'green', 'red', 'yellow']) plt.show()
Required Parameters
In this example, we utilize the bar()
function from pyplot
to create a bar chart. We provide a list of programming language names for the x-axis as the first argument (x
) and the respective bar heights representing popularity percentages as the second argument (height
).
Optional Parameters
Using the color
keyword argument, we speicfy the list of color names. By default, all bars will have a bluish color.
Another important optional parameter is width
and stands for the width(s) of the bars, its default value is 0.8. It can either be a single float number, meaning equal width for each bar, or an array of floats, in which each element corresponds to the width of the respective bar. Let’s change this parameter for our example:
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()
We can clearly see that the width decreases from left to right, that’s exactly what we expected.
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.
Swipe to show code editor
- Use the correct function to create a bar chart.
- Pass
countries
andgdp_list
in this function in the correct order. - Use the proper keyword argument to specify the width of the bars.
- Fill the list for this keyword argument with the following values
0.6
,0.9
,0.45
,0.2
from left to right.
Дякуємо за ваш відгук!