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 inMatplotlib
'sPyPlot
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:
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ả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]
0import
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]
2PyPlot
9
matplotlib
0matplotlib
1matplotlib
2
matplotlib
3
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
6
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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
222
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]
0import
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]
2ax
6
ax
7
matplotlib
0ax
9
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[]
2ax = 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[]
5matplotlib
3
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
6
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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
222
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]
0import
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]
2ax = 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]
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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]
12ax = 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]
4ax = 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[]
5ax = 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]
16ax = 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]
4ax = 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[]
5ax = 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]
20matplotlib
3
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
6
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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
222
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]
0import
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]
2ax = 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]
38ax = 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]
39ax = 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]
40ax = 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]
4ax = 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[]
5ax = 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]
44ax = 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]
4ax = 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]
46ax = 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]
40ax = 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]
4ax = 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[]
5ax = 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]
51ax = 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[]
5ax = 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]
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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:
Matplotlib
6
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]
5ax = 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]
6ax = 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]
7ax = 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]
Matplotlib
222
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]
0import
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]
2ax = 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]
3ax = 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]
5ax = 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]
6ax = 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]
7ax = 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__Matplotlib
27
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]
4Matplotlib
29legend[]
5Matplotlib
31
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]
4Matplotlib
33legend[]
5Matplotlib
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]
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]
5ax = 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]
6ax = 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]
7Matplotlib
222matplotlib
3
Output: