Matplotlib : Data Processing and Visulisation with Python (Python Exercise 22)

Data Processing and Visulisation with Python

Import module

Import the matplotlib.pyplot module under the name plt (the tidy way):

import matplotlib.pyplot as plt
import numpy as np

Maybe you’ll need the following line to see plots in the notebook. But as long as I have tested, it is no longer necessary any more now. However, I have not found any official document to support this. You can make your choice at your own risk.

%matplotlib inline

That line is only for jupyter notebooks, if you are using another editor, you’ll use: plt.show() at the end of all your plotting commands to have the figure pop up in another window.

Data preparation

Let’s prepare some data by generating x (in numpy array as shown in output) and y (as square of x).

The data we want to plot:

# generate data (x and y)
x = np.linspace(0,5,11,endpoint=True)
y = x**2
# check x
x
# check y
y

在这里插入图片描述

Basic Matplotlib Commands

Create a very simple line plot as the output.

# plot (x, y) with red line
plt.plot(x,y,'r-')

# set x label
plt.xlabel('X Axis Title Here')

# set y label
plt.ylabel('Y Axis Title Here')

# set title
plt.title('String Title Here')

在这里插入图片描述

Creating Multiplots on Same Canvas

Create two figures side by side as output. Plot (x, y) in the first figure with red dashed line and plot (y, x) in the second fiqure with solid green line and stars to mark sample points.

Note:

plt.subplot(nrows, ncols, plot_number) will be usful.

x = np.linspace(0,5,11)
y = x**2
plt.subplot(1,2,1)
plt.plot(x,y,'r--')
plt.subplot(1,2,2)
plt.plot(y,x,'g-',y,x,'g*');

在这里插入图片描述

Using Object Oriented Method to create figures

First create a figure instance, then add axes to it.

# Create Figure (empty canvas) with plt.figure()
fig = plt.figure()

# Add a set of axes to figure, name it 'axes', set its left, bottom, width, height to 0.1, 0.1, 0.8, 0.8
axes = fig.add_axes([0.1,0.1,0.8,0.8])

# Plot on that set of axes:
## First, plot (x, y) with blue line
x = np.linspace(0,5,21)
y = x**2
axes.plot(x,y,"b")

## Second, set xlabel to "X Label", set ylabel to "Y Label", and set title to "Title Here"
axes.set_xlabel('X Label')
axes.set_ylabel('Y Label')
axes.set_title('Title Here');

在这里插入图片描述

Figure in figure

To create figure in figure, we can first create a blank canvas and then add two sets of axes to it.

# Creates blank canvas
fig = plt.figure()

# Add two sets of axes to the figure, 
## Name the first set of axes 'axes1', set its left, bottom, width, height to 0.1, 0.1, 0.8, 0.8
axes1 = fig.add_axes([0.1,0.1,0.8,0.8])

## Name the second set of axes 'axes2', set its left, bottom, width, height to 0.2, 0.5, 0.4, 0.3
axes2 = fig.add_axes([0.2,0.5,0.4,0.3])

# Larger Figure Axes 1
## In axes1, plot (x,y) with blue line, and set xlabel, ylabel and title accordingly
x = np.linspace(0,5,21)
axes1.plot(x,x**2,'b-')
axes1.set_xlabel('X_label_axes1')
axes1.set_ylabel('Y_label_axes1')
axes1.set_title('Axes 1 Title')

# Insert Figure Axes 2
## In axes2, plort (y,x) with red line, and set xlabel, ylabel and title accordingly
axes2.plot(x**2,x,'r-')
axes2.set_xlabel('X_label_axes2')
axes2.set_ylabel('Y_label_axes2')
axes2.set_title('Axes 2 Title')

在这里插入图片描述

Using subplots()

The plt.subplots() object will act as a more automatic axes manager.

Create one set of axes with subplots()

Note: subplots() return a (figure, axes) tuple.

# Create a figure with one set of axes similar to plt.figure() except using tuple unpacking to grab fig and axes
fig = plt.figure()
ax = fig.add_subplot(111)
# Now use the axes object to add stuff to plot
## Plot (x, y) in red line, set xlabel, ylabel and title accordingly
x = np.linspace(0,5,100)
y = x**2
ax.plot(x,y,'r-')
ax.set_title('title')
ax.set_xlabel('x')
ax.set_ylabel('y')

在这里插入图片描述

Create more axes with subplots()

Now you can specify the number of rows and columns when creating the subplots() object.

# Empty canvas of 1 by 2 subplots (nrows=1, ncols=2)
fig,axes = plt.subplots(1,2)

在这里插入图片描述

# Axes is an array of axes to plot on
axes;

We can iterate through this array:

# iterate axes and plot
for ax in axes:
    ax.plot(x, y, 'b')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('title')

# Display the figure object    
fig

在这里插入图片描述

tight_layout()

Create four subplots as output (with each nrows, ncols, plot_number from [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ]), and tight_layout() to adjust their positions.

Note:

A common issue with matplolib is overlapping subplots or figures. We ca use fig.tight_layout() or plt.tight_layout() method, which automatically adjusts the positions of the axes on the figure canvas so that there is no overlapping content.

Hint:

Do not use plt.subplots(). Here you’d better use plt.subplot().

X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ]

# Create subplots accordingly
for nr,nc,num in X:
    plt.subplot(nr,nc,num)
    
plt.tight_layout()

# adjust to fit
plt.tight_layout()

在这里插入图片描述

Figure size, aspect ratio and DPI

Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created. You can use the figsize and dpi keyword arguments.

  • figsize is a tuple of the width and height of the figure in inches
  • dpi is the dots-per-inch (pixel per inch).

For example:

fig = plt.figure(figsize=(8,4), dpi=100)

在这里插入图片描述

The same arguments can also be passed to layout managers, such as the subplots function.
Create a figure with plt.subplots() with figsize set to 12 by 3 inches and plot accordingly.

fig,ax = plt.subplots(figsize=(12,3))
ax.plot(x,y,'r-')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('title')

在这里插入图片描述

Saving figures

Save the above figure to file “filename.png”.

Note:

Matplotlib can generate high-quality output in a number formats, including PNG, JPG, EPS, SVG, PGF and PDF.

To save a figure to a file we can use the savefig method in the Figure class.

fig.savefig("filename.png")

Set dpi in saved file

Save the above figure to file “filenameDPI.png” and set dpi to 200.

fig.savefig('filenameDPI.png',dpi=200)

Legends

You can use label=“label text” keyword argument when ploting or when other objects are added to the figure, and then use legend() method without arguments to add the legend to the figure:

# Create a blank figure with plt.figure()
fig = plt.figure()

# Add an axis to the figure
ax = fig.add_axes([0,0,1,1])

# Plot (x, x^2) and (x, x^3) with labels set
plt.plot(x,x**2,label='x**2')
plt.plot(x,x**3,label='x**3')
# Call legend()
plt.legend();

在这里插入图片描述

The legend function takes an optional keyword argument loc that can be used to specify where in the figure the legend is to be drawn. The allowed values of loc are numerical codes for the various places the legend can be drawn. See the documentation page for details. Some of the most common loc values are:

ax.legend(loc=1) # upper right corner
ax.legend(loc=2) # upper left corner
ax.legend(loc=3) # lower left corner
ax.legend(loc=4) # lower right corner
ax.legend(loc=0) # let matplotlib decide the optimal location

Now, call legend() with loc set to 1 and then show the figure.

# Create a blank figure with plt.figure()
fig = plt.figure()

# Add an axis to the figure
ax = fig.add_axes([0,0,1,1])

# Plot (x, x^2) and (x, x^3) with labels set
plt.plot(x,x**2,label='x**2')
plt.plot(x,x**3,label='x**3')
# Call legend()
plt.legend(loc=1);

在这里插入图片描述

Setting linetypes

Plot the following figure accordingly.

fig=plt.figure()
x = np.linspace(0,5,11)
# Plot (x, x^2) with blue line and dots
plt.plot(x,x**2,'b-')
plt.plot(x,x**2,'bo')
# plot (x, x^3) with green dashed line
plt.plot(x,x**3,'g--');

在这里插入图片描述

Colors with the color= parameter

Plot the following figure accordingly.

Note:

We can define colors by their names or RGB hex codes and optionally provide an alpha value using the color and alpha keyword arguments. Alpha indicates opacity.

fig, ax = plt.subplots()

# Plot (x, x+1) with half transparant blue line
ax.plot(x,x+1,'b',alpha=0.5)

# Plot (x, x+2) with line of color #8B008B in RGB hex code
ax.plot(x,x+2,'#8B008B')

# Plot (x, x+2) with line of color #FF8C00 in RGB hex code
ax.plot(x,x+3,'#FF8C00')

在这里插入图片描述

Line and marker styles

Plot the following figure accordingly.

Note:

To change the line width, we can use the linewidth or lw keyword argument. The line style can be selected using the linestyle or ls keyword arguments.

fig, ax = plt.subplots(figsize=(12,6))

# plot the line width of 0.25, 0.5. 1. 2
# ax.plot(x, x, color="red", linewidth=0.25)
# ax.plot(x, x+1, color="red", linewidth=0.50)
# ax.plot(x, x+2, color="red", linewidth=1.00)
# ax.plot(x, x+3, color="red", linewidth=2.00)
# try to finish all the above jobs in a for loop

linewidth = [0.25,0.5,1,2]
for i in range(4):
    ax.plot(x,x+i,color='red',linewidth=linewidth[i])

在这里插入图片描述

fig, ax = plt.subplots(figsize=(12,6))

# possible linestype options '-', '--', '-.', ':'
# ax.plot(x, x, color="green", lw=3, linestyle='-')
# ax.plot(x, x+1, color="green", lw=3, linestyle='--')
# ax.plot(x, x+2, color="green", lw=3, ls='-.')
# ax.plot(x, x+3, color="green", lw=3, ls=':')
# try to finish all the above jobs in a for loop

linestyle = ['-','--','-.',':']
for i in range(4):
    ax.plot(x,x+i,color='green',lw=3,linestyle=linestyle[i])

在这里插入图片描述

fig, ax = plt.subplots(figsize=(12,6))

# possible marker symbols: marker = '.', ',', 'o', 'v', 
# '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 
# 'H', '+', 'x', 'D', 'd', '|', '_'

# ax.plot(x, x, color="blue", lw=3, ls='-', marker='.')
# ax.plot(x, x+1, color="blue", lw=3, ls='--', marker=',')
# ax.plot(x, x+2, color="blue", lw=3, ls='-.', marker='o')
# ax.plot(x, x+3, color="blue", lw=3, ls=':', marker='v')
# ...

# try to finish all the above jobs in a for loop
fig, ax = plt.subplots(figsize=(12,6))

# possible marker symbols: marker = '.', ',', 'o', 'v', 
# '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 
# 'H', '+', 'x', 'D', 'd', '|', '_'

# ax.plot(x, x, color="blue", lw=3, ls='-', marker='.')
# ax.plot(x, x+1, color="blue", lw=3, ls='--', marker=',')
# ax.plot(x, x+2, color="blue", lw=3, ls='-.', marker='o')
# ax.plot(x, x+3, color="blue", lw=3, ls=':', marker='v')
# ...

# try to finish all the above jobs in a for loop

marker = ['.',',','o','v','^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h','H', '+', 'x', 'D', 'd', '|', '_']
for i in range(len(marker)):
    ax.plot(x,x+i,color='blue',lw=1,linestyle='-',alpha=0.5,marker=marker[i])

在这里插入图片描述

fig, ax = plt.subplots(figsize=(12,6))

# marker size and color
# ax.plot(x, x, color="purple", lw=1, ls='-', marker='o', markersize=2)
# ax.plot(x, x+1, color="purple", lw=1, ls='-', marker='o', markersize=4)
# ax.plot(x, x+2, color="purple", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
# ax.plot(x, x+3, color="purple", lw=1, ls='-', marker='s', markersize=8, 
#         markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green")

# try to finish all the above jobs in a for loop with random markerfacecolor and markeredgecolor
fig, ax = plt.subplots(figsize=(12,6))

# marker size and color
# ax.plot(x, x, color="purple", lw=1, ls='-', marker='o', markersize=2)
# ax.plot(x, x+1, color="purple", lw=1, ls='-', marker='o', markersize=4)
# ax.plot(x, x+2, color="purple", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
# ax.plot(x, x+3, color="purple", lw=1, ls='-', marker='s', markersize=8, 
#         markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green")

# try to finish all the above jobs in a for loop with random markerfacecolor and markeredgecolor

clist = 'r y g k b m c'.split()
for i in range(len(clist)):
    ax.plot(x,x+i,color='purple',lw=1,linestyle='-',alpha=0.8,marker='o',markersize=2*i,
           markerfacecolor=clist[np.random.randint(len(clist))],
            markeredgecolor=clist[np.random.randint(len(clist))],
           markeredgewidth=2*i)

在这里插入图片描述

Plot range

Plot the following figure accordingly.

Note:

We can configure the ranges of the axes using the set_ylim and set_xlim methods in the axis object.

fig, axes = plt.subplots(1, 2, figsize=(12, 6))

# Plot (x, x^2) and (x, x^3) and set the title in the first set of axes


# Plot the above again in the other subplot and set ranges accordingly.
fig, axes = plt.subplots(1, 2, figsize=(12, 6))

# Plot (x, x^2) and (x, x^3) and set the title in the first set of axes
axes[0].set_title('default axes ranges')
axes[1].set_title('custom axes range')

axes[0].plot(x,x**2,x,x**3)
# Plot the above again in the other subplot and set ranges accordingly.
axes[1].plot(x,x**2,x,x**3)
axes[1].set_xlim((2,4))
axes[1].set_ylim((0,50))

在这里插入图片描述

Special Plot Types

There are many specialized plots we can create, such as barplots, histograms, scatter plots, and much more.

scatter()

Now, try scatter(x, y)

x = np.linspace(0,5,11)
y = x**2
plt.scatter(x,y)

在这里插入图片描述

hist()

Generate 100 random samples from range(1, 1000) and plot the histogram.

from random import sample
data = sample(range(1, 1000), 100)

# plot the histogram with data
plt.hist(data)

在这里插入图片描述

boxplot()

First, generate three sets of normal distributed data. Each has 100 samples with mean of 0. And their standard deviations are 1, 2, 3 respectively.

Then plot them with boxplot in vertical form and set patch_artist to True.

# generate data
data = [np.random.normal(0, std, 100) for std in range(1, 4)]

# plot the data with rectangular box plot
plt.boxplot(data,patch_artist=True);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值