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

时间:2015-10-23 10:49:19

标签: python python-3.x matplotlib python-3.4

为什么这个示例来自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. "

1 个答案:

答案 0 :(得分:1)

我为Python2安装了这样的库:

sudo apt-get install python-matplotlib 

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

enter image description here

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

sudo apt-get install python3-matplotlib

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

相关问题