Bộ dữ liệu Penguins được sử dụng ở đây đã được thu thập và cung cấp bởi Tiến sĩ. Kristen Gorman tại Trạm Palmer, Nam Cực LTER. Tập dữ liệu này đã được Allison Horst phổ biến trong gói R của cô ấy
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
0 với mục tiêu cung cấp giải pháp thay thế cho tập dữ liệu mống mắt để khám phá và trực quan hóa dữ liệudata = palmerpenguins.load_penguins[]
data.head[]
speciesislandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsexyear0AdelieTorgersen39. 118. 7181. 03750. 0male20071AdelieTorgersen39. 517. 4186. 03800. 0nữ20072AdelieTorgersen40. 318. 0195. 03250. 0female20073AdelieTorgersenNaNNaNNaNNaNNaN20074AdelieTorgersen36. 719. 3193. 03450. 0nữ2007
Mục tiêu của ngày hôm nay là cho bạn thấy rất nhiều ví dụ cho thấy cách tùy chỉnh các khía cạnh khác nhau của chú giải. Hy vọng rằng sau khi đọc bài đăng này, bạn sẽ có thể lấy những phần mình cần và xây dựng huyền thoại tùy chỉnh của riêng mình
Biểu đồ cơ sở sẽ là một biểu đồ phân tán có chiều dài chân chèo so với chiều dài mỏ, được tô màu theo loài
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
Chú thích mặc định
Hãy bắt đầu bằng cách tạo biểu đồ và gọi
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
1 để xem hành vi mặc định của matplotlib khi thêm chú thích là gìfig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
Theo mặc định, Matplotlib tự động tạo chú giải phản ánh chính xác màu sắc và nhãn mà chúng tôi đã chuyển. Thông thường, nó cũng đặt huyền thoại ở một nơi tốt. Nhưng đó không phải là trường hợp ở đây vì truyền thuyết trùng lặp với một trong các dấu chấm
Điểm đánh dấu tự động chính xác
Ngoài ra, Matplotlib còn phản ánh các điểm đánh dấu khác nhau trong biểu đồ. Nên các bạn cứ quan tâm dùng mấy cái marker đẹp thôi còn legend sẽ update miễn phí
# The markers we use in the scatterplot
MARKERS = ["o", "^", "s"] # circle, triangle, square
fig, ax = plt.subplots[figsize=[8,8]]
for species, color, marker in zip[SPECIES_, COLORS, MARKERS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, marker=marker, alpha=0.7
]
ax.legend[];
Bây giờ, hãy xem nhiều cách tiếp cận khác nhau mà người ta có thể sử dụng để định vị chú giải ở những nơi khác nhau
Điều chỉnh vị trí chú thích bằng FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
2
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
Điều đầu tiên người ta có thể làm là truyền một cái gì đó cho đối số
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
2. Đây có thể là một chuỗi bằng tiếng Anh đơn giản cho biết vị trí của nhãn hoặc một số. Bạn có thể tìm thấy mô tả đầy đủ về các mã này trong tài liệu matplotlib của đối số FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
2fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
# Lower right corner is a better place for this legend
ax.legend[loc="lower right"];
# The same result with
# ax.legend[loc=4];
Truyền thuyết bên ngoài khu vực cốt truyện với FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
5
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
Cũng có thể đưa nó vào vị trí chú giải bên ngoài vùng vẽ đồ thị [i. e. ra khỏi trục]. Để làm như vậy, chúng ta cần tạo khoảng trống trong hình bất động sản nhờ hàm
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
6, sau đó sử dụng cùng một đối số FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
2 được mô tả ở trênfig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
# Let's say we want it on the right side.
# First, make room on the right side of the figure.
fig.subplots_adjust[right=0.8]
# Add the legend
# Pass `fig.transFigure` as the bounding box transformation 'bbox_transform'
# loc="center left" and bbox_to_anchor=[0.8, 0.5] indicate the left border
# of the legend is placed at 0.8 in the x axis of the figure, and it is
# vertically centered at y = 0.5
ax.legend[
loc="center left",
bbox_to_anchor=[0.8, 0.5],
bbox_transform=fig.transFigure
];
Người ta có thể làm điều tương tự để đặt chú thích ở phía bên trái của cốt truyện
fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
# Make room on the left side of the figure.
fig.subplots_adjust[left=0.2]
# Add the legend
# Now x = 0.16 to leave some space for the axis tick labels
ax.legend[
loc="center right",
bbox_to_anchor=[0.16, 0.5],
bbox_transform=fig.transFigure
];
Chúng tôi có thể có chú thích trên đầu trang/dưới cùng? . Trong trường hợp này, sẽ hợp lý hơn nhiều khi có các mục nhập cạnh nhau, vì vậy chúng tôi sử dụng
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
8 để nói với Matplotlib rằng chú giải có ba cộtfig, ax = plt.subplots[figsize=[9, 6]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
# Make room on top now
fig.subplots_adjust[top=0.8]
ax.legend[
loc="lower center", # "upper center" puts it below the line
ncol=3,
bbox_to_anchor=[0.5, 0.8],
bbox_transform=fig.transFigure
];
fig, ax = plt.subplots[figsize=[9, 6]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
# Make room on below
fig.subplots_adjust[bottom=0.2]
# Again, leave some extra space for the axis tick labels
ax.legend[
loc="upper center",
ncol=3,
bbox_to_anchor=[0.5, 0.16],
bbox_transform=fig.transFigure
];
Căn chỉnh chú thích với trục
Có thể căn chỉnh cốt truyện sang phải hoặc trái không?
Một điểm khác biệt với các ô ở trên là ở đây chúng tôi không sử dụng
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
9. Nếu chúng ta muốn căn chỉnh ranh giới của chú giải với ranh giới của trục, sẽ dễ dàng hơn khi sử dụng mặc định là trục. Lưu ý giá trị fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
0. Nó có nghĩa là chú thích là 5% chiều cao của trục trên ranh giới trên cùng của nódata = palmerpenguins.load_penguins[]
data.head[]
0Tùy chỉnh nhãn
Người ta có thể lưu trữ đối tượng được trả về bởi
FLIPPER_LENGTH = data["flipper_length_mm"].values
BILL_LENGTH = data["bill_length_mm"].values
SPECIES = data["species"].values
SPECIES_ = np.unique[SPECIES]
COLORS = ["#1B9E77", "#D95F02", "#7570B3"]
1 và làm nhiều điều thú vị với nóTruyền thuyết được trả về có một phương thức
fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
2 trả về danh sách các đối tượng fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
3. Những đối tượng này có rất nhiều phương thức mà người ta có thể sử dụng để tùy chỉnh giao diện của văn bảndata = palmerpenguins.load_penguins[]
data.head[]
1fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
4Hãy xem một ví dụ khác với nhiều tinh chỉnh hơn
data = palmerpenguins.load_penguins[]
data.head[]
2Rất nhiều thứ có thể được tùy chỉnh
Nếu bạn muốn tự mình thực hiện một số nghiên cứu, bạn luôn có thể gọi điện cho
fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
5 để xem các thuộc tính và phương thức của đối tượng fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
6 là gìTùy chỉnh tiêu đề
Cho đến nay, huyền thoại không có tiêu đề. Hãy thêm một và tùy chỉnh một số thuộc tính của nó
data = palmerpenguins.load_penguins[]
data.head[]
3Hãy đẩy xa hơn một chút
data = palmerpenguins.load_penguins[]
data.head[]
4fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
7Và thậm chí xa hơn nữa?
data = palmerpenguins.load_penguins[]
data.head[]
5Tùy chỉnh tay cầm
data = palmerpenguins.load_penguins[]
data.head[]
6Tùy chỉnh bố cục
Một số tên đối số có liên quan và ý nghĩa của chúng
8. Vùng đệm giữa các trục và đường viền chú giải, theo đơn vị cỡ chữfig, ax = plt.subplots[figsize=[8,8]] for species, color in zip[SPECIES_, COLORS]: idxs = np.where[SPECIES == species] # No legend will be generated if we don't pass label=species ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, alpha=0.7 ] ax.legend[];
9. Khoảng trắng phân đoạn bên trong đường viền chú giải, tính theo đơn vị cỡ chữfig, ax = plt.subplots[figsize=[8,8]] for species, color in zip[SPECIES_, COLORS]: idxs = np.where[SPECIES == species] # No legend will be generated if we don't pass label=species ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, alpha=0.7 ] ax.legend[];
0. Khoảng cách giữa các cột, theo đơn vị cỡ chữ# The markers we use in the scatterplot MARKERS = ["o", "^", "s"] # circle, triangle, square fig, ax = plt.subplots[figsize=[8,8]] for species, color, marker in zip[SPECIES_, COLORS, MARKERS]: idxs = np.where[SPECIES == species] ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, marker=marker, alpha=0.7 ] ax.legend[];
1. Độ dài của phần xử lý chú giải, tính theo đơn vị cỡ chữ# The markers we use in the scatterplot MARKERS = ["o", "^", "s"] # circle, triangle, square fig, ax = plt.subplots[figsize=[8,8]] for species, color, marker in zip[SPECIES_, COLORS, MARKERS]: idxs = np.where[SPECIES == species] ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, marker=marker, alpha=0.7 ] ax.legend[];
2. Vùng đệm giữa điều khiển chú thích và văn bản, theo đơn vị cỡ chữ# The markers we use in the scatterplot MARKERS = ["o", "^", "s"] # circle, triangle, square fig, ax = plt.subplots[figsize=[8,8]] for species, color, marker in zip[SPECIES_, COLORS, MARKERS]: idxs = np.where[SPECIES == species] ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, marker=marker, alpha=0.7 ] ax.legend[];
3. Khoảng cách dọc giữa các mục chú giải, tính theo đơn vị cỡ chữ# The markers we use in the scatterplot MARKERS = ["o", "^", "s"] # circle, triangle, square fig, ax = plt.subplots[figsize=[8,8]] for species, color, marker in zip[SPECIES_, COLORS, MARKERS]: idxs = np.where[SPECIES == species] ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, marker=marker, alpha=0.7 ] ax.legend[];
4. Kích thước tương đối của các điểm đánh dấu chú giải so với những điểm được vẽ ban đầu# The markers we use in the scatterplot MARKERS = ["o", "^", "s"] # circle, triangle, square fig, ax = plt.subplots[figsize=[8,8]] for species, color, marker in zip[SPECIES_, COLORS, MARKERS]: idxs = np.where[SPECIES == species] ax.scatter[ FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species, s=50, color=color, marker=marker, alpha=0.7 ] ax.legend[];
data = palmerpenguins.load_penguins[]
data.head[]
7Hãy sử dụng bố cục 3 cột và tùy chỉnh khoảng cách giữa các cột
data = palmerpenguins.load_penguins[]
data.head[]
8Và cuối cùng, một ví dụ tùy chỉnh nhiều khía cạnh cùng một lúc
data = palmerpenguins.load_penguins[]
data.head[]
9fig, ax = plt.subplots[figsize=[8,8]]
for species, color in zip[SPECIES_, COLORS]:
idxs = np.where[SPECIES == species]
# No legend will be generated if we don't pass label=species
ax.scatter[
FLIPPER_LENGTH[idxs], BILL_LENGTH[idxs], label=species,
s=50, color=color, alpha=0.7
]
ax.legend[];
7Đi đâu tiếp theo?
Sẽ không thực sự thú vị khi xem cách những thứ này được sử dụng trong các ví dụ thực tế phải không? . Sau đây là danh sách các trực quan hóa tùy chỉnh cao được tạo trong Matplotlib có chứa các huyền thoại đẹp mắt được tạo bằng các thủ thuật được hiển thị ở trên. có một cái nhìn