【问题标题】:delete legend from statsmodels interaction_plot从 statsmodels interaction_plot 中删除图例
【发布时间】:2022-06-07 19:50:21
【问题描述】:

我使用来自 statsmodels 的interaction_plot。我想添加一个图例,但interaction_plot 有自己的图例,所以我得到了两个图例。一个例子:

import numpy as np
np.random.seed(12345)
weight = np.random.randint(1,4,size=60)
duration = np.random.randint(1,3,size=60)
days = np.log(np.random.randint(1,30, size=60))
fig = interaction_plot(weight, duration, days,
             colors=[\'red\',\'blue\'], markers=[\'D\',\'^\'], ms=10)
import matplotlib.pyplot as plt
fig.legend(loc=\'upper center\', bbox_to_anchor=(0.5, 1.05), fancybox=True, shadow=True)
plt.show()

如何删除原始图例?

    标签: python matplotlib statsmodels


    【解决方案1】:

    此函数返回一个带有一个轴的图形,因此您需要检索它并删除它的图例:

    fig = interaction_plot(weight, duration, days,
                 colors=['red','blue'], markers=['D','^'], ms=10)
    
    # this is what I added
    fig.get_axes()[0].get_legend().remove()
    
    fig.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), fancybox=True, shadow=True)
    

    如果您使用的 matplotlib 版本早于 1.4.0,则将该行替换为 fig.get_axes()[0].legend_ = None

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多