Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. การหาค่า Rolling Mean (ค่าเฉลี่ยเคลื่อนที่) ด้วย Python Pandas

    ภาพรวม Rolling Mean หรือค่าเฉลี่ยเคลื่อนที่ ใช้วิเคราะห์แนวโน้มข้อมูลอนุกรมเวลา (time series) โดยใช้เมธอด rolling() ร่วมกับ apply() หรือเรียก mean() โดยตรง ขั้นตอนการทำงาน Import pandas สร้าง DataFrame ที่มีคอลัมน์หมวดหมู่ (Car) และค่าตัวเลข (Reg_Price) จัดกลุ่มด้วย groupby() แล้วใช้ rolling

  2. วิธีพลอตเส้นหลายสีเมื่อแกน X เป็น Datetime Index ของ Pandas

    ภาพรวม เมื่อแกน X เป็น datetime index การพลอตเส้นที่สีเปลี่ยนตามค่า (multicolor line) ต้องใช้ LineCollection จาก matplotlib.collections ขั้นตอนการทำงาน กำหนดขนาดฟิกเกอร์และปรับ padding สร้างข้อมูล datetime index และค่า y ด้วย numpy สร้าง figure และ axes แปลงวันที่เป็นตัวเลขด้วย mdates.date

  3. วิธีวางฉลาก (Label) บน Bubble Chart/Scatter Plot จากคอลัมน์ Pandas DataFrame

    ภาพรวม บทความนี้สาธิตวิธีเพิ่มข้อความกำกับ (annotation) ให้แต่ละจุดข้อมูลบน Scatter Plot โดยใช้ข้อมูลจากคอลัมน์ของ Pandas DataFrame ขั้นตอนการทำงาน กำหนดขนาดฟิกเกอร์และปรับ padding สร้าง DataFrame ที่มีคอลัมน์ x, y และ points (ข้อความกำกับ) สร้าง Scatter Plot ด้วย df.plot.scatter() ใช้ ax.

  4. วิธีส่งออบเจ็กต์ matplotlib ผ่านฟังก์ชัน: Axis, Axes หรือ Figure

    ภาพรวม เมื่อทำงานกับ matplotlib มักจะต้องส่งออบเจ็กต์การพลอต (plotting objects) ผ่านฟังก์ชันเพื่อสร้างกราฟที่ซับซ้อน คำถามคือควรส่งเป็น Axis, Axes หรือ Figure บทความนี้จะแนะนำแนวทางที่เหมาะสมพร้อมตัวอย่างโค้ด ขั้นตอนการทำงาน กำหนดขนาดฟิกเกอร์และปรับ padding ระหว่าง subplots ในเมธอด plot() ให้พ

  5. Python Matplotlib - แสดง Tick Labels บนสุดของกราฟ

    ใช้ ax.xaxis.set_tick_params(labeltop=True, labelbottom=False) ย้าย Label แกน X ไปด้านบน ตัวอย่างโค้ด from matplotlib import pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True fig, ax = plt.subplots(1, 1) # Show tick labels on top ax.xaxis.set_tick_p

  6. Python Matplotlib - ลบขอบสีขาวเมื่อใช้ subplot และ imshow

    ลบขอบสีขาว (White Border) รอบภาพ imshow โดยตั้งค่า Figure Size ให้ตรง Aspect Ratio ของข้อมูล และปิด Axis ตัวอย่างโค้ด import numpy as np import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True data = np.random.randint(0, 50, (50, 50)) siz

  7. Python Matplotlib - ตั้ง xticks และ yticks สำหรับ imshow Plot

    ใช้ ax.set_xticks() และ ax.set_yticks() กำหนดตำแหน่ง Ticks บน Heatmap/Image จาก imshow() ตัวอย่างโค้ด import numpy as np import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True ax = plt.gca() data = np.random.rand(6, 6) ax.imshow(data)

  8. Python Matplotlib - เลื่อนกราฟตามแกน X (Shift Graph)

    เลื่อนกราฟตามแกน X โดย Plot เส้นเดิม และเส้นที่เลื่อน (Shift X Values) พร้อม Legend ตัวอย่างโค้ด import numpy as np import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True x = np.linspace(-5, 5, 100) y = np.sin(x) # Original graph plt.pl

  9. Python Matplotlib - วาดกราฟ 2 Array ความยาวต่างกัน

    Plot 2 ชุดข้อมูลที่มีความยาว Array ต่างกัน โดยระบุ X, Y แยกกันสำหรับแต่ละชุด ตัวอย่างโค้ด import numpy as np import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True y1 = (np.random.random(100) - 0.5).cumsum() y2 = y1.reshape(-1, 10).mean(

  10. Python - วาด Multi-line Plot จากไฟล์ CSV ด้วย Matplotlib

    อ่าน CSV หลายคอลัมน์ แล้วใช้ DataFrame.plot() วาดกราฟเส้นหลายเส้นพร้อมกัน ตัวอย่างโค้ด import pandas as pd from matplotlib import pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True columns = [mpg, displ, hp, weight] df = pd.read_csv(auto-mpg.csv, usec

  11. Python Matplotlib - ปิด Ticks และ Labels บนแกน X และ Y

    ใช้ set_tick_params() ปิด Label และ set_xticks([]), set_yticks([]) ปิด Tick Marks ตัวอย่างโค้ด import numpy as np from matplotlib import pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True x = np.linspace(-10, 10, 100) y = np.sin(x) plt.plot(x, y) ax

  12. Python Matplotlib - สร้าง Broken Horizontal Bar Chart

    ใช้ ax.broken_barh() วาดแท่งแนวนอนที่斷續 (Broken) หลายช่วงในแต่ละ Row ตัวอย่างโค้ด import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True fig, ax = plt.subplots() # Row 1: 2 bars at y=10-19 (height 9) ax.broken_barh([(110, 30), (150, 10)

  13. Python Matplotlib - ตั้ง Label ให้ Horizontal Lines (hlines)

    ใช้ plt.hlines() วาดเส้นแนวนอน แล้วเพิ่ม Label ด้วย plt.text() ที่ปลายเส้น ตัวอย่างโค้ด import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.00, 3.50] plt.rcParams[figure.autolayout] = True # Horizontal line y=1, x from 1 to 4, orange plt.hlines(y=1, xmin=1, xmax=4, lw=7, color=orang

  14. Python - สร้าง Animation 3D ด้วย Matplotlib

    สร้าง Animation 3D โดยใช้ mpl_toolkits.mplot3d.Axes3D และ matplotlib.animation.FuncAnimation ขั้นตอน Import: Axes3D, animation สร้างข้อมูล Parametric: t, x=cos(t)-1, y=0.5*(cos(2t)-1), z=t สร้าง Figure และ 3D Axes ปิด Axis: ax.axis(off) Plot เส้นเริ่มต้น กำหนดฟังก์ชัน anima

  15. Python Matplotlib - วาดวงกลมพร้อม Annotation บนกราฟ

    ใช้ Matplotlib วาดจุดข้อมูล วาดวงกลมรอบจุดที่สนใจ และเพิ่ม Annotation พร้อมลูกศร ขั้นตอน ตั้งค่า Figure สร้างข้อมูลและเลือกจุดที่จะทำเครื่องหมาย Plot ข้อมูลทั้งหมด ตั้งค่า Axis Limits วาดวงกลม: plot(x, y, o, ms=radius*2, mec=yellow, mfc=none, mew=2) เพิ่ม Annotation: annota

  16. Python - วาด Scatter Plot สำหรับ Clustering ด้วย Matplotlib

    สร้าง Scatter Plot แสดง Clustering โดยใช้ Matplotlib: สุ่มข้อมูล, กำหนด Cluster และ Centers แล้ว Plot พร้อม Marker ต่างกัน ขั้นตอน ตั้งค่า Figure Size และ Layout สร้างข้อมูล X, Y, Cluster Labels, Centers ด้วย NumPy สร้าง Figure และ Subplot Plot Scatter ข้อมูล สีตาม Cluster Plot

  17. Python - ทดสอบว่าสมาชิกในลิสต์อยู่ในช่วง Min/Max ของอีกลิสต์หนึ่ง

    ตรวจสอบว่าทุกค่าใน range_list อยู่ในช่วง Min-Max ของ my_list หรือไม่ (Logic ในโค้ดตัวอย่างมีข้อบกพร่อง) ตัวอย่างโค้ด my_list = [5, 6, 4, 7, 8, 13, 15] print(The list is : ) print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem != max(my_list): my_r

  18. Python - หาผลรวมความยาวสตริงที่ Index ที่กำหนด

    ใช้ enumerate() วนลูปลิสต์สตริง พร้อมเช็คว่า Index อยู่ในลิสต์ Index ที่ต้องการหรือไม่ แล้วบวกความยาว ตัวอย่างโค้ด my_list = [python, is, best, for, coders] print(The list is :) print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list:

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:344/451  20-คอมพิวเตอร์/Page Goto:1 338 339 340 341 342 343 344 345 346 347 348 349 350