Matplotlib Artist 1 概览
发布时间
阅读量:
阅读量
Matplotlib API中有三层
- matplotlib.backend_bases.DrawArea:Draw Region
- matplotlib.backend_bases.DrawingController:Controls the Drawing Process on a Draw Area
- matplotlib.artist.DrawingObject:Controls the Rendering Process for Draw Objects
开发者95%的时间都是在使用Artist。Artist有两种类型:
- primitives(基本元素):涉及Line2D等。
- containers(存储结构):用于包裹组件的存储结构,包括Axis等。
基于常规做法,首先生成一个Figure实例F;随后借助F生成一个或多个Axes实例A;最后利用A提供的辅助功能构建基础元素。
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1) # two rows, one column, first plot
Axes是最常用的绘图类工具,在matplotlib中广泛使用。它提供了丰富的辅助功能模块(例如plotting函数如plot, text, hist, imshow),每个模块都能生成相应的图形元素。这些功能模块会根据输入数据自动生成相应的图形组件,并将它们组织在一个容器中等待渲染。为了手动设置Axes的位置,在创建Figure时可以使用add_axes方法,并传递一个包含left, bottom, width和height四个参数的列表。(这里的数字都是基于Figure的0-1坐标系统的数据)
fig2 = plt.figure()
ax2 = fig2.add_axes([0.15, 0.1, 0.7, 0.3])
在调用Axes的plot方法时,默认会生成并绘制一个Line2D对象
import numpy as np
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
line, = ax.plot(t, s, color='blue', lw=2)
通过ax.lines方法可以看到,在ax中lines与ax.plot()返回的实际是一个相同的Line2D实例

当继续调用ax.plot()时,生成的Line2D对象将会加入ax.lines列表中,并且可以通过调用remove()方法来移除该对象。
line = ax.lines[0]
line.remove()

在Matplotlib库中也存在设置横坐标轴、纵坐标轴刻度线以及它们的标记的方法;每个Axes实例都包含x轴和y轴的tick instances。
xtext = ax.set_xlabel('my xdata') # returns a Text instance
ytext = ax.set_ylabel('my ydata')
fig = plt.figure()
fig.subplots_adjust(top=0.8)
ax1 = fig.add_subplot(211)
ax1.set_ylabel('volts')
ax1.set_title('a sine wave')
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
line, = ax1.plot(t, s, color='blue', lw=2)
# Fixing random state for reproducibility
np.random.seed(19680801)
ax2 = fig.add_axes([0.15, 0.1, 0.7, 0.3])
n, bins, patches = ax2.hist(np.random.randn(1000), 50,
facecolor='yellow', edgecolor='yellow')
ax2.set_xlabel('time (s)')
plt.show()


Artist对象都包含以下属性
| Property | Description |
|---|---|
| alpha | The transparency - a scalar from 0-1 |
| animated | A boolean that is used to facilitate animated drawing |
| axes | The Axes that the Artist lives in, possibly None |
| clip_box | The bounding box that clips the Artist |
| clip_on | Whether clipping is enabled |
| clip_path | The path the artist is clipped to |
| contains | A picking function to test whether the artist contains the pick point |
| figure | The figure instance the artist lives in, possibly None |
| label | A text label (e.g., for auto-labeling) |
| picker | A python object that controls object picking |
| transform | The transformation |
| visible | A boolean whether the artist should be drawn |
| zorder | A number which determines the drawing order |
| rasterized | Boolean; Turns vectors into raster graphics (for compression & EPS transparency) |
全部评论 (0)
还没有任何评论哟~
