First session: Matplotlib

Get to know Matplotlib

Matplotlib is a Python 2D drawing library that produces publically quality graphics in a variety of hard copy formats and cross-platform interactive environments for drawing static, dynamic, and interactive charts.

Matplotlib is one of the most popular data visualization tools in Python. The interfaces for manipulating pandas and Seaborn are also based on Matplotlib.

2. Matplotlib

  1. Explicitly create figures and axes and call drawing methods on them, also known as object-oriented style
import matplotlib.pyplot as plt
import numpy as np
# Two drawing interfaces
# 1. Object-oriented thinking OO

x = np.linspace(0.2.100)
# Create FIG and AXES objects by display, object-oriented thinking

fig, ax = plt.subplots()

Add the information in the graph through the AX object
# Add line information
ax.plot(x, x, label='linear')
ax.plot(x, x ** 2, label='quadratic')
ax.plot(x, x ** 3, label='cubic')

Add axis information
ax.set_xlabel('x label')
ax.set_ylabel('y label')

Set the table header
ax.set_title('Simple Plot')

# set the title
ax.legend()
# display image
plt.show()

Copy the code

  1. Rely on PyPlot to automatically create figures and axes and plot them
import matplotlib.pyplot as plt
import numpy as np

# Two drawing interfaces
# 2. Rely on PyPlot to automatically create figure and axes and plot
x = np.linspace(0.2.100)
# Create FIG and AXES objects by display, object-oriented thinking

# Add lines
plt.plot(x, x)
plt.plot(x, x ** 2)
plt.plot(x, x ** 3)

# display legend
# plt.legend()
You can add the name of each line to the List when displaying the legend
plt.legend(['linear' 'quadratic'.'cubic'])
# display the axis name
plt.xlabel('x label')
plt.ylabel('y label')
# display title
plt.title('Simple plot')

plt.show()

Copy the code

3. Composition of Figure

A Figure is presented in a complete image window.

  • Figure: an image window, a program can create multiple figures, each Figure through a window display;
  • Axes: concept of a subgraph in a Figure, one Figure can have multiple Axes;
  • The next level of Axes, one Axes can have multiple Axes (several dimensions, 2 for 2), which are used to set elements related to Axes and grids;
  • The next level of Tick: Axes, used to set the elements associated with the coordinate scale of each axis;

1. Setting of Figure

import matplotlib.pyplot as plt
import numpy as np

# Two drawing interfaces

x = np.linspace(-2.2.100)
# Create FIG and AXES objects by display, object-oriented thinking


# Add to different figures
plt.figure()
Set the name and size of the figure
# plt.figure(num=3, figsize=(10, 10))
# Add lines
plt.plot(x, x)

plt.figure()
# Add lines
plt.plot(x, x ** 2)
# Change the style of the line
plt.plot(x, x ** 3, color='red', linewidth=2, linestyle=The '-')

plt.show()
Copy the code

Image display:

2. Setting of coordinate axes

import matplotlib.pyplot as plt
import numpy as np

# Two drawing interfaces

x = np.linspace(-2.2.100)
# Create FIG and AXES objects by display, object-oriented thinking


# Add to different figures
plt.figure()
# plt.figure(num=3, figsize=(10, 10))
# Add lines
plt.plot(x, x)
plt.plot(x, x ** 2, color='red', linewidth=2, linestyle=The '-')

# Axis Settings

# Axis range
plt.xlim(-1.2)
plt.ylim(0.1)

The name of the axis
plt.xlabel('I am X')
plt.ylabel('I am Y')

The ticks and the conversion of the ticks are displayed

The scale of # x is shown in 10 portions from -2 to 2
plt.xticks(np.linspace(-2.2.10))
The scale transformation of # y
plt.yticks([0.0.2.0.4.0.6.0.8.1],
           ['a'.'b'.'c'.'d'.'e'.'f'])

Settings for # axes
ax = plt.gca()
# set the right border not to show color
ax.spines['right'].set_color('none')
# set the left border not to show color
ax.spines['top'].set_color('none')
# set X-axis as the lower boundary
ax.xaxis.set_ticks_position('bottom')
# set the y axis as the left boundary
ax.yaxis.set_ticks_position('left')
The way the y axis changes position
ax.spines['left'].set_position(('data', -2))
The x axis changes position
ax.spines['bottom'].set_position(('data'.0))

plt.show()

Copy the code

Image display:

3. Display of legends

import matplotlib.pyplot as plt
import numpy as np

# Two drawing interfaces

x = np.linspace(-2.2.100)
# Create FIG and AXES objects by display, object-oriented thinking


# Add to different figures
plt.figure()
# plt.figure(num=3, figsize=(10, 10))
# Add lines
l1, = plt.plot(x, x)

l2, = plt.plot(x, x ** 2, color='red', linewidth=2, linestyle=The '-')

# Axis Settings

# Axis range
plt.xlim(-1.2)
plt.ylim(0.1)

The name of the axis
plt.xlabel('I am X')
plt.ylabel('I am Y')

# Legend setup
# handles: line objects
# lebels: the name of the corresponding line in handles
# LOC: best: automatically adapt to find the location of less data display
plt.legend(handles=[l1, l2, ], labels=['line1'.'line2'], loc='best')

plt.show()

Copy the code

Image display: