【问题标题】:Remove or adapt border of frame of legend using matplotlib使用 matplotlib 删除或调整图例框架的边框
【发布时间】:2014-10-21 19:28:49
【问题描述】:

使用 matplotlib 绘制绘图时:

  1. 如何去掉图例的框?
  2. 如何改变图例框的边框颜色?
  3. 如何只去掉图例框的边框?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    使用 matplotlib 绘制绘图时:

    如何去掉图例的框?

    plt.legend(frameon=False)
    

    如何改变图例框边框的颜色?

    leg = plt.legend()
    leg.get_frame().set_edgecolor('b')
    

    如何只去掉图例框的边框?

    leg = plt.legend()
    leg.get_frame().set_linewidth(0.0)
    

    对于matplotlib 面向对象的方法:

    axes.legend(frameon=False)
    
    leg = axes.legend()
    leg.get_frame().set_edgecolor('b')
    leg.get_frame().set_linewidth(0.0)
    

    【讨论】:

      【解决方案2】:

      还有一个相关的问题,因为我花了很长时间才找到答案:

      如何使图例背景为空白(透明,而不是白色):

      legend = plt.legend()
      legend.get_frame().set_facecolor('none')
      

      警告,您需要'none'(字符串)。 None 表示默认颜色。

      【讨论】:

      • 我认为最好的方法是使用framealpha,即plt.legend(framealpha=0.0)。不过,这将包括盒子
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2015-02-24
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多