Matplotlib触发marker重复定义UserWarning告警如何解决?
警告触发根本原因
Matplotlib的plot_date()函数默认fmt参数值为"o",你未显式指定fmt参数时,函数会默认携带marker='o'的定义,同时你又单独传入了marker=""参数,两处marker定义重复冲突,就会触发该UserWarning。你之前调整fmt参数未生效,通常是因为调整后的fmt仍携带marker定义,没有完全消除两处配置的冲突。
根本解决方法
- 方案1:显式指定
fmt参数统一控制格式,不单独传递linestyle、marker参数。你当前需要实线+无marker的效果,直接将fmt设为"-"即可,修改后代码如下:axs[0].plot_date(app.plot_dates, app.values, fmt='-', color='#F9D96D', linewidth=1.5) - 方案2:显式将
fmt参数设为空字符串,让函数优先读取你单独配置的linestyle、marker参数,修改后代码如下:axs[0].plot_date(app.plot_dates, app.values, fmt='', linestyle='-', marker="", color='#F9D96D', linewidth=1.5) - 方案3:针对性过滤该类警告(无需屏蔽所有Python警告),在代码开头加入以下配置即可:
import warnings warnings.filterwarnings("ignore", category=UserWarning, message="marker is redundantly defined by the 'marker' keyword argument")
内容的提问来源于stack exchange,提问作者CosmicSeizure
相关产品推荐
相关产品推荐

