import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 創建 3 個範例 DataFrame
df1 = pd.DataFrame({‘X’: [1, 2, 3], ‘Y’: [8, 10, 12]})
df2 = pd.DataFrame({‘X’: [1, 2], ‘Y’: [4, 5]})
df3 = pd.DataFrame({‘X’: [1, 2, 3, 4], ‘Y’: [14, 15, 16, 17]})
# 將每個 DataFrame 的行數填充到最大行數
max_rows = max(len(df) for df in [df1, df2, df3])
dfs = []
for df in [df1, df2, df3]:
missing_rows = max_rows – len(df)
df = pd.concat([df, pd.DataFrame(np.nan,
index=np.arange(missing_rows),
columns=df.columns)])
dfs.append(df.reset_index(drop=True)) # 重置 index
# 將所有 DataFrame 組合成一個大的 DataFrame
merged_df = pd.concat(dfs, axis=1, keys=[‘df1’, ‘df2’, ‘df3’])
# 繪製散點圖
ax = merged_df.plot(kind=’scatter’, x=(‘df1’, ‘X’), y=(‘df1’, ‘Y’), color=’red’)
merged_df.plot(kind=’scatter’, x=(‘df2’, ‘X’), y=(‘df2’, ‘Y’), color=’green’, ax=ax)
merged_df.plot(kind=’scatter’, x=(‘df3’, ‘X’), y=(‘df3’, ‘Y’), color=’blue’, ax=ax)
plt.show()

輸出結果:

推薦hahow線上學習python: https://igrape.net/30afN




![Python: 自定義函數計算計程車車資(先typing,再用預設值), 巢狀字典以及typing.Union[ ], assert 斷言 Python: 自定義函數計算計程車車資(先typing,再用預設值), 巢狀字典以及typing.Union[ ], assert 斷言](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220923222039_57.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python 表達式中的魔法:用海象運算子讓斷詞程式碼更乾淨 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS] Python 表達式中的魔法:用海象運算子讓斷詞程式碼更乾淨 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS]](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2026/02/20260210083748_0_a7d9bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言