Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

TL; DR -> Làm thế nào người ta có thể tạo một huyền thoại cho biểu đồ dòng trong ____ 2 PyPlot mà không tạo ra bất kỳ biến bổ sung nào? How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables?

Vui lòng xem xét tập lệnh đồ thị bên dưới:

if __name__ == '__main__':
    PyPlot.plot(total_lengths, sort_times_bubble, 'b-',
                total_lengths, sort_times_ins, 'r-',
                total_lengths, sort_times_merge_r, 'g+',
                total_lengths, sort_times_merge_i, 'p-', )
    PyPlot.title("Combined Statistics")
    PyPlot.xlabel("Length of list (number)")
    PyPlot.ylabel("Time taken (seconds)")
    PyPlot.show()

Như bạn có thể thấy, đây là cách sử dụng rất cơ bản của ________ 4. Điều này lý tưởng tạo ra một biểu đồ giống như dưới đây:

Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

Không có gì đặc biệt, tôi biết. Tuy nhiên, không rõ dữ liệu nào đang được vẽ ở đâu (Tôi đang cố gắng vẽ dữ liệu của một số thuật toán sắp xếp, độ dài so với thời gian được thực hiện và tôi muốn đảm bảo mọi người biết dòng nào là dòng nào). Do đó, tôi cần một huyền thoại, tuy nhiên, hãy xem ví dụ sau đây (từ trang web chính thức):

ax = subplot(1,1,1)
p1, = ax.plot([1,2,3], label="line 1")
p2, = ax.plot([3,2,1], label="line 2")
p3, = ax.plot([2,3,1], label="line 3")

handles, labels = ax.get_legend_handles_labels()

# reverse the order
ax.legend(handles[::-1], labels[::-1])

# or sort them by labels
import operator
hl = sorted(zip(handles, labels),
            key=operator.itemgetter(1))
handles2, labels2 = zip(*hl)

ax.legend(handles2, labels2)

Bạn sẽ thấy rằng tôi cần tạo thêm một biến ax. Làm thế nào tôi có thể thêm một huyền thoại vào biểu đồ của mình mà không phải tạo biến bổ sung này và giữ lại tính đơn giản của tập lệnh hiện tại của tôi?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. Pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

    Matplotlib.pyplot.legend()

    Bàn luậnlegend() which is used to Place a legend on the axes.

    Matplotlib là một trong những gói Python phổ biến nhất được sử dụng để trực quan hóa dữ liệu. Đây là một thư viện đa nền tảng để tạo các lô 2D từ dữ liệu trong các mảng. Pyplot là một tập hợp các chức năng kiểu lệnh làm cho matplotlib hoạt động như MATLAB. Mỗi hàm pyplot tạo ra một số thay đổi đối với một hình: ví dụ: tạo ra một hình, tạo ra một khu vực âm mưu trong một hình, vẽ một số dòng trong một khu vực âm mưu, trang trí cốt truyện bằng nhãn, v.v.Loc in legend() is used to specify the location of the legend.Default value of loc is loc=”best” (upper left). The strings ‘upper left’, ‘upper right’, ‘lower left’, ‘lower right’ place the legend at the corresponding corner of the axes/figure.

    Một truyền thuyết là một khu vực mô tả các yếu tố của biểu đồ. Trong thư viện matplotlib, có một hàm gọi là Legend () được sử dụng để đặt một huyền thoại trên các trục.bbox_to_anchor=(x, y) of legend() function is used to specify the coordinates of the legend, and the attribute ncol represents the number of columns that the legend has.It’s default value is 1.

    Syntax:

    Thuộc tính LỘC trong legend() được sử dụng để chỉ định vị trí của giá trị legend.default của loc là loc = Hồi tốt nhất (phía trên bên trái). Các chuỗi ‘phía trên bên trái,‘ phía trên bên phải, ‘dưới bên trái,‘ dưới bên phải, đặt huyền thoại ở góc tương ứng của các trục/hình.

    Thuộc tính bbox_to_anchor = (x, y) của hàm legend () được sử dụng để chỉ định tọa độ của truyền thuyết và ncol thuộc tính biểu thị số lượng cột mà huyền thoại có. Giá trị mặc định của nó là 1.

    • matplotlib.pyplot.legend ([Màu xanh da trời, màu xanh lá cây], bbox_to_anchor = (0,75, 1.15), ncol = 2): [None or bool] Whether to draw a shadow behind the legend.It’s Default value is None.
    • Sau đây là một số thuộc tính của chức năng legend():: [None or int or float] The relative size of legend markers compared with the originally drawn ones.The Default is None.
    • Shadow: [Không có hoặc Bool] Cho dù nên vẽ một cái bóng phía sau giá trị mặc định của Legend.itt là không có.: [None or int] The number of marker points in the legend when creating a legend entry for a Line2D (line).The Default is None.
    • Markerscale: [Không có hoặc int hoặc float] Kích thước tương đối của các dấu hiệu huyền thoại so với các điểm được vẽ ban đầu. Mặc định là không có.: The font size of the legend.If the value is numeric the size will be the absolute font size in points.
    • Numpoints: [Không có hoặc int] số điểm đánh dấu trong truyền thuyết khi tạo một mục nhập huyền thoại cho một line2D (dòng). Mặc định là không có.: [None or “inherit” or color] The legend’s background color.
    • Phông chữ: Kích thước phông chữ của huyền thoại. Nếu giá trị là số kích thước sẽ là kích thước phông chữ tuyệt đối theo điểm.: [None or “inherit” or color] The legend’s background patch edge color.

    facecolor: [Không có hoặc kế thừa của người Hồi giáo hoặc màu sắc] màu nền Legend Legend.

    EDGECOLOR: [Không có hoặc thừa kế hoặc màu sắc] Màu sắc cạnh của Legend Legend.

    Các cách sử dụng hàm legend () trong Python -

    Ví dụ 1:

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    0

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    2

    PyPlot9

    matplotlib0matplotlib1matplotlib2

    matplotlib3

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

    Matplotlib6

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7Matplotlib222

    Các cách sử dụng hàm legend () trong Python -

    Ví dụ 1:

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    0

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    2

    ax6

    ax7

    matplotlib0ax9

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7legend()1legend()2
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4legend()4legend()5

    matplotlib3

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

    Matplotlib6

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7Matplotlib222

    Các cách sử dụng hàm legend () trong Python -

    Ví dụ 1:

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    0

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    2

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    12
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    14legend()5

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    16
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    18legend()5

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    20

    matplotlib3

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

    Matplotlib6

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7Matplotlib222

    Các cách sử dụng hàm legend () trong Python -

    Ví dụ 1:

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    0

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    2

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    38
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    39
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    40
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    42legend()5

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    44
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    46
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    40
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    49legend()5

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    51
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    52legend()5

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    Output:

    Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?

    Matplotlib6

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7Matplotlib222

    Các cách sử dụng hàm legend () trong Python -

    Ví dụ 1:

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    0

    import

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    2

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    3
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    8__

    Matplotlib27

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4Matplotlib29legend()5

    Matplotlib31

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4Matplotlib33legend()5

    Matplotlib6

    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    4
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    5
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    6
    ax = subplot(1,1,1)
    p1, = ax.plot([1,2,3], label="line 1")
    p2, = ax.plot([3,2,1], label="line 2")
    p3, = ax.plot([2,3,1], label="line 3")
    
    handles, labels = ax.get_legend_handles_labels()
    
    # reverse the order
    ax.legend(handles[::-1], labels[::-1])
    
    # or sort them by labels
    import operator
    hl = sorted(zip(handles, labels),
                key=operator.itemgetter(1))
    handles2, labels2 = zip(*hl)
    
    ax.legend(handles2, labels2)
    
    7Matplotlib222

    matplotlib3

    Output:

    Hướng dẫn how do you add a legend to a graph in python? - làm cách nào để bạn thêm chú giải vào biểu đồ trong python?


    Làm thế nào để bạn thêm nhãn vào đồ thị trong Python?

    Với pyplot, bạn có thể sử dụng các hàm xlabel () và ylabel () để đặt nhãn cho trục x và y ...
    Thêm nhãn vào trục x và y: nhập Numpy dưới dạng NP. ....
    Thêm một tiêu đề cốt truyện và nhãn cho trục x và y: nhập Numpy dưới dạng NP. ....
    Đặt thuộc tính phông chữ cho tiêu đề và nhãn: Nhập Numpy dưới dạng NP. ....
    Định vị tiêu đề bên trái:.

    Làm cách nào để hiển thị một huyền thoại trong matplotlib?

    Matplotlib với Python đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô con.Sử dụng phương thức PLOT (), các dòng vẽ với nhãn LINE1, line2 và line3.Đặt một huyền thoại trên hình bằng phương thức Legend (), với số lượng nhãn cho giá trị NCOL trong đối số.Để hiển thị hình, sử dụng phương thức show ().use show() method.

    Làm thế nào để bạn thêm một tiêu đề và truyền thuyết vào một cốt truyện trong Python?

    Trong ví dụ này, chúng tôi sẽ vẽ các dòng khác nhau với sự trợ giúp của matplotlib và sử dụng đối số tiêu đề cho PLT.Legend () để chỉ định tiêu đề huyền thoại ...
    Nhập mô -đun yêu cầu ..
    Tạo dữ liệu ..
    Thêm một tiêu đề vào một huyền thoại ..
    Thông thường vẽ dữ liệu ..
    Hiển thị âm mưu ..

    Lệnh huyền thoại trong Python là gì?

    Huyền thoại có thể được đặt ở các vị trí khác nhau: một truyền thuyết có thể được đặt bên trong hoặc bên ngoài biểu đồ và vị trí có thể được di chuyển.Phương thức Legend () thêm huyền thoại vào cốt truyện.Trong bài viết này, chúng tôi sẽ chỉ cho bạn một số ví dụ về các huyền thoại sử dụng matplotlib.Khóa học liên quan.Trực quan hóa dữ liệu với matplotlib và python.The legend() method adds the legend to the plot. In this article we will show you some examples of legends using matplotlib. Related course. Data Visualization with Matplotlib and Python.