Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีเปลี่ยนช่วงแกน X และ Y ใน Matplotlib

    การกำหนดช่วงแกน X และ Y แบบ Manual ใช้ xlim() และ ylim() กำหนดขอบเขตการแสดงผล ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูล x, y ด้วย numpy พล็อตข้อมูล ตั้งค่า plt.xlim(-10, 10) และ plt.ylim(-1, 1) แสดงผลด้วย show() ตัวอย่างโค้ด import numpy as np import matplotlib.pyplot as plt plt

  2. วิธีพล็อต Arbitrary Markers บน Pandas Data Series ด้วย Matplotlib

    การใช้ Marker กำหนดเองบน Pandas Series Plot ใช้ pyplot.plot() โดยตรงกับ Series index และ values ระบุ marker ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง Pandas Series พร้อม datetime index พล็อตด้วย plt.plot(ts.index, ts, *, ls=dotted, color=red) หมุน labels ด้วย tick_params(rotation=45) แส

  3. วิธีพล็อตข้อมูล True/False หรือ Active/Inactive ใน Matplotlib

    การแสดงข้อมูล Boolean (True/False) เป็นภาพ ใช้ imshow() แสดง boolean array เป็นภาพขาวดำ/สี ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง boolean array ด้วย 0.5 สร้าง figure และ axes ใช้ imshow() พร้อม aspect=auto, cmap, interpolation แสดงผลด้วย show() ตัวอย่างโค้ด 0.5 fig = plt.figure()

  4. Matplotlib Colorbar Background และ Label Placement

    การปรับ Colorbar Background และ Label Placement ใช้ colorbar.set_ticklabels() ปรับแต่ง labels บน colorbar ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูล 2D ด้วย numpy linspace reshape พล็อต contourf สร้าง colorbar จาก contour set ตั้งค่า ticklabels ใหม่ด้วย cb.set_ticklabels([1, 2, 3,

  5. Horizontal Stacked Bar Chart ใน Matplotlib

    การสร้าง Stacked Horizontal Bar Chart ใช้ barh() สองครั้ง ครั้งที่สองระบุ left parameter เป็นค่าของครั้งแรก ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding เตรียมข้อมูล year, issues_addressed, issues_pending พล็อต barh แรก (completed) สีแดง พล็อต barh ที่สอง (pending) left=issues_addressed สีเหลือง

  6. วิธีล้าง Textbox ที่เคยวาดใน Matplotlib

    การลบ Text Object ที่สร้างไว้ก่อนหน้า เก็บ reference ของ text object ที่คืนจาก fig.text() แล้วเรียก .remove() ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูลและพล็อต สร้าง text ด้วย text = fig.text(0.5, 0.96, $y=sin(x)$) ลบ text ด้วย text.remove() (uncomment เพื่อทดสอบ) แสดงผลด้วย show(

  7. วิธีพล็อตด้วย Scale ต่างกันใน Matplotlib

    การพล็อตข้อมูลสองชุดที่มี Scale ต่างกัน (Dual Y-axis) ใช้ ax.twinx() สร้าง axes ที่สองใช้แกน x เดียวกัน แต่ง y แยกกัน ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูล t, data1 (exp), data2 (sin) สร้าง figure และ axes แรก ax1 ตั้งค่า labels และ plot data1 บน ax1 ตั้งสี label ด้วย tick_param

  8. วิธีกำหนดสีเฉพาะให้เซลล์เฉพาะใน Matplotlib Table

    การระบุสีพื้นหลังเซลล์แต่ละเซลล์ใน Table ใช้ parameter cellColours (list of lists) ใน ax.table() ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding กำหนด columns tuple เตรียม cell_text (list of lists) และ colors (list of lists สีแต่ละเซลล์) สร้าง figure และ axes เพิ่ม table ด้วย ax.table(cellText=...,

  9. การคำนวณ Curl ของ Vector Field ใน Python และพล็อตด้วย Matplotlib

    การคำนวณและพล็อต Curl ของ Vector Field 3D ใช้ quiver() แสดง vector field 3D คำนวณ curl components ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง figure และ 3D axes สร้าง meshgrid x, y, z กำหนด vector components u, v, w (curl field) ใช้ ax.quiver() แสดง vectors ปิด axes ด้วย ax.axis(off)

  10. การปรับความสูง Subplot แบบ Absolute (ไม่ใช่ Relative) ใน Matplotlib

    การตั้งค่าความสูง Subplot แบบ Absolute Coordinates ใช้คลาส Axes() โดยตรง ระบุตำแหน่ง [left, bottom, width, height] ใน figure coordinates (0-1) ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง figure สร้าง Axes แรก: pl.Axes(figure, [.4, .6, .25, .25]) (left=0.4, bottom=0.6, width=0.25, height=0.25)

  11. วิธีเข้าถึง Axis Label Object ใน Matplotlib

    การเข้าถึง Object ของ Axis Label ใช้ ax.xaxis.get_label().get_text() ดึงข้อความของ xlabel ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง figure และ axes กำหนด N และข้อมูลสุ่ม พล็อตข้อมูล ตั้งค่า xlabel ด้วย set_xlabel() ดึง label object: x_lab = ax.xaxis.get_label() ดึงข้อความ: x_lab.get

  12. วิธีเลื่อน Tick Label โดยไม่ย้าย Tick ใน Matplotlib

    การเลื่อน Tick Label แยกจาก Tick Mark ใช้ axvline() วาดเส้นแนวตั้ง แล้ว annotate() วาง label ตรงที่ต้องการ ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding กำหนด delta ตำแหน่งที่ต้องการ สร้างข้อมูล x, y วาดเส้นแนวตั้งด้วย axvline(delta, ls=--, color=r) ทำ annotation ด้วย annotate() ระบุตำแหน่ง xy of

  13. Contour Hatching ใน Matplotlib Plot

    การเพิ่ม Hatching Patterns ให้ Contour Plot ใช้ parameter hatches ใน contourf() เพื่อเพิ่มรูปแบบเส้นขีด ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูล x, y, z ด้วย numpy meshgrid Flatten x, y สำหรับ contourf สร้าง figure และ axes พล็อต contourf() พร้อม hatches=[-, /, \\, //] เพิ่ม co

  14. วิธีตั้งค่า Gridlines แนวนอน/แนวตั้ง Major/Minor แยกกันใน Matplotlib

    การตั้งค่า Grid Lines แยกตามแกนและระดับ Major/Minor ใช้ grid() บน axis object เฉพาะ (ax.xaxis, ax.yaxis) พร้อม which parameter ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง figure และ axes ตั้งค่า major grid แนวตั้ง (yaxis) สีแดง solid ตั้งค่า minor locator สำหรับ xaxis ด้วย MultipleLocator(0

  15. วิธีปรับแต่งการแสดง Edge Labels ใน NetworkX ด้วย Matplotlib

    การปรับตำแหน่ง Edge Labels ใน NetworkX Graph ใช้ label_pos parameter ใน draw_networkx_edge_labels() ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง DiGraph และเพิ่ม nodes, edges ตั้งค่า weight attributes คำนวณ layout ด้วย spring_layout วาด graph ด้วย nx.draw() วาด edge labels ด้วย nx.draw_n

  16. วิธีพล็อต Contourf และ Log Color Scale ใน Matplotlib

    Contour Plot พร้อม Logarithmic Color Scale ใช้ contourf() พร้อม ticker.LogLocator() และ colormap ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding กำหนด N และสร้าง meshgrid X, Y คำนวณ Z = Z1 + 50*Z2 (หลาย scales) Mask ค่าที่ <= 0 (log ไม่รับค่า <= 0) สร้าง figure และ axes พล็อต contourf() พร้อม loc

  17. วิธีดูสีของ Figure ล่าสุดใน Matplotlib

    การดึงสีของเส้นที่พล็อตล่าสุด ใช้ get_color() method บน Line2D objects ที่คืนจาก plot() ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้างข้อมูล x, y ด้วย numpy พล็อตหลายเส้นพร้อมกัน plt.plot(x, y, x, y**2, x, y**3) ตั้งค่า legend ดึงสีแต่ละเส้นด้วย p[0].get_color(), p[1].get_color(), p[2].get_c

  18. วิธีบันทึกรูปเป็น PDF แบบ Raster Images ใน Matplotlib

    การบันทึก Figure เป็น PDF แบบ Rasterized ใช้ rasterized=True ใน add_subplot() แล้ว savefig PDF ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding สร้าง figure และ axes ด้วย rasterized=True สร้างข้อมูลสุ่มและแสดงด้วย imshow() บันทึกด้วย plt.savefig(rasterized.pdf) ตัวอย่างโค้ด import numpy as np import

  19. วิธีซ่อน Output ของ Matplotlib ใน Python / Jupyter Notebook

    การปิดการแสดง Output Object ใน Jupyter ใช้ semicolon (;) หรือกำหนดให้ตัวแปร _ เพื่อไม่ให้แสดง representation ของ artists วิธีการ import numpy as np, matplotlib.pyplot as plt สร้างข้อมูล x พล็อตปกติ: plt.plot(x) จะแสดง output เช่น Out[4]: [] ใช้ semicolon: plt.plot(x); ไม่แสดง output หรือก

  20. การพล็อต Scatter Points บน Polar Axis ใน Matplotlib

    Scatter Plot บน Polar Coordinates ใช้ projection=polar ใน add_subplot() แล้ว scatter(theta, r) ขั้นตอนหลัก ตั้งค่าขนาดรูปและ padding กำหนด N จำนวนจุด สร้าง r, theta, area, color ด้วย numpy random สร้าง figure และ polar axes พล็อต scatter(theta, r, c=colors, s=area, cmap=hsv, alpha=0.75)

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:305/451  20-คอมพิวเตอร์/Page Goto:1 299 300 301 302 303 304 305 306 307 308 309 310 311