MatplotlibMatplotlib架构概述
Less is more effective
Less is more attractive
Less is more impactive
这⾥记下关于Matplotlib体系结构的笔记,主要是上课内容加上⾃⼰的资料,夹英夹中,⼤家可以当个参考。
Matplotlib架构概述
Matplotlib architecture
matplotlib中subplotMatplotlib体系结构分为三层,可以将其视为堆栈。位于另⼀层之上的每⼀层都知道如何与它下⾯的层进⾏通信,但是下层却不知道它上⾯的层。从下到上的三层是:Backend, Artist, Scripting Layer.
Backend Layer (FigureCanvas, Renderer, Event)
Has three built-in abstract interface classes:
FigureCanvas: matplotlib.backened_bases.FigureCnvas
Encompasses the area onto which the figure is drawn
例如画纸
Renderer: matplotlib.backened_bases.Renderer
Knows how to draw on the FigureCanvas
例如画笔
Event: matplotlib.backend_bases.Event
Handles user inputs such as keyboard strokes and mouse clicks
Artist Layer (Artist)
Comprised of one main object - Artist:
Knows how to use the Renderer to draw on the canvas.
在matplotlib中看到的所有内容Figure都是⼀个 Artist实例。
Title, lines, tick labels, and images, all correspond to individual Artist instances.
Two types of Artist objects:
Primitive: Line2D, Rectangle, Circle, and Text
Composite: Axis, Tick, Axes, and Figure
Each artist may contain other composite artists as well as primitive artists.
⼀个⽤Artist作图例⼦~
# Putting the Artist Layer to Use
# generate a histogram of some data using the Artist layer
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas # import FigureCanvas
from matplotlib.figure import Figure # import Figure artist
fig = Figure()
canvas = FigureCanvas(fig)
# create 10000 random numbers using numpy
import numpy as np
x = np.random.randn(10000)
ax = fig.add_subplot(111)# create an axes artist
ax.hist(x,100)# generate a histgram of the 10000 numbers
# add a little to the figure and save it
ax.set_title('Normal distribution with $\mu=0, \sigma=1$')
fig.savefig('matplotlib_histogram.png')
Scripting Layer (pyplot)
⽇常⽤途,更简洁
Comprised mainly of pyplot, a scripting interface that is lighter that the Artist layer.
Let’s see how we can generate the same histogram of 10000 random values using the pyplot interface
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(10000)
plt.hist(x,100)
plt.title(r'Normal distribution with $\mu=0, \sigma=1$')
plt.savefig('matplotlib_histogram.png')
plt.show()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论