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

本文介绍了在使用Matplotlib绘制图表时遇到的一个常见问题——设置图例位置时出现的错误,并给出了正确的图例位置参数及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://www.juzicode.com/archives/2486

 

错误提示:

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

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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值