Histogram(Bar),Pie Chart in Python

Histogram

Use the bar () function provided by Matplotlib to draw a histogram

Similar to the plot() function described earlier, every time the program calls the bar() function, a set of histogram will be generated. If you want to generate multiple sets of histogram, you can call the bar() function multiple times to achieve this

bar()

bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Main parameters:

  • x: Include a list of subscripts for all columns
  • Height: the numerical sequence of y-axis is also the height of histogram, which is generally the data we need to show
  • Width: the width of the histogram. Generally, this is 0.8
  • align: the column alignment has two optional values: cneter and edge. Center indicates that each column is odd according to the subscript, edge indicates that each column starts from the following table, and then displays to the right of the subscript. If this parameter is not specified, the default is center

Optional parameters:

  • Color: the color of each column. You can specify a fixed value or a list
  • edgecolor: the color of each column frame
  • linewidth: the border width of each column. If this parameter is not set, there is no border by default
  • Tick? Label: the label displayed on each column, no label by default
  • xerr: length of line segment at the top of each column in the direction of transverse axis
  • yerr: length of line segment at the top of each column in the longitudinal direction
  • ecolor: set the line color of xerr and yerr. You can specify a fixed value or a list

Using matplotlib to draw a simple histogram

import matplotlib.pyplot as plt

num_list = [1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),num_list)
plt.show()

Set the column color:

import matplotlib.pyplot as plt

num_list = [1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),num_list,color = 'rgbc') #Red, green, blue and green
plt.show()

Setting labels

import matplotlib.pyplot as plt

name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),num_list,color = 'rgbc',tick_label = name_list)
plt.show()

stacked column chart

import matplotlib.pyplot as plt

name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,1.6,7.8,6]
num_list2 = [1,2.3,3,2]
plt.bar(range(len(num_list)),num_list, color = 'r',tick_label = name_list)
plt.bar(range(len(num_list2)),num_list2, color = 'g',tick_label = name_list, bottom=num_list)
plt.show()

Add legend

legend

import matplotlib.pyplot as plt

name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,1.6,7.8,6]
num_list2 = [1,2.3,3,2]
plt.bar(range(len(num_list)),num_list, color = 'r',tick_label = name_list, label = 'boys')
plt.bar(range(len(num_list2)),num_list2, color = 'g',tick_label = name_list, label = 'girl')
plt.legend(loc='best')
plt.show()

Horizontal bar chart

Change bar to barh

import matplotlib.pyplot as plt

name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,1.6,7.8,6]
num_list2 = [1,2.3,3,2]
plt.barh(range(len(num_list)),num_list, color = 'r',tick_label = name_list, label = 'boys')
plt.barh(range(len(num_list2)),num_list2, color = 'g',tick_label = name_list, label = 'girl')
plt.legend(loc='best')
plt.show()


Parallel histogram

Note that the x starting point of the second histogram is the original base plus the corresponding width

import matplotlib.pyplot as plt

name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,1.6,7.8,6]
num_list2 = [1,2.3,3,2]
x = list(range(len(num_list)))
total_width = 0.8
n=2
width = total_width / n
plt.bar(x ,num_list ,width = width, label='boys')
for i in range(len(x)):
    x[i] = x[i] + width
plt.bar(x ,num_list2 ,width = width, label='girls',tick_label = name_list)
plt.legend(loc='best')
plt.show()

Pie chart

Concept:

A pie chart shows the ratio of the size of items in a series to the sum of the total items

Pie chart can automatically draw pie according to the percentage of data

Basic syntax for drawing pie charts:

Create a pie chart of array X. the area of each seven stars is determined by x/sum(x);

If sun (x) < 1, the X array will not be standardized, and the x value is the area proportion of the wedge area. Note: this situation will result in an empty wedge of 1-sum(x).

If sum (x) is more than 1, then x[i]/sum(x) calculates the proportion of each wedge, and the 360 ° area of pie chart is filled.

pie

pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None)

Parameter details:

x: If sum (x) > 1, sum (x) will be normalized

Expand: distance from each block to the center, a list or array

labels: list, optional, default, None; label each wedge

Color: array like, optional, default, None; if not, use the color in current, active cycle.

autopct: controls the percentage setting in pie chart. You can use format string or format function: integer (‘% d%%’), floating point (‘% 1.3f%%’), string (‘% s%%’), function

label distance: float, optional, default: 1.1; the scale of label mark relative to radius is 1.1 by default, if less than 1, it will be drawn on the inside of pie chart;

pctdistance: float, optional, default: 0.6; similar to label distance specifying the position scale of autopct, the default is 0.6;

shadow: bool, optional, default: False; True

startangle: float, optional, default: None; the default drawing angle is from the positive direction of x-axis to the counterclockwise direction, such as setting = 90, from the positive direction of y-axis

Radius:float, optional, default:None; the radius of the pie chart. If it is None, the default is 1

Counter clock: float, optional, default: None; specify fraction direction, counter clockwise (True) or clockwise

Wedgeprops: dict,optional,default:None; describes the width value of the wedge boundary line. The parameter form ‘wedgeprops = {‘ linewidth ‘: 3}’ the width of the wedge boundary line is 3

textprops: dict,optional,default:None; dictionary parameters passed to text objects

Center: list of float,optional,default:(0,0); the center of the icon is, default (0,0), or two scalar sequences (sequence of 2 scalars)


Bar Charts

To construct a bar plot using Matplotlib, first import Matplotlib’s pyplot library. The alias plt is commonly used to substitute matplotlib.pyplot. If using a Jupiter notebook, include the line %matplotlib inline. In the next example, NumPy is used. So NumPy must be included in the imports as well as Matplotlib.

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

We need some data to add to our bar chart. In this case, the data is from a set of coefficient of thermal expansion lab measurements. The coefficient of thermal expansion (CTE) is a material property that describes how much a material will change in length as a result of a change in temperature. Different materials have different CTE’s and we can use the lab data to determine which material will expand the most if all three materials are heated up to the same temperature (assuming all three materials start at the same temperature).

First, we need to input the lab measurement data as NumPy arrays:

# Data
aluminum = np.array([
    6.4e-5, 3.01e-5, 2.36e-5, 3.0e-5, 7.0e-5, 4.5e-5, 3.8e-5, 4.2e-5, 2.62e-5,
    3.6e-5
])
copper = np.array([
    4.5e-5, 1.97e-5, 1.6e-5, 1.97e-5, 4.0e-5, 2.4e-5, 1.9e-5, 2.41e-5, 1.85e-5,
    3.3e-5
])
steel = np.array([
    3.3e-5, 1.2e-5, 0.9e-5, 1.2e-5, 1.3e-5, 1.6e-5, 1.4e-5, 1.58e-5, 1.32e-5,
    2.1e-5
])

Next, calculate the average or mean of each data set using NumPy’s np.mean() function.

# Calculate the average
aluminum_mean = np.mean(aluminum)
copper_mean = np.mean(copper)
steel_mean = np.mean(steel)

Then build a list of materials and CTE’s. Note the list of materials is a list of strings, the list of x-positions x_pos is an array of numbers [0,1,2], and the list CTEs is a list of three numbers from the np.mean() calculation above. Python’s len() function returns the length of a list, array, dictionary or tuple.

# Create arrays for the plot
materials = ['Aluminum', 'Copper', 'Steel']
x_pos = np.arange(len(materials))
CTEs = [aluminum_mean, copper_mean, steel_mean]

After the materialsx_pos, and CTEs (the labels below the bars) are defined, the bar chart is created using the ax.bar() method.

Mathplotlib’s ax.bar() method requires two positional arguments, a list of bar positions and a list of bar heights. In this bar chart, x_pos is the list of bar positions and CTEs is the list of bar heights.

The list of materials is passed to the ax.set_xticklabels() method.

# Build the plot
fig, ax = plt.subplots()

ax.bar(x_pos, CTEs, align='center', alpha=0.5)
ax.set_ylabel('Coefficient of Thermal Expansion ($\degree C^{-1}$)')
ax.set_xticks(x_pos)
ax.set_xticklabels(materials)
ax.set_title('Coefficent of Thermal Expansion (CTE) of Three Metals')
ax.yaxis.grid(True)

# Save the figure and show
plt.tight_layout()
plt.savefig('bar_plot.png')
plt.show()

../_images/06.06-Bar-Charts-and-Pie-Charts_12_0.png

Pie Charts

Pie charts can be constructed with Matplotlib’s ax.pie() method. The one required positional argument supplied to the ax.pie() method is a list of pie piece sizes. Optional keyword arguments include a list of pie piece labels (label= ) and if the percentages will be auto-calculated and in what format (autopct= ).

For our first pie chart, the data we will plot describes the number of students who choose different engineering majors at colleges in the US each year.

The following table lists the approximate numbers of engineering graduates in different engineering disciplines:

DisciplineNumber of graduates
Civil Engineering15,000 graduates
Electrical Engineering50,000 graduates
Mechanical Engineering45,000 graduates
Chemical Engineering10,000 graduates

We will plot this data on a pie chart with Matplotlib’s ax.pie() method. The pie piece labels are defined as a list of strings, and the pie piece sizes are defined as a list of integers. The line ax.axis('equal') is needed to ensure the pie chart is a circle. If you leave out ax.axis('equal'), the pie chart may look like an oval instead of a circle.

The code section below builds a pie chart with four pie pieces, each pie piece labeled with a relative size auto-calculated to the nearest 10th of a percent.

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = ['Civil', 'Electrical', 'Mechanical', 'Chemical']
sizes = [15, 50, 45, 10]

fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
ax.axis('equal')  # Equal aspect ratio ensures the pie chart is circular.
ax.set_title('Engineering Diciplines')

plt.show()

../_images/06.06-Bar-Charts-and-Pie-Charts_15_0.png

Pie pieces can be highlighted by “exploding” them out. Exploded pie pieces are applied to a Matplotlib pie chart by supplying the explode= keyword argument to the ax.pie() method. shadow=True and startangle= are two additional keyword arguments that can be passed to the ax.pie() method to control the angle and rotation of the pieces on a pie chart.

The code section below creates a pie chart with the pie pieces separated and the “Chemical” piece exploded out.

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

# Pie chart, where the slices will be ordered and plotted counter-clockwise
labels = ['Civil', 'Electrical', 'Mechanical', 'Chemical']
sizes = [15, 30, 45, 10]

# Explode out the 'Chemical' pie piece by offsetting it a greater amount
explode = (0.1, 0.1, 0.1, 0.4)  
fig, ax = plt.subplots()
ax.pie(sizes,
       explode=explode,
       labels=labels,
       autopct='%1.1f%%',
       shadow=True,
       startangle=90)
ax.axis('equal')  # Equal aspect ratio ensures the pie chart is circular.
ax.set_title('Engineering Diciplines')

plt.show()

../_images/06.06-Bar-Charts-and-Pie-Charts_17_0.png
Follow Us On