Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีได้สีน้ำเงิน Default ของ matplotlib.pyplot.scatter


ภาพรวม

สี Default ของ Scatter Points คือสีน้ำเงิน #1f77b4 (Tableau Blue) สาธิตการตรวจสอบและใช้สีนี้

ขั้นตอน

  1. สร้าง Figure, Subplot
  2. พลอต Sc จุดที่ (-1, 1) โดยไม่ระบุสี (ใช้ Default)
  3. Annotate ป้ายกำกับ
  4. พลอตจุดที่ (1, 1) ระบุสี c='#1f77b4' (Hex)
  5. Annotate ป้ายกำกับ
  6. ตั้งค่า Limits และแสดงผล

ตัวอย่างโค้ด

from matplotlib import pyplot as plt

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

fig, ax = plt.subplots()
ax.scatter(-1, 1)
ax.annotate("default color", xy=(-0.9, 1))
ax.scatter(1, 1, c='#1f77b4')
ax.annotate("using hex", xy=(1.1, 1))
ax.set_xlim(-2, 3)
ax.set_ylim(-1, 2)
plt.show()

ผลลัพธ์

จุดทั้งสองมีสีเหมือนกัน สีน้ำเงิน Tableau Blue (#1f77b4)

รหัสสี Default ทั้งหมด (Tableau 10)

['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
 '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']

เข้าถึงได้จาก plt.rcParams['axes.prop_cycle'].by_key()['color']