Python错误集锦:matplotlib legend提示:ValueError: Unrecognized location ‘upper’. Valid locations are

原文链接: http://www.juzicode.com/python-error-matplotlib-legend-valueerror-unrecognized-location-upper-valid-locations/

错误提示:

使用matplotlib legend提示:ValueError: Unrecognized location ‘upper’. Valid locations are

plt.legend(title='参数where:',fontsize='xx-small',loc='upper')
Traceback (most recent call last):
  File "09-plt-step.py", line 30, in <module>
    plt.legend(title='参数where:',fontsize='xx-small',loc='upper')
  File "D:\Python\Python38\lib\site-packages\matplotlib\pyplot.py", line 2738, in legend
    return gca().legend(*args, **kwargs)
  File "D:\Python\Python38\lib\site-packages\matplotlib\axes\_axes.py", line 417, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
  File "D:\Python\Python38\lib\site-packages\matplotlib\legend.py", line 455, in __init__
    raise ValueError(
ValueError: Unrecognized location 'upper'. Valid locations are
        best
        upper right
        upper left
        lower left
        lower right
        right
        center left
        center right
        lower center
        upper center
        center

错误原因:

1、使用matplotlib的legend图例说明时,说明位置参数loc=’upper’非法,没有’upper’类型的位置值

解决方法:

1、通过错误提示找到合法的位置值,可以使用下面这些字符串表示的位置,比如upper right表示右上角::

        best
        upper right
        upper left
        lower left
        lower right
        right
        center left
        center right
        lower center
        upper center
        center

如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注