首页 > 解决方案 > MatplotlibDeprecationWarning 向子图添加背景颜色时

问题描述

我正在尝试从数据集中创建一个箱形图,然后更改每个子图的背景颜色:

url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv"
column_names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
data_set = read_csv(url, names=column_names)


data_set.plot(kind="box", subplots=True, color="yellow", layout=(2, 2), sharex=False, sharey=False)

pyplot.subplot(221).set_facecolor("red")
pyplot.subplot(222).set_facecolor("purple")
pyplot.subplot(223).set_facecolor("green")
pyplot.subplot(224).set_facecolor("blue")

pyplot.show()

我确实得到了彩色子图,但是,我不断收到警告消息:

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

有谁知道如何解决这个问题?

标签: pythonmatplotlib

解决方案


这似乎是 Matplotlib 中一个持续存在的问题,截至今天尚未解决。从这里阅读线程来看,似乎没有迫在眉睫的弃用风险。

但是,如果您希望此消息消失,您可以尝试此用户在此处尝试的操作。. 这里的用户替换fig.subplot()fig.add_subplot()


推荐阅读