Настройка поля легенды в модуле Pyplot Matplotlib: методы удаления рамки

  1. Метод 1: использование функции ax.legend()с параметром frameon, установленным в значение False:

    import matplotlib.pyplot as plt
    # Create a plot
    plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
    plt.plot([1, 2, 3], [2, 4, 1], label='Line 2')
    # Turn off the legend box frame
    plt.legend(frameon=False)
    # Display the plot
    plt.show()
  2. Метод 2. Доступ к объекту легенды и непосредственное изменение его свойств:

    import matplotlib.pyplot as plt
    # Create a plot
    plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
    plt.plot([1, 2, 3], [2, 4, 1], label='Line 2')
    # Get the legend object
    legend = plt.legend()
    # Turn off the legend box frame
    legend.set_frame_on(False)
    # Display the plot
    plt.show()
  3. Метод 3. Использование функции plt.gca().legend()с параметром frameon, для которого установлено значение False:

    import matplotlib.pyplot as plt
    # Create a plot
    plt.plot([1, 2, 3], [4, 5, 6], label='Line 1')
    plt.plot([1, 2, 3], [2, 4, 1], label='Line 2')
    # Turn off the legend box frame
    plt.gca().legend(frameon=False)
    # Display the plot
    plt.show()