0.98.0 版本更新#

  • matplotlib.image.imread() 现在不再总是返回 RGBA 数据——如果图像是亮度或 RGB,它将尽可能返回 MxN 或 MxNx3 数组。此外,uint8 不再总是强制转换为浮点数。

  • 重写了 matplotlib.cm.ScalarMappable 回调基础设施,以使用 matplotlib.cbook.CallbackRegistry 而不是自定义回调处理。任何使用 ScalarMappablematplotlib.cm.ScalarMappable.add_observer 的用户应改为使用 matplotlib.cm.ScalarMappable.callbacksSM CallbackRegistry

  • 新的轴函数和轴方法提供了对绘图颜色循环的控制:matplotlib.axes.set_default_color_cyclematplotlib.axes.Axes.set_color_cycle

  • Matplotlib 现在需要 Python 2.4,因此 matplotlib.cbook 将不再提供 setenumerate()reversed()izip 兼容函数。

  • 在 Numpy 1.0 中,箱子仅由左边缘指定。轴方法 matplotlib.axes.Axes.hist() 现在使用未来 Numpy 1.3 的直方图语义。提供 binedges,最后一个值现在给出右上边缘,这在 Numpy 1.0 中隐式设置为 +无穷大。这也意味着默认情况下,最后一个箱子不再包含上部异常值。

  • 新的轴方法和 pyplot 函数,hexbin(),是 scatter() 在大数据集上的一个替代方案。它生成类似于 pcolor() 的二维直方图,但使用六边形分箱。

  • 新的关键字参数 symmetricmatplotlib.ticker.MaxNLocator 中允许要求轴以零为中心。

  • 工具包现在必须从 mpl_toolkits 导入(而不是 matplotlib.toolkits

关于转换重构的说明#

0.98 系列的一个主要新功能是更加灵活和可扩展的转换基础设施,使用 Python/Numpy 编写,而不是自定义的 C 扩展。

这次重构的主要目标是使扩展 matplotlib 以支持新类型的投影变得更加容易。这主要是一个内部改进,它可能带来的用户可见变化尚未到来。

关于新变换框架的设计描述,请参见 matplotlib.transforms

为了提高效率,这些函数中的许多返回的是 Numpy 数组的视图。这意味着如果你保留对它们的引用,它们的内容可能会改变。如果你想存储它们当前值的快照,请使用 Numpy 数组方法 copy()。

视图区间现在只存储在一个地方——在 matplotlib.axes.Axes 实例中,而不是在定位器实例中。这意味着定位器必须从它们的 matplotlib.axis.Axis 获取它们的限制,而后者又从 Axes 查找其限制。如果一个定位器被临时使用且未分配给一个轴或轴实例(例如,在 matplotlib.contour 中),则必须创建一个虚拟轴来存储其边界。调用 matplotlib.ticker.TickHelper.create_dummy_axis() 来完成此操作。

Pbox 的功能已被合并到 Bbox 中。它的方法现在都返回副本,而不是就地修改。

以下列出了从旧的转换框架更新代码到新的框架所需进行的许多简单更改。特别是,返回副本的方法以过去时态的动词命名,而就地修改对象的方法以现在时态的动词命名。

matplotlib.transforms#

旧方法

新方法

Bbox.get_bounds

transforms.Bbox.bounds

Bbox.width

transforms.Bbox.width

Bbox.height

transforms.Bbox.height

Bbox.intervalx().get_bounds() Bbox.intervalx().set_bounds()

transforms.Bbox.intervalx [现在是一个属性。]

Bbox.intervaly().get_bounds() Bbox.intervaly().set_bounds()

transforms.Bbox.intervaly [现在是一个属性。]

Bbox.xmin

transforms.Bbox.x0transforms.Bbox.xmin [1]

Bbox.ymin

transforms.Bbox.y0transforms.Bbox.ymin [1]

Bbox.xmax

transforms.Bbox.x1transforms.Bbox.xmax [1]

Bbox.ymax

transforms.Bbox.y1transforms.Bbox.ymax [1]

Bbox.overlaps(bboxes)

Bbox.count_overlaps(bboxes)

bbox_all(bboxes)

Bbox.union(bboxes) [这是一个静态方法。]

lbwh_to_bbox(l, b, w, h)

Bbox.from_bounds(x0, y0, w, h) [这是一个静态方法。]

inverse_transform_bbox(trans, bbox)

bbox.inverse_transformed(trans)

Interval.contains_open(v)

interval_contains_open(tuple, v)

Interval.contains(v)

interval_contains(tuple, v)

identity_transform()

transforms.IdentityTransform

blend_xy_sep_transform(xtrans, ytrans)

blended_transform_factory(xtrans, ytrans)

scale_transform(xs, ys)

Affine2D().scale(xs[, ys])

get_bbox_transform(boxin, boxout)

BboxTransform(boxin, boxout)BboxTransformFrom(boxin)BboxTransformTo(boxout)

Transform.seq_xy_tup(points)

Transform.transform(points)

Transform.inverse_xy_tup(points)

Transform.inverted().transform(points)

matplotlib.axes#

旧方法

新方法

Axes.get_position()

matplotlib.axes.Axes.get_position() [2]

Axes.set_position()

matplotlib.axes.Axes.set_position() [3]

Axes.toggle_log_lineary()

matplotlib.axes.Axes.set_yscale() [4]

Subplot

移除

Polar 类已移至 matplotlib.projections.polar

matplotlib.artist#

旧方法

新方法

Artist.set_clip_path(path)

Artist.set_clip_path(path, transform) [5]

matplotlib.collections#

旧方法

新方法

linestyle

linestyles [6]

matplotlib.colors#

旧方法

新方法

ColorConvertor.to_rgba_list(c)

colors.to_rgba_array(c) [matplotlib.colors.to_rgba_array() 返回一个包含RGBA颜色四元组的Nx4 NumPy数组。]

matplotlib.contour#

旧方法

新方法

Contour._segments

matplotlib.contour.Contour.get_paths [返回一个 matplotlib.path.Path 实例的列表。]

matplotlib.figure#

旧方法

新方法

Figure.dpi.get() Figure.dpi.set()

matplotlib.figure.Figure.dpi (一个属性)

matplotlib.patches#

旧方法

新方法

Patch.get_verts()

matplotlib.patches.Patch.get_path() [返回一个 matplotlib.path.Path 实例]

matplotlib.backend_bases#

旧方法

新方法

GraphicsContext.set_clip_rectangle(tuple)

GraphicsContext.set_clip_rectangle(bbox)

GraphicsContext.get_clip_path()

GraphicsContext.get_clip_path() [7]

GraphicsContext.set_clip_path()

GraphicsContext.set_clip_path() [8]

RendererBase#

新方法:

更改的方法:

移除的方法:

  • draw_arc

  • draw_line_collection

  • draw_line

  • draw_lines

  • draw_point

  • draw_quad_mesh

  • draw_poly_collection

  • draw_polygon

  • draw_rectangle

  • draw_regpoly_collection