|
1 | 1 | # 艺术家教程
|
2 | 2 |
|
| 3 | +> 原文:[Artist tutorial](http://matplotlib.org/users/artists.html) |
| 4 | +
|
| 5 | +> 译者:[飞龙](https://github.com/) |
| 6 | +
|
| 7 | +> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) |
| 8 | +
|
3 | 9 | matplotlib API 有三个层级。 `matplotlib.backend_bases.FigureCanvas`是绘制图形的区域,`matplotlib.backend_bases.Renderer`是知道如何在`ChartCanvas`上绘制的对象,而`matplotlib.artist.Artist`是知道如何使用渲染器在画布上画图的对象。 `FigureCanvas`和`Renderer`处理与用户界面工具包(如 wxPython)或 PostScript® 等绘图语言交互的所有细节,`Artist`处理所有高级结构,如表示和布局图形,文本和线条。用户通常要花费95%的时间来处理艺术家。
|
4 | 10 |
|
5 | 11 | 有两种类型的艺术家:基本类型和容器类型。基本类型表示我们想要绘制到画布上的标准图形对象:`Line2D`,`Rectangle`,`Text`,`AxesImage`等,容器是放置它们的位置(`Axis`,`Axes`和`Figure`)。标准用法是创建一个`Figure`实例,使用`Figure`创建一个或多个`Axes`或`Subplot`实例,并使用`Axes`实例的辅助方法来创建基本类型。在下面的示例中,我们使用`matplotlib.pyplot.figure()`创建一个`Figure`实例,这是一个便捷的方法,用于实例化`Figure`实例并将它们与您的用户界面或绘图工具包`FigureCanvas`连接。正如我们将在下面讨论的,这不是必须的 - 你可以直接使用 PostScript,PDF,Gtk+ 或 wxPython `FigureCanvas`实例,直接实例化你的图形并连接它们 - 但是因为我们在这里关注艺术家 API,我们让`pyplot`为我们处理一些细节:
|
@@ -314,3 +320,130 @@ for label in ax.get_xticklabels():
|
314 | 320 | | `yaxis` | `matplotlib.axis.YAxis`实例 |
|
315 | 321 |
|
316 | 322 | ## 轴容器
|
| 323 | + |
| 324 | +`matplotlib.axis.Axis`实例处理刻度线,网格线,刻度标签和轴标签的绘制。您可以分别为y轴配置左和右刻度,为x轴分别配置上和下刻度。 `Axis`还存储在自动缩放,平移和缩放中使用的数据和视图间隔,以及`Locator`和`Formatter`实例,它们控制刻度位置以及它们表示为字符串的方式。 |
| 325 | + |
| 326 | +每个`Axis`对象都包含一个`label`属性(这是 pylab 在调用`xlabel()`和`ylabel()`时修改的东西)以及主和次刻度的列表。刻度是`XTick`和`YTick`实例,它包含渲染刻度和刻度标签的实际线条和文本基本类型。因为刻度是按需动态创建的(例如,当平移和缩放时),您应该通过访问器方法`get_major_ticks()`和`get_minor_ticks()`访问主和次刻度的列表。虽然刻度包含所有下面要提及的基本类型,`Axis`方法包含访问器方法来返回刻度线,刻度标签,刻度位置等: |
| 327 | + |
| 328 | +```py |
| 329 | +In [285]: axis = ax.xaxis |
| 330 | + |
| 331 | +In [286]: axis.get_ticklocs() |
| 332 | +Out[286]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) |
| 333 | + |
| 334 | +In [287]: axis.get_ticklabels() |
| 335 | +Out[287]: <a list of 10 Text major ticklabel objects> |
| 336 | + |
| 337 | +# note there are twice as many ticklines as labels because by |
| 338 | +# default there are tick lines at the top and bottom but only tick |
| 339 | +# labels below the xaxis; this can be customized |
| 340 | +In [288]: axis.get_ticklines() |
| 341 | +Out[288]: <a list of 20 Line2D ticklines objects> |
| 342 | + |
| 343 | +# by default you get the major ticks back |
| 344 | +In [291]: axis.get_ticklines() |
| 345 | +Out[291]: <a list of 20 Line2D ticklines objects> |
| 346 | + |
| 347 | +# but you can also ask for the minor ticks |
| 348 | +In [292]: axis.get_ticklines(minor=True) |
| 349 | +Out[292]: <a list of 0 Line2D ticklines objects> |
| 350 | +``` |
| 351 | + |
| 352 | +下面是`Axis`的一些有用的访问器方法的总结(它们拥有相应的`setter`,如`set_major_formatter`)。 |
| 353 | + |
| 354 | + |
| 355 | +| 访问器方法 | 描述 | |
| 356 | +| --- | --- | |
| 357 | +| get_scale | 轴的比例,例如`'log'`或`'linear'` | |
| 358 | +| get_view_interval | 轴视图范围的内部实例 | |
| 359 | +| get_data_interval | 轴数据范围的内部实例 | |
| 360 | +| get_gridlines | 轴的网格线列表 | |
| 361 | +| get_label | 轴标签 - `Text`实例 | |
| 362 | +| get_ticklabels | `Text`实例的列表 - 关键字`minor=True|False` | |
| 363 | +| get_ticklines | `Line2D`实例的列表 - 关键字`minor=True|False` | |
| 364 | +| get_ticklocs | `Tick`位置的列表 - 关键字`minor=True|False` | |
| 365 | +| get_major_locator | 用于主刻度的`matplotlib.ticker.Locator`实例 | |
| 366 | +| get_major_formatter | 用于主刻度的`matplotlib.ticker.Formatter`实例 | |
| 367 | +| get_minor_locator | 用于次刻度的`matplotlib.ticker.Locator`实例 | |
| 368 | +| get_minor_formatter | 用于次刻度的`matplotlib.ticker.Formatter`实例 | |
| 369 | +| get_major_ticks | 用于主刻度的`Tick`实例列表 | |
| 370 | +| get_minor_ticks | 用于次刻度的`Tick`实例列表 | |
| 371 | +| grid | 为主或次刻度打开或关闭网格 | |
| 372 | + |
| 373 | +这里是个例子,出于美观不太推荐,它自定义了轴域和刻度属性。 |
| 374 | + |
| 375 | +```py |
| 376 | +import numpy as np |
| 377 | +import matplotlib.pyplot as plt |
| 378 | + |
| 379 | +# plt.figure creates a matplotlib.figure.Figure instance |
| 380 | +fig = plt.figure() |
| 381 | +rect = fig.patch # a rectangle instance |
| 382 | +rect.set_facecolor('lightgoldenrodyellow') |
| 383 | + |
| 384 | +ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4]) |
| 385 | +rect = ax1.patch |
| 386 | +rect.set_facecolor('lightslategray') |
| 387 | + |
| 388 | + |
| 389 | +for label in ax1.xaxis.get_ticklabels(): |
| 390 | + # label is a Text instance |
| 391 | + label.set_color('red') |
| 392 | + label.set_rotation(45) |
| 393 | + label.set_fontsize(16) |
| 394 | + |
| 395 | +for line in ax1.yaxis.get_ticklines(): |
| 396 | + # line is a Line2D instance |
| 397 | + line.set_color('green') |
| 398 | + line.set_markersize(25) |
| 399 | + line.set_markeredgewidth(3) |
| 400 | + |
| 401 | +plt.show() |
| 402 | +``` |
| 403 | + |
| 404 | + |
| 405 | + |
| 406 | +## 刻度容器 |
| 407 | + |
| 408 | +`matplotlib.axis.Tick`是我们从`Figure`到`Axes`再到`Axis`再到`Tick`的最终的容器对象。`Tick`包含刻度和网格线的实例,以及上侧和下侧刻度的标签实例。 每个都可以直接作为`Tick`的属性访问。此外,也有用于确定上标签和刻度是否对应`x`轴,以及右标签和刻度是否对应`y`轴的布尔变量。 |
| 409 | + |
| 410 | + |
| 411 | +| 刻度属性 | 描述 | |
| 412 | +| --- | --- | |
| 413 | +| `tick1line` | `Line2D`实例 | |
| 414 | +| `tick2line` | `Line2D`实例 | |
| 415 | +| `gridline` | `Line2D`实例 | |
| 416 | +| `label1` | `Text`实例 | |
| 417 | +| `label2` | `Text`实例 | |
| 418 | +| `gridOn` | 确定是否绘制刻度线的布尔值 | |
| 419 | +| `tick1On` | 确定是否绘制主刻度线的布尔值 | |
| 420 | +| `tick2On` | 确定是否绘制次刻度线的布尔值 | |
| 421 | +| `label1On` | 确定是否绘制主刻度标签的布尔值 | |
| 422 | +| `label2On` | 确定是否绘制次刻度标签的布尔值 | |
| 423 | + |
| 424 | +这里是个例子,使用美元符号设置右侧刻度,并在`y`轴右侧将它们设成绿色。 |
| 425 | + |
| 426 | +```py |
| 427 | +import numpy as np |
| 428 | +import matplotlib.pyplot as plt |
| 429 | +import matplotlib.ticker as ticker |
| 430 | + |
| 431 | +# Fixing random state for reproducibility |
| 432 | +np.random.seed(19680801) |
| 433 | + |
| 434 | +fig = plt.figure() |
| 435 | +ax = fig.add_subplot(111) |
| 436 | +ax.plot(100*np.random.rand(20)) |
| 437 | + |
| 438 | +formatter = ticker.FormatStrFormatter('$%1.2f') |
| 439 | +ax.yaxis.set_major_formatter(formatter) |
| 440 | + |
| 441 | +for tick in ax.yaxis.get_major_ticks(): |
| 442 | + tick.label1On = False |
| 443 | + tick.label2On = True |
| 444 | + tick.label2.set_color('green') |
| 445 | + |
| 446 | +plt.show() |
| 447 | +``` |
| 448 | + |
| 449 | + |
0 commit comments