1.4.x 中的 API 变更#
代码更改#
对axes模块进行了重大重构。axes模块已被拆分为更小的模块:
_base
模块,其中包含一个新的私有_AxesBase
类。此类包含除绘图和标签方法之外的所有方法。_subplot
模块,包含所有与子图相关的类。
在
axes
模块的命名空间中,有一些东西已经不存在了。如果你使用它们,你需要从它们原来的位置导入:math
->import math
ma
->from numpy import ma
cbook
->from matplotlib import cbook
docstring
->from matplotlib import docstring
is_sequence_of_strings
->from matplotlib.cbook import is_sequence_of_strings
is_string_like
->from matplotlib.cbook import is_string_like
iterable
->from matplotlib.cbook import iterable
itertools
->import itertools
martist
->from matplotlib import artist as martist
matplotlib
->import matplotlib
mcoll
->from matplotlib import collections as mcoll
mcolors
->from matplotlib import colors as mcolors
mcontour
->from matplotlib import contour as mcontour
mpatches
->from matplotlib import patches as mpatches
mpath
->from matplotlib import path as mpath
mquiver
->from matplotlib import quiver as mquiver
mstack
->from matplotlib import stack as mstack
mstream
->from matplotlib import stream as mstream
mtable
->from matplotlib import table as mtable
作为支持 Qt5 重构的一部分,模块
matplotlib.backends.qt4_compat
被重命名为matplotlib.backends.qt_compat
。qt4_compat
在 1.4 版本中已被弃用,并将在 1.5 版本中移除。The
errorbar()
方法已经更改,使得上下限(lolims、uplims、xlolims、xuplims)现在指向正确的方向。对于
errorbar()
的 fmt 关键字参数,现在支持字符串 'none' 来抑制线条和标记的绘制;使用 None 对象进行此操作已被弃用。默认的 fmt 值已更改为空字符串 (''),因此线条和标记由plot()
的默认值控制。在字体路径效果渲染中修复了一个错误,这意味着现在字体大小与非路径效果字体一致。更多详情请参见 matplotlib/matplotlib#2889。
Sphinx 扩展
ipython_directive
和ipython_console_highlighting
已经移至 IPython 项目本身。虽然它们在此版本中仍保留在 Matplotlib 中,但已被弃用。请更新conf.py
中的扩展,使其指向IPython.sphinxext.ipython_directive
而不是matplotlib.sphinxext.ipython_directive
。在
matplotlib.finance
中,几乎所有函数都已被弃用,并替换为一对名为*_ochl
和*_ohlc
的函数。前者是本模块之前使用的 '开盘-收盘-最高-最低' 报价顺序,而后者是金融领域标准的 '开盘-最高-最低-收盘' 顺序。为了保持一致性,
matplotlib.patheffects.SimplePatchShadow
中的face_alpha
关键字已被弃用,取而代之的是alpha
关键字。同样,所有AbstractPathEffect
中的offset_xy
关键字现在被命名为offset
。matplotlib.patheffects._Base
已重命名为matplotlib.patheffects.AbstractPathEffect
。matplotlib.patheffect.ProxyRenderer
已重命名为matplotlib.patheffects.PathEffectRenderer
,并且现在是一个完整的 RendererBase 子类。绘制
Figure.colorbar
轮廓的艺术家已从matplotlib.lines.Line2D
更改为matplotlib.patches.Polygon
,因此colorbar.ColorbarBase.outline
现在是一个matplotlib.patches.Polygon
对象。图例处理接口已从可调用对象更改为实现
legend_artists
方法的任何对象(在 v1.4 的弃用阶段将维护此接口)。有关更多详细信息,请参阅 图例指南。进一步的图例更改包括:matplotlib.axes.Axes._get_legend_handles
现在返回一个句柄生成器,而不是一个列表。函数
legend()
的 loc 位置参数已被弃用。请改用 loc 关键字参数。
已添加
rcParams["savefig.transparent"]
(default:False
) 以控制保存图形时的默认透明度。稍微重构了
Annotation
家族。Annotation
中的文本位置现在完全由底层的Text
对象处理,因此.set_position
按预期工作。属性 xytext 和 textcoords 已被弃用,取而代之的是 xyann 和 anncoords,以便Annotation
和AnnotationBbox
可以共享一个用于获取/设置文本或框位置的通用且合理命名的 API。xyann -> 设置注释的位置
xy -> 设置箭头指向的位置
anncoords -> 设置注释位置的单位
xycoords -> 设置点位置的单位
set_position()
->Annotation
仅设置注释的位置
matplotlib.mlab.specgram
,matplotlib.mlab.psd
,matplotlib.mlab.csd
,matplotlib.mlab.cohere
,matplotlib.mlab.cohere_pairs
,matplotlib.pyplot.specgram
,matplotlib.pyplot.psd
,matplotlib.pyplot.csd
, 和matplotlib.pyplot.cohere
现在在之前引发 AssertionError 的地方引发 ValueError。对于
matplotlib.mlab.psd
、matplotlib.mlab.csd
、matplotlib.mlab.cohere
、matplotlib.mlab.cohere_pairs
、matplotlib.pyplot.specgram
、matplotlib.pyplot.psd
、matplotlib.pyplot.csd
和matplotlib.pyplot.cohere
,在返回形状为 (n, 1) 数组的情况下,现在将其转换为 (n, ) 数组。以前,(n, m) 数组被平均为 (n, ) 数组,但 (n, 1) 数组保持不变。此更改使得两种情况下的维度一致。添加了
rcParams["axes.formatter.useoffset"]
(default:True
) 以控制ticker.ScalarFormatter
中 useOffset 的默认值。添加了
Formatter
子类StrMethodFormatter
,它与FormatStrFormatter
做同样的事情,但适用于新样式的格式化字符串。已弃用
matplotlib.testing.image_util
及其唯一函数matplotlib.testing.image_util.autocontrast
。这些将在 v1.5.0 中完全移除。在
plot_date()
方法中的fmt
参数已从bo
更改为o
,因此默认情况下可以进行颜色循环。移除了类
FigureManagerQTAgg
并弃用了NavigationToolbar2QTAgg
,后者将在1.5版本中移除。从
FigureCanvasQTAgg
中移除了以前公开的(非前缀的)属性rect
和drawRect
;它们始终是(保留的)drawRectangle()
函数的实现细节。matplotlib.tight_bbox.adjust_bbox
和matplotlib.tight_bbox.process_figure_for_rasterizing
的函数签名已更改。新增的 fixed_dpi 参数允许覆盖figure.dpi
设置,而不是从文件格式推断预期行为。为
mpl_toolkits.axes_grid1.axes_grid.ImageGrid
添加了对水平/垂直轴填充的支持 --- 如果需要单独的轴填充,参数 axes_pad 现在可以是类似元组的形式。原始行为保持不变。为
matplotlib.transforms.Affine2D
添加了对倾斜变换的支持,可以使用skew
和skew_deg
方法创建。在
axes.Axes.pie
中添加了顺时针参数以控制扇区方向在
matplotlib.lines.Line2D
中,markevery 功能得到了扩展。以前可以通过使用两元素列表或两元素元组来指定整数起始索引和步长。现在这只能通过使用两元素元组来完成。如果使用了两元素列表,那么它将被视为 NumPy 花式索引,并且只会显示与给定索引对应的两个标记。从
mpl_toolkits.axes_grid1.anchored_artists.AnchoredSizeBar
调用中移除了 prop 关键字参数。它被传递给基类的__init__
并且仅用于设置填充。现在 fontproperties*(这是真正用于设置 `.AnchoredSizeBar` 字体属性的参数)被传递来代替 *prop。如果未传递 fontproperties,但传递了 prop,则 prop 被用于代替 fontproperties。如果两者都被传递,prop 将被静默忽略。在
pyplot.subplot
及相关命令中使用索引 0 已被弃用。由于缺乏验证,调用plt.subplots(2, 2, 0)
不会引发异常,但会将坐标轴放置在 _最后一个_ 位置。这是因为子图的索引是基于 1 的(以镜像 MATLAB),因此在索引到用于确定坐标轴位置的GridSpec
对象之前,会减去 1。传入 0 会导致向GridSpec
传递 -1,从而返回最后一个位置。尽管这种行为显然是错误的且非预期的,但我们出于谨慎考虑,正在进行一个弃用周期,以确保没有任何用户在利用这个 '特性'。在 1.4 版本中,使用 0 作为索引将引发警告,而在 1.5 版本中将引发异常。现在,偏移框的裁剪默认关闭。
Matplotlib 现在在关闭图形时使用了一个不那么激进的
gc.collect(1)
调用,以避免在内存中有大量用户对象时出现主要延迟。现在,*所有*饼图艺术家的默认剪切值默认为
False
。
代码移除#
移除了
mlab.levypdf
。该代码引发了 NumPy 错误(并且已经存在很长时间),并且不是 Levy 分布的标准形式。应改用scipy.stats.levy
。