ในการหลีกเลี่ยงการซ้ำสีเส้นใน matplotlib.pyplot สามารถทำตามขั้นตอนดังนี้:
ขั้นตอน
ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
สร้างข้อมูล x และ y ด้วย numpy
วาดข้อมูล x และ y ด้วยเมธอด plot()
ในเมธอด plot() ใช้ค่า hexadecimal เฉพาะตัวสำหรับแอตทริบิวต์ color เช่น color="#980ab5" เพื่อตั้งค่ากราฟสีเฉพาะตัว กำหนดสีที่ต้องการเช่น color="green" ได้เช่นกัน
แสดงรูปด้วยเมธอด show()
ตัวอย่าง
import numpy as np from matplotlib import pyplot as plt # ตั้งค่าขนาดรูป plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # x และ y data points x = np.linspace(1, 100, 1000) y = np.log(x) # วาด x และ y พร้อม color attribute plt.plot(x, y, color="#980ab5", lw=3) # แสดง plot plt.show()
ผลลัพธ์
จะแสดงกราฟ log(x) สีม่วงเข้ม (#980ab5) ความหนา 3

หากรันโค้ดโดยไม่มี color attribute กราฟจะใช้สี default
