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

วิธีจัดเงาสีจุดใน Scatter Plot ตาม Colormap ใน Matplotlib


ใช้ colormap copper ในเมธอด scatter()

ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูลสุ่ม x และ y 100 จุดด้วย numpy
  • พล็อตจุดกระจาย x และ y พร้อม color=x และ colormap=copper
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import numpy as np
from matplotlib import pyplot as plt, cm

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

x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y, c=x, cmap='copper')
plt.show()

ผลลัพธ์

วิธีจัดเงาสีจุดใน Scatter Plot ตาม Colormap ใน Matplotlib