Skip to main content
 首页 » 编程设计

python之使用 mpatches 的 matplotlib 图例指南示例不起作用

2025年05月04日16落叶

为什么这个例子取自here并可下载here在 Python 3 中对我不起作用:

import matplotlib.patches as mpatches 
import matplotlib.pyplot as plt 
 
red_patch = mpatches.Patch(color='red', label='The red data') 
plt.legend(handles=[red_patch]) 
 
plt.show() 

我只是得到一个空白图。我的 Python 和 matplotlib 版本分别是 3.4.1 和 1.3.1。

我得到这个错误代码:

UserWarning: No labeled objects found. Use label='...' kwarg on individual plots. 
  warnings.warn("No labeled objects found. " 

请您参考如下方法:

我为 Python2 安装了这样的库:

sudo apt-get install python-matplotlib  

并将您的示例复制粘贴到一个文件中,我得到了这张图片:

对于Python3,我刚刚安装了python3-mathplotlib:

sudo apt-get install python3-matplotlib 

我得到了与上面相同的图像。