我已经使用了很长一段时间的小子程序来格式化我正在绘制的图表的轴。举几个例子:
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的文档,我无法理解这一点
根据这个matplotlib page
这意味着我们应该这样做
但是问题仍然存在于
ticker.LogLocator
,即使标签被传递给了ticker.FixedFormatter
。 所以这个案例的解决方案是定义格式化程序函数
并将格式化程序函数传递给FixedFormatter
有关详细信息,请参见上面的链接
解决方法:
避免警告的方法是使用FixedLocator(它是matplotlib.ticker的一部分)。下面我展示了绘制三个图表的代码。我以不同的方式格式化它们的轴。请注意,“设置刻度”会使警告静音,但会更改实际刻度位置/标签(我花了一些时间才发现FixedLocator使用相同的信息,但保持刻度位置不变)。您可以使用x/y来查看每个解决方案如何影响输出
输出图表:
显然,有两行像上面那样的空闲代码(我基本上是获取ytick或xtick并再次设置它们)只会给我的程序增加噪音。我宁愿删除警告。但是,请查看一些“错误报告”(来自上面/下面评论的链接;该问题实际上不是一个错误:它是一个正在生成一些问题的更新),管理matplotlib的贡献者有理由保留该警告
旧版本的MATPLOTLIB: 如果您使用控制台来控制代码的关键输出(如我所做的),则警告消息可能会有问题。因此,延迟处理该问题的一种方法是将matplotlib降级为3.2.2版。我使用Anaconda管理Python包,下面是用于降级matplotlib的命令:
并非所有列出的版本都可用。例如,无法安装matplotlib 3.3.0,尽管它列在matplotlib的发行版页面:https://github.com/matplotlib/matplotlib/releases
如果有人使用函数
axes.xaxis.set_ticklabels()
(或等效的yaxis)来到这里,您不需要使用FixedLocator,您可以在axes.xaxis.set_ticks(values_list)
之前使用axes.xaxis.set_ticklabels(labels_list)
避免此警告相关问题 更多 >
编程相关推荐