Matplotlib Beginner Tutorial

MATLAB

MATLAB is a combination of the words Matrix & Laboratory, meaning matrix factory (matrix laboratory). It is a high-tech computing environment mainly for scientific computing, visualization and interactive programming released by Mathworks, Inc.

It integrates many powerful functions such as numerical analysis, matrix calculation, scientific data visualization, and modeling and simulation of nonlinear dynamic systems in an easy-to-use Windows environment, providing a comprehensive solution for scientific research, engineering design, and many scientific fields where effective numerical calculations must be performed, and largely free from the editing mode of traditional non-interactive programming languages (such as C, Fortran), representing the advanced level of today’s international scientific computing software.

Matplotlib

Matplotlib, the most widely used data visualization library in Python, was first developed by John Hunter in 2002 with the aim of building a Matlab-style interface to plotting functions. It takes advantage of Python’s numeric and Numarray modules, and clones many of Matlab’s functions to help users easily obtain high-quality 2D graphics.

Matplotlib can draw many kinds of graphs including ordinary line graphs, histograms, pie charts, scatter plots, and error line graphs, etc. It is easy to customize various properties of graphs such as the type of lines, color, thickness, font size, etc. It supports some of the TeX layout commands and can display mathematical formulas in graphs in a beautiful way.

Matplotlib is also very easy to master, because most of the functions used in Matplotlib have the same name as the corresponding functions in Matlab, and the meanings of various parameters and usage methods are the same, which makes users familiar with Matlab feel comfortable to use. For those who are not familiar with Matlab, the meaning of these functions is often clear at a glance, so it takes very little time to master them.

I have used MATLAB a lot over the years for data analysis and visualization. MATLAB is good at drawing nice graphs. When I started working with EEG data, I found that I needed to write applications to interact with my data, and developed an EEG analysis application in MATLAB. As the application became more complex, needing to interact with databases, http servers, and manipulate complex data structures, I began to struggle with the limitations of MATLAB as a programming language and decided to migrate to Python. Python as a programming language made up for all of MATLAB’s shortcomings, but I had a hard time finding a 2D drawing package (3D VTK, on the other hand, exceeded all my needs).

When I went looking for a Python drawing package, I had a few requirements.

  • The drawing should look good - release quality. An important requirement for me is that the text looks good (anti-aliased, etc.)
  • Postscript output for containing TeX documents
  • embeddable GUI for application development
  • The code should be easy enough that I can understand it and extend it
  • Drawing should be easy

Not finding a package that fit my needs, I did what any self-proclaimed Python programmer would do: rolled up my sleeves and started building my own. I didn’t have any real experience with computer graphics and decided to emulate MATLAB’s plotting capabilities because MATLAB does it so well. This had the added advantage that many people had a lot of experience with MATLAB, so they could quickly start plotting in python. From a developer’s point of view, having a fixed user interface (the pylab interface) is very useful because the contents of the code base can be redesigned without affecting the user code.

Matplotlib architecture

The Matplotlib architecture is logically divided into three layers, located on three different levels. The upper level content can communicate with the lower level content, but the lower level content cannot communicate with the upper level content. The three layers, in order from top to bottom, are

  • Scripting (scripting layer)
  • Artist (presentation layer)
  • Backend (backend layer)

The access relationship between them is: Scripting accesses Artist, Artist accesses Backend

Backend

Backend is the bottom layer, that is, the matplotlib API layer. backend in the mpl.backend_bases class, which contains a variety of keyboard, mouse Event class, used to draw lines, graphics RenderBase class, responsible for the Figure object and its output is separated from the FigureCanvasBase class and so on (just an interface, specific different outputs, such as PDF, PNG definition in the backends class). That is, all derived / output methods are placed under mpl.backends, such as Qt5 backend for mpl.backends.qt5agg. output PDF backend for mpl.backend_pdf, where the class is responsible for interacting with different backends / platforms. This design ensures the decoupling of the code and the unity of design.

Different backends provide different APIs, which are defined in the backends class. This layer contains the matplotlib API, which is a collection of several classes that are used to implement various graphical elements in the underlying layer. The main ones include.

  • FigureCanves: used to represent the drawing area of the graph
  • Renderer: the object used to draw the content on the FigureCanves.
  • Event: object used to handle user input (keyboard, mouse input)

Artist

If what the Backend layer does is establish an architecture of Canvas, Renderer, Event, then what the Artist layer does is tell the program what to draw on the Renderer to the Canvas. This layer doesn’t have to touch the backend, doesn’t have to relate to the differences between different implementations, and just tells the Backend layer what it needs to DRAW.

All elements are instances/subclasses of the mpl.artist.Artist class. artist Module contains a large number of already defined elements, including Tick scales, Text text, Patch composite graphics (axes, shadows, circles, arrows, Rectangle, etc.), Figure graphics, Legend labels, Line2D The elements in the artist Module interact and pass information through the draw method and the backend Module, which is unidirectional.

The middle layer of the entire architecture, where all the various elements used to compose an image are located, such as titles, axes, axis labels, markers, and other content are instances of Artist. And these elements form a hierarchical structure.

Artist layer has many visual elements, i.e. title, axis labels, scale, etc. Artist is divided into two categories, Primitive Artist and Composite Artist.

  • Primitive Artist is a standalone element containing basic shapes, such as a rectangle, a circle or a text label
  • Composite Artist is a combination of these simple elements, such as horizontal coordinates, vertical coordinates, graphs, etc.

When dealing with this layer, it is mainly the upper layer of the graphical structure that you deal with, and you need to understand what each of these objects represents in the graph. The following figure shows the structure of an Artist object.

The top layer of Artist is the Figure layer, representing the graphical concept; Axes is on top of Figure, representing what the axes draw (i.e. axis objects), and there will be one Axes object for each dimension; Axis is used to display the values on the axes (i.e. scales and scale values), the position of the scales is managed by Locator object, and the label format of the scales is adjusted by Formatter object.

Scripting

If the previous layer represents HTML, then this layer represents JavaScript, and you can think of this layer as an interactive interface with state persistence. Like Python’s interactive interpreter. mpl.pyplot is the entire entry point for this layer.

This layer contains a pyplot interface. This package provides the classic Python interface to manipulate matplotlib programmatically based, it has its own namespace, and requires importing the NumPy package.

Graphing with Matplotlib Layers

Drawing with Artist Layer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Step One: We import FigureCanvas from Back-end layer.
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

# Step two: We import Figure Artist
from matplotlib.figure import Figure

# Step 3: We create a Figure object using the constructor
fig = Figure()

import numpy as np
np.random.seed(6)
x = np.random.randn(20000)

# Next we create an Axes Artist object.
ax = fig.add_subplot(111)

# Next we call the Axes method hist() function.
ax.hist(x, 100)

# Finally we assign a title to the plot.
ax.set_title('Artist Layer Histogram')

# And we save it, in this case as a PNG file. 
fig.savefig('Matplotlib_histogram.png')

  • ax = fig.add_subplot(111): follows the MATLAB convention. Creates a grid containing one row and one column and uses the first cell in the grid as the location of the execution object.
  • ax.hist(x,100): Creates a rectangular sequence of original Artistlayer objects for each bar and adds them to the ExesContainer. 100 means 100 bar bars are generated.

Drawing with Scripting Layer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(6)
X = np.random.randn(20000)

plt.hist(X, 100)
plt.title('Scripting Layer Histogram')
plt.savefig('matplotlib_histogram.jpg')

Artist or Scripting Layer?

Obviously, using the Scripting layer (matplotlib.pyplot) is relatively easier to use, lighter, and has a simpler syntax. Although the Artist layer is preferred by professional developers. But it is better to master both.

Matplotlib image structure

  • anvas (drawing board) is located at the bottom, generally not accessible to users
  • Figure (canvas) is built on top of Canvas
  • Axes are built on top of Figure
  • Auxiliary display layers such as axis, legend, and image layers are built on top of Axes

Container layer

The container layer mainly consists of Canvas (canvas), Figure (graphics, drawing area), Axes (axis)

  • Canvas is the system layer at the bottom of Canvas, which acts as a drawing board in the process of drawing, i.e., the tool that places the Figure (drawing area)
  • Figure is the first layer above the Canvas and the first layer of the application layer that needs to be manipulated by the user, acting as the canvas in the drawing process.
  • Axes is the second layer of the application layer, which acts as the drawing area on the canvas during the drawing process.
  • Figure: means the whole figure (you can set the size and resolution of the canvas by figure(), etc.)
  • Axes(coordinate system):the drawing area of the data
  • Axis: an axis in the coordinate system, containing size limits, scales and scale labels

Features.

  • A figure(canvas) can contain multiple axes(coordinate system/plotting area), but an axes can belong to only one figure.
  • An axes (coordinate system/drawing area) can contain multiple axes (axes), including two for 2d coordinate system and three for 3d coordinate system

The auxiliary display layer is the content of Axes (drawing area) other than the image drawn from the data, mainly including

  • Axes appearance (facecolor)
  • Border lines (spines)
  • Axes (axis)
  • Axes name (axis label)
  • Axes scale (tick)
  • axis label * tick label
  • grid
  • legend
  • title

This layer can be set to make the image display more intuitive and easier to understand by the user, but does not have a substantial impact on the image.

Auxiliary display layer

The auxiliary display layer is the content of Axes (drawing area) other than the image drawn from the data, mainly including

  • Axes appearance (facecolor)
  • Border lines (spines)
  • Axes (axis)
  • Axes name (axis label)
  • Axes scale (tick)
  • axis label * tick label
  • grid
  • legend
  • title

This layer can be set to make the image display more intuitive and easier to understand by the user, but does not have a substantial impact on the image.

Image layer

The image layer refers to the image layer within Axes via

  • plot (line graph)
  • scatter (scatter plot)
  • bar (bar chart)
  • histogram
  • pie

Drawing with PYPLOT

plt.plot() plots

Quickly get started with plot() plotting

This method of plotting graphs is done entirely through the pyplot interpretation layer. When the plt.plot() method is called, if the system does not detect the existence of a Figure object and an Axes object, one is created, and if detected, it is used directly. The backend selected after creation is the default backend, and the canvas is generally of class mpl.backends.backends_xxagg.

1
plt.plot(np.random.randn(50).cumsum(), 'r--.')

pyplot.plot can accept drawing objects and some command-line like parameters, with a choice of colors, dotted line states (horizontal lines, vertical lines, stars), and various other shapes. plot’s parameters, see Line 2D, can be specified as follows.

1
plot(x, y, color='green', linestyle='dashed', marker='o',markerfacecolor='blue', markersize=12)

pyplot can be handled directly in a process-oriented manner, and calling plt.axis will set the axis property in the current Axes.

1
2
3
4
5
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [2,21,23,11], "r^", [12,21,43,21], "b", [0,2,12,40,18], "y--")#x轴、y轴和其颜色、类型、第二个y轴和其颜色、类型、第三个y轴和其颜色、类型
plt.title("My First plt Figure")
plt.axis([-1,5,-10,50]) #sets the min and max of the x and y axes, with v = [xmin, xmax, ymin, ymax]
plt.show()

subgraphics, labels, scales, text

A process-oriented approach allows us to do image plotting quickly, with some of the more important methods, such as

  • subplot(m,n,a) Plot multiple graphs on a single Figure and Axes, and you can return to this subplot for modification at any time.
  • subplots_adjust() Quickly adjusts the graph spacing
  • title() Set the title
  • grid() sets the grid
  • xlabel() sets the x-axis label
  • xticks() sets the x-axis scale
  • legend() controls the legend display
  • text() places text at the specified location
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from matplotlib.ticker import NullFormatter
import numpy as np

# 设置x轴和y轴的数据
t = np.arange(0, 5, 0.1)
y1 = np.sin(2 * np.pi * t)
y2 = np.sin(2 * np.pi * t)

# 设置整体图形外观
plt.subplots_adjust(top=2, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35)

# plt.title("My First plot") not work here
# 设置第一个子图形的标题、绘图、标签、网格
plt.subplot(411)  # 用来水平切分4个,垂直切分1个,调用第1个图形
plt.title("My First plot", fontsize=14)  # only work here
plt.plot(t, y1, "b-.")  # 在这里还可以设置绘制的点的样式、颜色等
plt.ylabel("Value-M1")
plt.grid(True)

# 同上,调用第二个图形,设置文本和图例
plt.subplot(412)
plt.plot(t, y2, "r--")
plt.ylabel("Value-M2")
plt.text(1, 0.5, "text1")
plt.text(3, 0.3, "text2")
plt.legend(["Legend"], loc=1)  # 图例位置和文本内容

# 同上,调用第三个图形,gca返回当前instance,设置刻度
plt.subplot(413)
plt.plot(t, y1, "p-.")
plt.gca().xaxis.set_minor_formatter(NullFormatter())  # gca获取当前axes,set..设置刻度

plt.subplot(411)
# 你可以在任意时候,回到之前绘制的子图中继续在其上绘制其他图案。比如:
# plt.plot(t,y1,"p-.") #这条命令会绘制在图1中,并且和之前图1存在的内容并存
plt.show()

Figure and Axes: graphs, subgraphs and their axes

Figure and Axes objects

1
2
class matplotlib.figure.Figure
(figsize=None, dpi=None, facecolor=None, edgecolor=None, linewidth=0.0, frameon=None, subplotpars=None, tight_layout=None)

Figure has many more methods, the frequent ones are

  • add_axes(rect) Add sub axes
  • add_subplot(mna) Add a subplot
  • clear() Clears the image
  • draw(renderer) The underlying interface
  • gca() Current Axes
  • get_children() Get child elements
  • hold() Ugly MATLAB compatibility
  • savefig() Save the image
  • set_canvas() underlying interface
  • show() Show the image
  • subplots_adjust() subplots fine-tuning
  • text() Place text
1
2
class matplotlib.axes.Axes
(fig, rect, facecolor=None, frameon=True, sharex=None, sharey=None, label='', xscale=None, yscale=None, axisbg=None, **kwargs)

Axes has many methods, there are about 100 individual methods, so no examples will be given here, consult the manual.

Calling and creating Figure and Axes objects

Plotting with plt is certainly convenient, but sometimes we need to fine tune and generally need to use.

  • gca() returns the Axes object in the current state, the Alias of Fig. gca()
  • gca().get_children() Convenient to see the elements under the current Axes
  • gcf() returns the Figure object in its current state, which is generally used to iterate through the Axes of multiple figures (plt.gcf().get_axes()). Another way is to use the index of the Axes matrix to extract the Axes of a subplot.

For those who want to redraw a graph instead of drawing it in a Figure and distinguishing it into several subplots, create a new Figure object directly using plt.figure(), which can also be captured using plt.gcf(). Each figure can be composed of many subplots, each of which has only one Axes, and they can share axes. Each subplot has its own AxesSubplot subaxis, contained in the total Axes.

Just as the interaction between the Figure object and the Canvas object is performed with canvas = FigureCanvasBase(fig), the association between the Figure object and the Axes object can be performed using the following methods.

  • axes = plt.subplot(mna) # MATLAB way, generally used only for interaction, not capture the output
  • Axes = mpl.axes.Axes(fig,*kwargs # OOP approach, less commonly used
  • fig, axes_martix = plt.subplots(nc,nr) # Fast way, suitable for fast creation of Figure objects and Axes objects, generally used to create plots with a specified number of subplots.
  • axes = fig.add_subplot(mna) # Suitable for creating a single subplot when Figure objects are available, similar to subplot(). The advantage over subplots is that you can use gridspec to draw plots with X-Y axis dependent graphs.
  • axes = fig.add_axes([rect]) # Suitable for adding a small subplot and its Axes at a specified location of a graph. generally used as a drawing of subsidiary graphs.

An example: the OOP approach

1
2
3
4
5
6
7
8
9
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure() #调用一个新的画布
fig2 = plt.gcf() # fig2 == fig1 => True

ax_sub = fig.add_subplot(2, 2, 1) #添加子画布,2×2大小,第一个
ax_sub2 = fig.add_subplot(2, 2, 2) #添加子画布,2×2大小,第二个
ax_sub3 = fig.add_subplot(2, 2, 3) #添加子画布,2×2大小,第三个
# 需要注意,add_subplot返回的是sub axes对象

An example: plt.subplots method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
%matplotlib inline
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
#快速创建图形 Fig 和 Axes Martix
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
#或者使用 fig, ((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = plt.subplots(3,3)
print(ax)

# plot the linear_data on the 5th,8th subplot axes
ax[2][2].plot(x,y, '-')
ax[1][1].plot(x,y**2, '-')

# 对于一个包含多个轴的fig,可以使用 gcf().get_axis,或者直接对矩阵进行索引和操纵
for ax in plt.gcf().get_axes():
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(True)
        
# necessary on some systems to update the plot
plt.gcf().canvas.draw()

[[<matplotlib.axes._subplots.AxesSubplot object at 0x7f57f8c3b310>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f8bec110>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f86c11d0>]
 [<matplotlib.axes._subplots.AxesSubplot object at 0x7f57f8b16290>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f84e7350>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f86ed410>]
 [<matplotlib.axes._subplots.AxesSubplot object at 0x7f57f8ca8a10>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f857ecd0>
  <matplotlib.axes._subplots.AxesSubplot object at 0x7f57f855ce50>]]

Common methods of drawing

Firure common methods

  • subplots_adjust is equivalent to calling subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=None) top function for adjusting the presentation of width, height, etc. around the subplot.
  • savefig(fname,dpi,bbox_inches,format) to save the image

Axes common methods

  • hist bar/pie pie/box box etc.
  • plot If you don’t specify the type of plot, such as pie or bar, just pass in plot and fill in the parameter as data.
  • add_patch Add a graph
  • annotate Add arrows
  • text Add a text description
  • get(set)_x(y)lim Get/set the range of different axes
  • g/s_(x/y)ticks Set the scale position #plt.xticks((0,1,2),(“a”, “b”, “c”)) can Quickly specify the position and label value.
  • g/s_(x/y)ticklabels Set tick labels
  • g/s_(x/y)label sets the axis name
  • title Header settings
  • legend setting

For Axes call get/set and many other methods, in fact, you can also directly call plt global objects, but call axes individual elements can get more fine-grained control, and more object-oriented some.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fig, ax = plt.subplots(2, 3, sharex=True, sharey=True)
fig.subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=0, hspace=0) 
#也可以使用plt顶级函数
#histogram 柱状图
ax[0,0].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)
ax[0,1].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)
ax[0,2].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)
ax[1,0].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)
ax[1,1].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)
ax[1,2].hist(np.random.randint(1,500,size=100),bins=50,color="k",alpha=0.5)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
fig.savefig("hello.png",format="png", dpi=100,bbox_inches="tight")
#两个较为重要的参数是bbox_inches控制边距,第二个是dpi控制精度

fig,ax = plt.subplots()
ax.plot(np.random.randn(500).cumsum(),label="a") 
#此处设置plot的label值,即可以调用legend时自动生成图例。
ax.plot(np.random.randn(500).cumsum(),label="b")
ax.set_xticks([0,150,300,450,600])
ax.set_xticklabels(["one","two","three","four","five","six"]) #自动跳过了six
#也可以直接写ax.xticks([真实数值],[对应标签名称])
ax.set_title("table",loc="center")
ax.set_xlabel("numbers")
ax.set_ylabel("values")
ax.legend(loc="best") #legend图例,在plot时传入label进行创建
ax.text(0,10,"Hello World")
# matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=False, **kwargs)
ax.annotate("", xy=(0, 0), xycoords='data', xytext=(400,10), textcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),)

SPINES coordinate system plotting

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
%matplotlib inline
fig ,ax = plt.subplots()
x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(x)/x

ax.plot(x,y,"r")
ax.plot(x,y2,"b")
ax.plot(x,y3,"g")

print(plt.gca(),ax)
# AxesSubplot(0.125,0.125;0.775x0.755) AxesSubplot(0.125,0.125;0.775x0.755)

ax.spines["right"].set_visible(False)
#class matplotlib.spines.Spine(axes, spine_type, path, **kwargs)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0))

PATCH graph drawing

Graph drawing requires calling the axes.add_patch() method, passing in parameters for the graph, i.e. the line/path/patches object. Very similar to Qt graph drawing, the underlying API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import matplotlib.path as mpath
import matplotlib.patches as mpatches
fig = plt.figure(figsize=(12,6))
axes = fig.add_subplot(1,2,1)
grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T
patches = []
# add a circle
circle = mpatches.Circle(grid[0], 0.1, ec="none")
patches.append(circle)
# add a rectangle
rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")
patches.append(rect)
axes.add_patch(patches[0]);axes.add_patch(patches[1])

AXES3D charting

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from mpl_toolkits.mplot3d import Axes3D

x = np.arange(8)#生成数据
y = np.random.randint(0,10,8)
y2 = y + np.random.randint(0,3,8)
y3 = y2 + np.random.randint(0,3,8)
y4 = y3 + np.random.randint(0,3,8)
y5 = y4 + np.random.randint(0,3,8)

fig = plt.figure() ; ax = Axes3D(fig) #普通方法调用fig和ax
#或者采用ax = fig.add_subplot(111, projection='3d')绘制子图形。
# For those using older versions of matplotlib, 
#change ax = fig.add_subplot(111, projection='3d') to ax = Axes3D(fig).

ax.bar(x,y,0,zdir="y")#数据和作为第三轴的轴:y
ax.bar(x,y2,10,zdir="y")
ax.bar(x,y3,20,zdir="y")
ax.bar(x,y4,30,zdir="y")
ax.bar(x,y5,40,zdir="y")
ax.set_xlabel("X label")#标签
ax.set_ylabel("Y label")
ax.set_zlabel("Z label")
ax.view_init(elev=50)#视角
ax.grid(False)
#3D设置在class mpl_toolkits.mplot3d.axes3d.Axes3D(fig, rect=None, *args, **kwargs)

Subplot grid and multi-panel plotting

The difference with fig.add_subplot(), use fig.add_axes() method to manually create a new Axes, but does not formulate the location of this Axes, you need to manually specify, generally used as a subsidiary graphics drawing, after calling this subaxes, you can draw on the subsidiary graphics.

1
2
3
4
5
fig = plt.figure()
ax_1 = fig.add_axes([0.1,0.1,1,1]) #此处定义的是图表的 rect [left, bottom, width, height]
ax_2 = fig.add_axes([0.7,0.7,0.3,0.3])
ax_1.plot(np.random.randint(0,10,50),"ro")
ax_2.plot(np.random.randint(0,10,3),"g--")

In the introduction of add_subplot(), you can define “12X” here to mean 1 horizontal and 2 vertical subplots, and select the Xth one. However, for precise position control, you can use the GridSpec object, pass in a square size, and then make a slice call to add_subplot() as an argument, so that you can do fine subplot control.

1
2
3
4
5
6
7
8
fig = plt.figure()
gs = plt.GridSpec(3,3) #新建一个网格
fig.add_subplot(gs[1,:2]).plot(np.random.randint(0,10,10),"o") #不再传入1,2,1,而是传入
s2 = fig.add_subplot(gs[0,:2])
s2.plot(np.random.randint(0,10,10),"o")
fig.add_subplot(gs[2,0]).plot(np.random.randint(0,10,10),"o")
fig.add_subplot(gs[:2,2]).plot(np.random.randint(0,10,10),"o")
fig.add_subplot(gs[2,1:]).plot(np.random.randint(0,10,10),"o")

The following is an example of an application of Gridspec.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from matplotlib.gridspec import GridSpec
import numpy as np

fig = plt.figure()
gs = GridSpec(3,3)
main = fig.add_subplot(gs[1:,1:])
hist = fig.add_subplot(gs[1:,0:1])
hist_x = fig.add_subplot(gs[0:1,1:])

data_x = np.random.random(size=10000)
data_y = np.random.normal(size=10000)

main.scatter(data_x,data_y)
main.set_xlim(0,1)
hist.hist(data_y,orientation='horizontal',bins=1000) #水平显示
hist.invert_xaxis() #反转X轴以方便比较数据
hist_x.hist(data_x,bins=1000)
hist_x.set_xlim(0,1)

GCA advanced usage

The plotting can be finely controlled using plt.gca(), e.g.

1
plt.gca().fill_between(x,y1,y2,alpha,color)

It is possible to draw a filled graph consisting of two curves, which is useful when representing areas, such as integral areas. In addition, it can be used in this way.

1
2
3
4
5
6
7
xaxis = plt.gca().get_xaxis() #从AXES实例中获取XAXIS实例

for item in xaxis.get_ticklabels(): #从XAXIS实例中获取TICKLABELS
    item.set_alpha(1) #TICKLABELS由TEXT实例构成,TEXT实例有很多方法
    item.set_rotation(45)
    item.set_backgroundcolor('r')
    item.set_color('w')

gca() is essentially an Axes object, and a review of the documentation shows that this object has many available methods, such as in this case taking out the x sub-axis and traversing the scale labels of this axis (Text object), setting the rotation, transparency and color, background, for longer time dates, so that the axis text is not crowded together.

Matplotlib style

The generated graphs can be easily embellished using several embellishment styles that come with matplotlib. You can use matplotlib.pyplot.style.available to get all the beautification styles:

1
2
import matplotlib.pyplot as plt
print(plt.style.available)

Check the specific official website see the style reference sheet.

Beautify using the self-contained style of.

1
2
3
4
5
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# 使用自定义样式:
plt.style.use('./images/presentation.mplstyle')
plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-light.mplstyle')