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

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

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

  • 在 Numpy 1.0 中,bin 仅由左边缘指定。axes 方法 matplotlib.axes.Axes.hist() 现在对直方图使用未来 Numpy 1.3 的语义。提供 binedges 时,最后一个值现在表示右上边缘,这在 Numpy 1.0 中默认隐式设置为正无穷。这也意味着最后一个 bin 默认不再包含上层异常值。

  • 新的 axes 方法和 pyplot 函数 hexbin() 是大型数据集的 scatter() 的替代方案。它生成类似 2D 直方图的 pcolor(),但使用六边形 bin。

  • matplotlib.ticker.MaxNLocator 中新增了 kwarg symmetric,允许要求坐标轴以零为中心。

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

关于转换重构的说明#

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

这次重构的主要目标是让 Matplotlib 更容易扩展以支持新型投影。这主要是内部改进,可能的用户可见的更改尚未到来。

有关新转换框架的设计说明,请参见 matplotlib.transforms

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

视图间隔现在仅存储在一个地方——在 matplotlib.axes.Axes 实例中,而不是同时存储在定位器实例中。这意味着定位器必须从其 matplotlib.axis.Axis 获取其限制,而 Axis 又从 Axes 查找其限制。如果定位器是临时使用且未分配给 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() 返回一个 Nx4 的 NumPy RGBA 颜色四元组数组。]

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