匯入了必要的套件:seaborn、matplotlib.pyplot 和 matplotlib.font_manager。
設定了字體和字號:使用 FontProperties 類別來指定字體檔案路徑和字號大小。
載入了 tips 數據集:使用 sns.load_dataset 函數來載入 seaborn 的 tips 數據集。
設定了 seaborn 的樣式:使用 sns.set() 函數來設定 seaborn 的預設樣式。
畫了散點圖:使用 sns.scatterplot 函數來畫散點圖。
設定了圖表標題:使用 plt.title 函數來設定圖表標題,並指定了中文字體和字號。
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# 設定字體和字號
font = fm.FontProperties(fname='/Users/YOURNAME/Library/Fonts/jf-openhuninn-2.0.ttf', size=12)
tips = sns.load_dataset("tips")
sns.set()
sns.scatterplot(x="total_bill", y="tip", data=tips)
plt.title('餐廳小費關係圖', fontproperties=font)
plt.show()
沒有留言:
張貼留言