No artists with labels found to put in legend.
时间: 2024-06-09 18:10:56 浏览: 27
这个错误通常发生在绘制图例时,因为没有找到要在图例中显示的标签。可能的原因是您没有指定标签,或者标签没有正确地与数据对应。
通常,你需要在绘制图表时指定标签。例如,如果你使用 matplotlib 绘制散点图,你可以这样指定标签:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
plt.scatter(x, y, label='my data')
plt.legend()
```
在这个例子中,我们使用 `label` 参数指定了标签。当我们调用 `plt.legend()` 时,matplotlib 将自动在图例中显示这个标签。
如果你的数据是 pandas DataFrame,你可以使用列名作为标签。例如:
```python
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [10, 20, 30, 40],
'label': ['A', 'B', 'C', 'D']
})
plt.scatter(data['x'], data['y'], label=data['label'])
plt.legend()
```
在这个例子中,我们将数据存储在一个 pandas DataFrame 中,并使用 DataFrame 的列名作为标签。这样,当我们调用 `plt.legend()` 时,matplotlib 将自动使用列名作为标签。
相关推荐
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)