Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การทำเครื่องหมายจุดข้อมูลจาก Pandas DataFrame ใน Matplotlib


เพื่อทำเครื่องหมายจุดข้อมูลจาก Pandas DataFrame ใน Matplotlib สามารถทำตามขั้นตอน:

  • ตั้งค่าขนาดรูปและปรับ padding
  • สร้างข้อมูลสองมิติ (DataFrame) มีคอลัมน์ x, y และ textc
  • พลอทจุดข้อมูลคอลัมน์ x และ y ด้วย plot()
  • รวมอ็อบเจ็กต์ Pandas ตามแกนที่กำหนดด้วย concat()
  • วนลูปอ็อบเจ็กต์ Pandas
  • วางข้อความสำหรับแต่ละจุดที่พลอทด้วย text()
  • แสดงรูปด้วย show()

ตัวอย่าง

import pandas as pd 
import matplotlib.pyplot as plt 
plt.rcParams["figure.figsize"] = [7.50, 3.50] 
plt.rcParams["figure.autolayout"] = True 
df = pd.DataFrame(dict(x=[2, 4, 1, 5, 2], y=[2, 1, 3, 5, 7], text=['First', 'Second', 'Third', 'Fourth', 'Fifth'])) 
ax = df.set_index('x')['y'].plot(style='*', color='red', ms=20) 
a = pd.concat({'x': df.x, 'y': df.y, 'text': df.text}, axis=1) 
for i, point in a.iterrows(): 
    ax.text(point['x']+0.125, point['y'], str(point['text'])) 
plt.show()

ผลลัพธ์

การทำเครื่องหมายจุดข้อมูลจาก Pandas DataFrame ใน Matplotlib