English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Bar chart or bar graph is a chart or graphic that displays categorical data with rectangular bars, whose height or length is proportional to the values they represent. Bars can be drawn vertically or horizontally.
Bar charts show comparisons between discrete categories. One axis of the chart shows the specific categories to be compared, and the other axis represents the measured values.
The Matplotlib API provides the bar() function, which can be used in MATLAB style as well as in an object-oriented API. The signature of the bar() function used with the axis object is as follows -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 ax.bar(x, height, width, bottom, align)
This function uses a size of (x − width = 2; x + ; width=2; bottom; bottom + to bind a rectangle creation bar chart.
The parameters of this function are -
x - Scalar sequence representing the x-coordinate of the bar. If x is the center (default) or left edge of the bar, then align the control. height - Scalar or scalar sequence representing the height of the bar. width - Scalar or similar array, optional. The default width of the bar is 0.8. bottom - Scalar or similar array, optional. The default y-coordinate of the bar is None. align - {'center', 'edge'}, optional, default: center.
This function returns a Matplotlib container object containing all bars. Below is a simple example of a Matplotlib bar chart. It shows the number of students enrolled in various courses offered by a college.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math plt.rcParams['font.sans-serif'] = ['SimHei'] # Step one (replace sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Original text from [Lidi Huo], please contact the author for commercial use and keep the original link for non-commercial use: fig = plt.figure() ax = fig.add_axes([0,0,1,1] langs = ['C', 'C++Java, Python, PHP students = [23,17,35,29,12students = [ ] plt.show()
Execute the above example code to get the following results -
ax.bar(langs,students)
Multiple bar charts may be needed when comparing multiple quantities and changing one variable, with one colored bar for a quantity value.25units. Each bar will move 0.25units. The data object is a multivariate graph containing the number of students who passed in the three branches of the Engineering College over the past four years.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math plt.rcParams['font.sans-serif'] = ['SimHei'] # Step one (replace sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Original text from [Lidi Huo], please contact the author for commercial use and keep the original link for non-commercial use: data = [[30, 25, 50, 20], [40, 23, 51, 17], [35, 22, 45, 19]] X = np.arange(4) fig = plt.figure() ax = fig.add_axes([0,0,1,1] ax.bar(X + 0.00, data[0], color = 'b', width = 0.25) ax.bar(X + 0.25, data[1], color = 'g', width = 0.25) ax.bar(X + 0.50, data[2, color = 'r', width = 0.25) plt.show()
Execute the above example code to get the following results -
Stacked bar charts represent the bars of different groups on top of each other. The resulting bar height displays the combined result of the groups.
The optional bottom parameter of the pyplot.bar() function specifies the starting value of the bar. It does not run from zero to a value, but from the bottom to the value. The first call to pyplot.bar() draws a blue bar. The second call to pyplot.bar() draws a red bar chart, with the bottom of the blue bar at the top of the red bar chart.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math plt.rcParams['font.sans-serif'] = ['SimHei'] # Step one (replace sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Original text from [Lidi Huo], please contact the author for commercial use and keep the original link for non-commercial use: N = 5 menMeans = (20, 35, 30, 35, 27) womenMeans = (25, 32, 34, 20, 25) ind = np.arange(N) # the x locations for the groups width = 0.35 fig = plt.figure() ax = fig.add_axes([0,0,1,1] ax.bar(ind, menMeans, width, color='r') ax.bar(ind, womenMeans, width, bottom=menMeans, color='b') ax.set_ylabel('Score') ax.set_title('Scores by Group and Gender') ax.set_xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5)) ax.set_yticks(np.arange(0, 81, 10)) ax.legend(labels=['Male', 'Female']) plt.show()
Execute the above example code to get the following results -