Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Thay đổi màu và marker từng điểm trong Seaborn jointplot

Để tùy chỉnh màu và marker cho từng điểm dữ liệu riêng lẻ trong sns.jointplot(), ta xóa axes joint mặc định và vẽ lại thủ công.

Các bước

  • Thiết lập figure size và autolayout.
  • Load dataset mẫu tips từ Seaborn (cần internet).
  • Tạo jointplot mặc định.
  • Xóa axes joint: g.ax_joint.cla().
  • Tạo mảng màu ngẫu nhiên và danh sách marker.
  • Duyệt từng hàng dữ liệu, vẽ điểm riêng bằng plot() với màu/marker riêng.
  • Đặt nhãn axes và hiển thị.

Ví dụ

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip", data=tips, height=4.50)
g.ax_joint.cla()
colors = np.random.random((len(tips), 3))
markers = ['v', '^', '<', '*', 'o'] * 100
for i, row in enumerate(tips.values):
    g.ax_joint.plot(row[0], row[1], color=colors[i], marker=markers[i])
g.set_axis_labels('total bill', 'tip', fontsize=10)
plt.show()

Kết quả

Thay đổi màu và marker từng điểm trong Seaborn jointplot