No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
时间: 2024-03-21 22:40:15 浏览: 69
这个警告信息出现在 Python Matplotlib 库中,通常表示在图例中没有找到标签。这可能是因为没有给要显示的数据添加标签,或者标签以下划线开头,被忽略了。
下面是一些可能导致该警告信息出现的常见情况:
1. 没有为要显示的数据添加标签。可以通过在 plot 函数或 scatter 函数中使用 label 参数来添加标签。例如:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, label='line')
plt.legend()
plt.show()
```
2. 标签以下划线开头被忽略了。可以通过在 legend 函数中使用 handles 和 labels 参数来手动添加标签。例如:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
line, = plt.plot(x, y)
plt.legend(handles=[line], labels=['line'])
plt.show()
```
在这个例子中,我们使用了 `line, = plt.plot(x, y)` 来获取 plot 函数的返回值,然后将其作为参数传递给 legend 函数。
总之,要解决这个警告信息,需要确保所有要显示的数据都有标签,并且标签不以下划线开头。如果需要手动添加标签,可以使用 handles 和 labels 参数来完成。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)