用户警告:FixedFormatter 只能与 FixLocator 一起使用

我已经使用了很长一段时间的小子例程来格式化我正在绘制的图表的轴。举几个例子:


def format_y_label_thousands(): # format y-axis tick labels formats

    ax = plt.gca()

    label_format = '{:,.0f}'

    ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])


def format_y_label_percent(): # format y-axis tick labels formats

    ax = plt.gca()

    label_format = '{:.1%}'

    ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])

然而,昨天更新 matplotlib 后,在调用这两个函数中的任何一个时,我收到以下警告:


UserWarning: FixedFormatter should only be used together with FixedLocator

  ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])

出现这样的警告的原因是什么?我无法弄清楚如何查看 matplotlib 的文档。


UYOU
浏览 327回答 4
4回答

qq_笑_17

解决方法:避免警告的方法是使用 FixLocator (这是 matplotlib.ticker 的一部分)。下面我展示了绘制三个图表的代码。我以不同的方式格式化它们的轴。请注意,“set_ticks”使警告静音,但它更改了实际的刻度位置/标签(我花了一些时间才弄清楚FixedLocator使用相同的信息但保持刻度位置完整)。您可以使用 x/y 来查看每个解决方案如何影响输出。import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npimport matplotlib.ticker as mtickermpl.rcParams['font.size'] = 6.5x = np.array(range(1000, 5000, 500))y = 37*xfig, [ax1, ax2, ax3] = plt.subplots(1,3)ax1.plot(x,y, linewidth=5, color='green')ax2.plot(x,y, linewidth=5, color='red')ax3.plot(x,y, linewidth=5, color='blue')label_format = '{:,.0f}'# nothing done to ax1 as it is a "control chart."# fixing yticks with "set_yticks"ticks_loc = ax2.get_yticks().tolist()ax2.set_yticks(ax1.get_yticks().tolist())ax2.set_yticklabels([label_format.format(x) for x in ticks_loc])# fixing yticks with matplotlib.ticker "FixedLocator"ticks_loc = ax3.get_yticks().tolist()ax3.yaxis.set_major_locator(mticker.FixedLocator(ticks_loc))ax3.set_yticklabels([label_format.format(x) for x in ticks_loc])# fixing xticks with FixedLocator but also using MaxNLocator to avoid cramped x-labelsax3.xaxis.set_major_locator(mticker.MaxNLocator(3))ticks_loc = ax3.get_xticks().tolist()ax3.xaxis.set_major_locator(mticker.FixedLocator(ticks_loc))ax3.set_xticklabels([label_format.format(x) for x in ticks_loc])fig.tight_layout()plt.show()输出图表:显然,像上面这样的几行闲置代码(我基本上是获取 yticks 或 xticks 并再次设置它们)只会给我的程序增加噪音。我希望删除该警告。但是,请查看一些“错误报告”(来自上面/下面评论中的链接;问题实际上不是错误:它是产生一些问题的更新),并且管理 matplotlib 的贡献者有他们的理由保留警告。旧版本的 MATPLOTLIB: 如果您使用控制台来控制代码的关键输出(就像我一样),则警告消息可能会出现问题。因此,延迟处理该问题的一种方法是将 matplotlib 降级到版本 3.2.2。我使用 Anaconda 来管理我的 Python 包,以下是用于降级 matplotlib 的命令:conda install matplotlib=3.2.2并非所有列出的版本都可用。

慕无忌1623718

如果有人使用该函数(或 yaxis 等效函数)来到这里axes.xaxis.set_ticklabels(),您不需要使用 FixLocator,您可以使用axes.xaxis.set_ticks(values_list) BEFORE axes.xaxis.set_ticklabels(labels_list)来避免此警告。

慕雪6442864

当我尝试使用定位的日期刻度旋转 X 轴上的刻度标签时,我遇到了同样的问题:ax.set_xticklabels(ax.get_xticklabels(), rotation=45) ax.xaxis.set_major_locator(dates.DayLocator())它使用“tick_params()”方法计算出来:ax.tick_params(axis='x', labelrotation = 45)

慕斯王

根据这个 matplotlib页面# FixedFormatter should only be used together with FixedLocator. # Otherwise, one cannot be sure where the labels will end up.这意味着一个人应该做positions = [0, 1, 2, 3, 4, 5]labels = ['A', 'B', 'C', 'D', 'E', 'F']ax.xaxis.set_major_locator(ticker.FixedLocator(positions))ax.xaxis.set_major_formatter(ticker.FixedFormatter(labels))ticker.LogLocator但即使标签被传递到 ,问题仍然存在ticker.FixedFormatter。所以这种情况下的解决方案是定义格式化函数# FuncFormatter can be used as a decorator@ticker.FuncFormatterdef major_formatter(x, pos):    return f'{x:.2f}'并将格式化程序函数传递给FixedFormatterax.xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=5))ax.xaxis.set_major_formatter(major_formatter)详情请参阅上面的链接。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python