API 在 2.1.0 版本中的变更#

日志尺度的默认行为已更改为屏蔽 <= 0 的值#

调用 matplotlib.axes.Axes.set_xscalematplotlib.axes.Axes.set_yscale 现在默认使用 'mask' 方法来处理无效值(与 'clip' 相反)。这意味着在日志刻度上任何 <= 0 的值将不会显示。

之前它们被裁剪到一个非常小的数量并显示。

matplotlib.cbook.CallbackRegistry.process() 默认情况下会抑制异常#

Matplotlib uses instances of CallbackRegistry as a bridge between user input event from the GUI and user callbacks. Previously, any exceptions raised in a user call back would bubble out of the process method, which is typically in the GUI event loop. Most GUI frameworks simple print the traceback to the screen and continue as there is not always a clear method of getting the exception back to the user. However PyQt5 now exits the process when it receives an un-handled python exception in the event loop. Thus, process() now suppresses and prints tracebacks to stderr by default.

现在,通过 exception_handler 属性和关键字参数,用户可以配置 process() 如何处理异常。要恢复之前的行为,请传递 None

cb = CallbackRegistry(exception_handler=None)

一个只接受 Exception 作为其唯一参数的函数也可以传递

def maybe_reraise(exc):
    if isinstance(exc, RuntimeError):
        pass
    else:
        raise exc

cb = CallbackRegistry(exception_handler=maybe_reraise)

改进了坐标轴网格的切换功能#

g 键绑定现在可以独立切换 xy 网格的状态(通过循环所有四种开/关组合)。

新的 G 键绑定切换次要网格的状态。

如果只有网格线的一部分(在任一方向)可见,则禁用这两种绑定,以避免对图形做出不可逆的更改。

刻度标签被关闭而不是不可见#

在内部,Tick~matplotlib.axis.Tick.label1On 属性现在用于隐藏刻度标签,而不是在刻度标签对象上设置可见性。这提高了整体性能并修复了一些问题。因此,如果需要显示这些标签,则需要使用 tick_params(),例如:

ax.tick_params(labelbottom=True)

移除空图例的警告#

pyplot.legend 在找不到标记的艺术家时会发出警告。此警告已被移除。

更精确的图例自动定位#

图例的自动定位现在更倾向于使用 Line2D 包围的区域,而不是将图例直接放置在线上。

清理库存样本数据#

股票样本数据已经过清理,以去除冗余并提高可移植性。AAPL.dat.gzINTC.dat.gzaapl.csv 文件已被完全删除,并且也将不再从 matplotlib.cbook.get_sample_data 中提供。如果需要 CSV 文件,我们建议使用继续包含在样本数据中的 msft.csv。如果可以接受 NumPy 二进制文件,我们建议使用以下两个新文件之一。aapl.npy.gzgoog.npy 文件已被 aapl.npzgoog.npz 替换,其中第一列的类型已从 datetime.date 更改为 numpy.datetime64,以提高跨 Python 版本的可移植性。请注意,Matplotlib 尚未完全支持 numpy.datetime64

更新 qhull 到 2015.2#

Matplotlib 附带的 qhull 版本,用于 Delaunay 三角剖分,已从 2012.1 版更新到 2015.2 版。

改进的带大偏移量的Delaunay三角剖分#

Delaunay 三角剖分现在能更好地处理大的 x,y 偏移。这可能会导致使用 Matplotlib 计算的任何三角剖分发生细微变化,即任何请求计算 Delaunay 三角剖分的 matplotlib.tri.Triangulation 的使用,包括 matplotlib.pyplot.tricontourmatplotlib.pyplot.tricontourfmatplotlib.pyplot.tripcolormatplotlib.pyplot.triplotmatplotlib.mlab.griddatampl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf

使用 backports.functools_lru_cache 而不是 functools32#

它维护得更好,使用更广泛(被 pylint、jaraco 等使用)。

cbook.is_numlike 仅执行实例检查#

matplotlib.cbook.is_numlike 现在只检查其参数是否是 (numbers.Number, np.Number) 的实例。特别是,这意味着数组现在不再是数字类型。

椭圆弧现在在正确的角度之间绘制#

matplotlib.patches.Arc 补丁现在可以正确地在给定的角度之间绘制。

之前绘制的是一个圆弧,然后被拉伸成椭圆,因此最终的弧线并不位于 theta1theta2 之间。

-d$backend 不再设置后端#

不再可能通过在命令行传递 -d$backend 来设置后端。请改用 MPLBACKEND 环境变量。

Path.intersects_bbox 总是将边界框视为填充的#

之前,当 Path.intersects_bbox 被调用时,如果 filled 设置为 False,它会将路径和边界框都视为未填充。这种行为没有得到很好的文档记录,通常也不是期望的行为,因为边界框用于表示位于边界框内的更复杂形状。现在这种行为已经改变:当 filledFalse 时,路径将被视为未填充,但边界框仍被视为填充。旧的行为可以说是一个实现错误。

Path.intersects_bbox 被调用时,如果 filled 设置为 True``(默认值),行为不会改变。对于那些罕见的情况,当 ``Path.intersects_bbox 被调用时 filled 设置为 False,并且实际上需要旧的行为时,建议的解决方法是调用 Path.intersects_path 并将矩形作为路径传递:

from matplotlib.path import Path
from matplotlib.transforms import Bbox, BboxTransformTo
rect = Path.unit_rectangle().transformed(BboxTransformTo(bbox))
result = path.intersects_path(rect, filled=False)

WX 不再生成 IdleEvent 事件或调用 idle_event#

FigureCanvasWx 中移除了未使用的私有方法 _onIdle

IdleEvent 类和 FigureCanvasBase.idle_event 方法将在 2.2 版本中移除。

magnitude_spectrum() 的正确缩放#

函数 matplotlib.mlab.magnitude_spectrum()matplotlib.pyplot.magnitude_spectrum() 隐含地假设了窗口函数值的总和为1。在 Matplotlib 和 Numpy 中,标准的窗口函数被缩放到最大值为1,这通常会导致一个 n 点信号的总和为 n/2 的数量级。因此,当使用标准窗口函数时,magnitude_spectrum() 的幅度缩放偏差了该数量(Bug 8417)。现在,其行为与 matplotlib.pyplot.psd()scipy.signal.welch() 一致。以下示例展示了新的和旧的缩放:

import matplotlib.pyplot as plt
import numpy as np

tau, n = 10, 1024  # 10 second signal with 1024 points
T = tau/n  # sampling interval
t = np.arange(n)*T

a = 4  # amplitude
x = a*np.sin(40*np.pi*t)  # 20 Hz sine with amplitude a

# New correct behavior: Amplitude at 20 Hz is a/2
plt.magnitude_spectrum(x, Fs=1/T, sides='onesided', scale='linear')

# Original behavior: Amplitude at 20 Hz is (a/2)*(n/2) for a Hanning window
w = np.hanning(n)  # default window is a Hanning window
plt.magnitude_spectrum(x*np.sum(w), Fs=1/T, sides='onesided', scale='linear')

更改 bar()barh() 的签名#

对于 2.0 版本,*align* 的默认值 更改为 'center'。然而,这导致 bar()barh() 的签名具有误导性,因为第一个参数仍然是 leftbottom 分别:

bar(left, height, *, align='center', **kwargs)
barh(bottom, width, *, align='center', **kwargs)

尽管在两种情况下都表现得像中心。这些方法现在接受 *args, **kwargs 作为输入,并且文档中记录了它们的主要签名如下:

bar(x, height, *, align='center', **kwargs)
barh(y, width, *, align='center', **kwargs)

分别将 leftbottom 作为关键字参数传递给 bar()barh() 将会发出警告。该支持将在 Matplotlib 3.0 中移除。

字体缓存为json#

字体缓存现在保存为 json 格式,而不是 pickle 格式。

无效(非有限)轴限制错误#

在使用 set_xlim()set_ylim() 时,传递非有限值现在会导致 ValueError。之前的行为会导致限制错误地重置为 (-0.001, 0.001)

scatterCollection 偏移量不再隐式展平#

scatter 和三维的 scatter)不再隐式地展平其偏移量。因此,scatterxy 参数不能再是二维以上的数组。

弃用#

GraphicsContextBaselinestyle 属性。#

GraphicsContextBase.get_linestyleGraphicsContextBase.set_linestyle 方法,这些方法没有效果,已被弃用。Matplotlib 所有自带的后端都使用 GraphicsContextBase.get_dashesGraphicsContextBase.set_dashes,这些方法更为通用。第三方后端也应迁移到 *_dashes 方法。

测试#

matplotlib.testing.noseclasses 已被弃用,并将在 2.3 版本中移除。

EngFormatternum 参数格式化为字符串#

在调用 matplotlib.ticker.EngFormatter 实例时,将字符串作为 num 参数传递已被弃用,并将在 2.3 版本中移除。

mpl_toolkits.axes_grid 模块#

mpl_toolkits.axes_grid 的所有功能都可以在 mpl_toolkits.axes_grid1mpl_toolkits.axisartist 中找到。基于 mpl_toolkits.axisartist 中的 Axismpl_toolkits.axes_grid 的轴类可以在 mpl_toolkits.axisartist 中找到。

Figure.add_axes 中的 Axes 碰撞#

通过使用与先前轴实例相同的参数将轴实例添加到图形中,当前会重用先前的实例。此行为在 Matplotlib 2.1 中已被弃用。在未来的版本中,将始终创建并返回一个 实例。同时,在这种情况下,matplotlib.figure.AxesStack 会引发一个弃用警告。

此警告可以通过为每个轴实例传递一个*唯一*的标签来抑制,并确保未来的行为。更多信息请参阅 add_axes() 的文档字符串。

关于此次弃用背后的理由的更多细节可以在 #7377#9024 中找到。

contour.negative_linestyle 的前有效验证器#

前公共验证函数 validate_negative_linestylevalidate_negative_linestyle_legacy 将在2.1版本中被弃用,并可能在2.3版本中移除。没有公共函数来替代它们。

cbook#

许多未使用或几乎未使用的 matplotlib.cbook 函数和类已被弃用:convertertostrtodatetimetodatetofloattointuniqueis_string_likeis_sequence_of_stringsis_scalarSorterXlatorsoundexNulldict_delallRingBufferget_split_indwrapget_recursive_filelistpiecesexception_to_strallequalalltrueonetrueallpairsfinddirreverse_dictrestrict_dictissubclass_saferecursive_removeunmasked_index_ranges

代码移除#

qt4_compat.py#

已移动到 qt_compat.py。 重命名是因为它现在也处理 Qt5。

先前已弃用的方法#

GraphicsContextBase.set_graylevelFigureCanvasBase.onHilitempl_toolkits.axes_grid1.mpl_axes.Axes.toggle_axisline 方法已被移除。

由于缺少 get_children 方法,从未正常工作的 ArtistInspector.findobj 方法已被移除。

matplotlib.path 模块中,已弃用的 point_in_pathget_path_extentspoint_in_path_collectionpath_intersects_pathconvert_path_to_polygonscleanup_pathclip_path_to_rect 函数已被移除。它们的功能仍然作为 Path 类的方法公开。

已弃用的 Artist.get_axesArtist.set_axes 方法已被移除

matplotlib.backends.backend_ps.seq_allequal 函数已被移除。请改用 np.array_equal

已弃用的 matplotlib.rcsetup.validate_maskedarraymatplotlib.rcsetup.deprecate_savefig_extensionmatplotlib.rcsetup.validate_tkpythoninspect 函数,以及相关的 savefig.extensiontk.pythoninspect rcparams 条目已被移除。

已移除 matplotlib.projections.polar.PolarAxes 的关键字参数 resolution。自版本 0.98.x 起,它已被弃用且无效。

Axes.set_aspect("normal")#

设置 Axes 的纵横比为 "normal" 的支持已被移除,取而代之的是同义词 "auto"

shading 参数用于 pcolor#

pcolorshading 关键字参数已被移除。请改为适当设置 edgecolors

lines 模块中移除的函数#

The matplotlib.lines 模块不再从 matplotlib.cbook 导入 pts_to_presteppts_to_midsteppts_to_poststep 函数。

PDF 后端功能#

已移除 matplotlib.backends.backend_pdf.PdfFileembedTeXFonttex_font_mapping 方法。外部用户不太可能调用这些与 PDF 后端内部字体系统相关的方法。

matplotlib.delaunay#

移除现在由 Qhull 通过 matplotlib.tri 处理的 Delaunay 三角剖分代码。