4 回答
![?](http://img1.sycdn.imooc.com/5859e2d50001f6bb01000100-100-100.jpg)
TA贡献1818条经验 获得超7个赞
解决方法:
避免警告的方法是使用 FixLocator (这是 matplotlib.ticker 的一部分)。下面我展示了绘制三个图表的代码。我以不同的方式格式化它们的轴。请注意,“set_ticks”使警告静音,但它更改了实际的刻度位置/标签(我花了一些时间才弄清楚FixedLocator使用相同的信息但保持刻度位置完整)。您可以使用 x/y 来查看每个解决方案如何影响输出。
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
mpl.rcParams['font.size'] = 6.5
x = np.array(range(1000, 5000, 500))
y = 37*x
fig, [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-labels
ax3.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
并非所有列出的版本都可用。
![?](http://img1.sycdn.imooc.com/5458620000018a2602200220-100-100.jpg)
TA贡献1744条经验 获得超4个赞
如果有人使用该函数(或 yaxis 等效函数)来到这里axes.xaxis.set_ticklabels()
,您不需要使用 FixLocator,您可以使用axes.xaxis.set_ticks(values_list)
BEFORE axes.xaxis.set_ticklabels(labels_list)
来避免此警告。
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
TA贡献1812条经验 获得超5个赞
当我尝试使用定位的日期刻度旋转 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)
![?](http://img1.sycdn.imooc.com/533e4c640001354402000200-100-100.jpg)
TA贡献1864条经验 获得超2个赞
根据这个 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.FuncFormatter
def major_formatter(x, pos):
return f'{x:.2f}'
并将格式化程序函数传递给FixedFormatter
ax.xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=5))
ax.xaxis.set_major_formatter(major_formatter)
详情请参阅上面的链接。
添加回答
举报