Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีสร้าง Animation จากลำดับกราฟ Matplotlib แบบ Time-Ordered

    การสร้าง Animation จากลำดับกราฟ (Sequence of Plots) ที่เรียงตามเวลา ใน Matplotlib สามารถทำได้โดยใช้ FuncAnimation ร่วมกับการอัปเดตข้อมูลบน Axes เดียวกัน หลักการทำงาน สร้าง Figure และ Axes เตรียมข้อมูลหลายเฟรม (หรือสร้างข้อมูลสุ่มในฟังก์ชัน animate) สร้าง Artist เริ่มต้น (เช่น imshow, p

  2. ดึงค่า (x,y) ของเส้น Contour Plot ใน Matplotlib

    ใช้ contour() ได้ collections → get_paths() → vertices ของแต่ละ path ตัวอย่างโค้ด import matplotlib.pyplot as plt plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True m = [[3, 2, 1, 0], [2, 4, 1, 0], [2, 4, 1, 3], [4, 3, 1, 3]] cs = plt.contour([3, 4, 2, 1], [5, 1,

  3. วาด 3D Surface จากข้อมูล Scatter {x,y,z} ใน Matplotlib

    ใช้ plot_surface() กับ meshgrid จากข้อมูล x, y คำนวณ z ตัวอย่างโค้ด import matplotlib.pyplot as plt import numpy as np plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True fig = plt.figure() ax = fig.add_subplot(111, projection=3d) x = np.array(np.linspace(-2, 2, 1

  4. วาดเส้นเดียวที่สีเปลี่ยนไปเรื่อยๆ ใน Matplotlib

    แบ่งเส้นเป็นช่วงๆ แล้ววาดแต่ละช่วงด้วยสีสุ่มในลูป ตัวอย่างโค้ด import matplotlib.pyplot as plt import numpy as np import random plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True x = np.linspace(1, 10, 100) y = np.sin(x) fig, ax = plt.subplots() for i in range(0,

  5. วาง Legend Symbol แบบกำหนดเองใน Matplotlib

    สร้าง custom handler โดยสืบทอด HandlerPatch และ override create_artists ตัวอย่างโค้ด import matplotlib.pyplot as plt, matplotlib.patches as mpatches from matplotlib.legend_handler import HandlerPatch plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True class Handler

  6. แก้ Deprecation Warning ของ pylab.pause ใน Matplotlib

    ใช้ warnings.filterwarnings(ignore) เพื่อปิด warning ชั่วคราว ตัวอย่างโค้ด from matplotlib import pyplot as plt, pylab as pl import warnings plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True warnings.filterwarnings(ignore) pl.pause(0) plt.show() ผลลัพธ์ Process

  7. 3D Scatter Plot พร้อม Hue Colormap และ Legend ใน Matplotlib

    ใช้ scatter() บน axes 3D พร้อม cmap และ legend_elements() สำหรับ legend ตัวอย่างโค้ด import numpy as np import seaborn as sns from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True x = n

  8. ใช้ Pseudocolor กับภาพใน Matplotlib

    Pseudocolor เหมาะกับภาพ grayscale (single channel) เพิ่มคอนทราสต์ ใช้ imshow() กับ colormap ต่างๆ ตัวอย่างโค้ด from matplotlib import pyplot as plt, image as mimg plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True img = mimg.imread(bird.jpg) lum_img = img[:, :, 0]

  9. ตั้งค่า Colormap ให้ภาพใน Matplotlib

    ใช้ imshow() พร้อมพารามิเตอร์ cmap เช่น hot, viridis, plasma เป็นต้น ตัวอย่างโค้ด from matplotlib import pyplot as plt, image as mimg plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True img = mimg.imread(bird.jpg) lum_img = img[:, :, 0] # เลือก channel แรก (Red) p

  10. ลบเส้น/โค้งเฉพาะใน Matplotlib

    ใช้ plot() คืนค่า list ของ Line2D objects ใช้ pop() แล้ว remove() ตัวอย่างโค้ด from matplotlib import pyplot as plt, image as mimg plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True line_1 = plt.plot([1, 2, 3]) line_2 = plt.plot([2, 4, 6]) line = line_2.pop(0) li

  11. วาด Horizontal Bar Chart 2 ชุด แบบ Share Y-axis ใน Matplotlib

    ใช้ subplot() พร้อม sharey=ax1 และเมธอด barh() สำหรับแท่งแนวนอน ตัวอย่างโค้ด import matplotlib.pyplot as plt import numpy as np plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True y = [3, 1, 5] x1 = [10, 7, 3] x2 = [9, 5, 1] fig = plt.figure() axe1 = plt.subplot(1

  12. ติดตั้ง Matplotlib บน Mac 10.7 ใน virtualenv

    ขั้นตอนติดตั้งใน terminal: virtualenv <env_name> (เช่น virtualenv env) source env/bin/activate pip install matplotlib requirements.txt cat requirements.txt (ตรวจสอบเวอร์ชัน) หมายเหตุ: Mac OS X 10.7 (Lion) เป็นระบบเก่ามาก อาจต้องใช้ matplotlib เวอร์ชันเก่าที่รองรับ Python 2.7 หรืออัปเกรด OS

  13. ทำให้ Poly3DCollection โปร่งใสใน Matplotlib 3D

    ใช้ Poly3DCollection กับพารามิเตอร์ alpha (0-1) สำหรับความโปร่งใสของหน้า 3D ตัวอย่างโค้ด from matplotlib import pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True fig = plt.fig

  14. เพิ่ม Title ให้ Seaborn lmplot

    Seaborn lmplot() สร้าง FacetGrid ไม่ใช่ Axes ตรงๆ ต้องใช้ plt.gca() ได้ axes แล้วค่อย set_title() ตัวอย่างโค้ด import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np plt.rcParams[figure.figsize] = [7.50, 3.50] plt.rcParams[figure.autolayout] = True df = pd.D

  15. 繼承 Frame Class ใน Tkinter Application (OOP)

    ใน OOP สร้าง class สืบทอดจาก Frame เพื่อ reuse properties และ methods ตัวอย่างโค้ด from tkinter import * win = Tk() win.geometry(700x350) class NewFrame(Frame): def __init__(self, win): super().__init__() self[height] = 200 self[width] = 200 self[bd] = 10

  16. Geometry Manager อันไหนดีกว่า .grid() ใน Tkinter

    ไม่มี อันไหนดีกว่า อย่างสมบูรณ์ ขึ้นอยู่กับ use case: Pack Geometry Manager ง่ายที่สุด ใช้สำหรับ stack widgets แนวตั้ง/แนวนอน เหมาะ: toolbar, status bar, simple forms Grid Geometry Manager จัดวาง 2 มิติ (row/column) เหมาะ form ซับซ้อน ควบคุม alignment, spanning ได้ละเอียด Place Geometry Manag

  17. ใช้ OpenCV ร่วมกับ Tkinter แสดงกล้องเว็บแคม

    รวม OpenCV (Computer Vision) กับ Tkinter GUI ต้องติดตั้ง: pip install opencv-python pip install Pillow หลักการ OpenCV อ่านเฟรมจากกล้อง cv2.VideoCapture(0) แปลง BGR → RGB สร้าง PIL Image → ImageTk.PhotoImage แสดงใน Label, ใช้ after() วนลูปอัปเดต ตัวอย่างโค้ด from tkinter import * from PIL import

  18. สร้าง Hyperlink ใน Tkinter Text Widget

    Text widget ไม่มี hyperlink built-in ต้องใช้ snippet HyperlinkManager จาก GitHub ขั้นตอน ดาวน์โหลด tkHyperlinkManager.py จาก GitHub Import: from tkHyperLinkManager import HyperlinkManager สร้าง Text widget และ HyperlinkManager instance ใช้ hyperlink.add(callback) ผูก URL กับข้อความ ตัวอย่างโค้ด

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:297/451  20-คอมพิวเตอร์/Page Goto:1 291 292 293 294 295 296 297 298 299 300 301 302 303