ขั้นตอนการทำงาน
- กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
- สร้าง figure และชุด subplots
- ตั้งค่า x และ y scale ของ axes
- ผูกฟังก์ชันกับ event
- ใช้เมธอด show() เพื่อแสดงรูปภาพ
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
def onkey(event):
if event.key.isalpha():
if event.xdata is not None and event.ydata is not None:
ax.plot(event.xdata, event.ydata, 'bo-')
fig.canvas.draw()
cid2 = fig.canvas.mpl_connect('key_press_event', onkey)
plt.show()
ผลลัพธ์
