Google Colab筆記

 

 

1.Google Colab掛載Google Drive並運行程序

from google.colab import drive
drive.mount('/content/drive/')

https://blog.csdn.net/weixin_43792757/article/details/84523103git

 

2.利用kaggle的API講數據集直接下載到Google Colab

登陸kaggle在My Account中建立本身的Token:github

{"username":"abc","key":"123"}json

!pip install -U -q kaggle
!mkdir -p ~/.kaggle
!echo '{"username":"abc","key":"123"}' > ~/.kaggle/kaggle.json
!chmod 600 ~/.kaggle/kaggle.json
#須要注意的是,你必須在kaggle上參加相應的比賽才能下載對應的數據!!!
!kaggle competitions download -c digit-recognizer

https://blog.csdn.net/qq_35654046/article/details/87621396api

https://medium.com/@move37timm/using-kaggle-api-for-google-colaboratory-d18645f93648app

https://github.com/Kaggle/kaggle-api字體

 

3.解決google colab環境下使用matplotlib繪圖中文亂碼問題

從網上下載一個支持中文的字體到系統字體目錄:google

!wget -O /usr/share/fonts/truetype/liberation/simhei.ttf "http://file3.data.weipan.cn/87016829/e6a4de46faf0c7ba622132eaaad96a291d62f045?ip=1546784382,2409:8a20:203b:550:dbc:11e3:2ef9:cee9&ssig=OSXPXG8otr&Expires=1546784982&KID=sae,l30zoo1wmz&fn=simhei.ttf&se_ip_debug=2409:8a20:203b:550:dbc:11e3:2ef9:cee9&from=1221134"

引入下載的中文字體spa

import matplotlib.pyplot as plt
import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/liberation/simhei.ttf')
plt.rcParams['axes.unicode_minus'] = False  # 用來正常顯示負號

畫座標及座標軸文本的時候引用.net

df_Gender=df.groupby("Gender").Purchase.sum().reset_index()
df_Gender=df_Gender.apply(lambda x:x[1]/100000000,axis=1).reset_index(drop=True).reset_index().rename({0:"Purchase"},axis=1)
fig,ax=plt.subplots(figsize=(6,6))
rects=plt.bar(('F','M'),df_Gender.Purchase)
ax.set_xlabel("性別",fontproperties=zhfont)
ax.set_ylabel('億元',fontproperties=zhfont)
ax.set_ylim((0,45))
ax.set_title("不一樣性別的總消費額",fontproperties=zhfont)
for rect in rects:
    height = rect.get_height()
    plt.text(rect.get_x() + rect.get_width() / 2, height+0.2, '%.0f'%height, ha='center', va='bottom',fontsize=12,)
plt.show()

效果以下:debug

參考資料:解決google colab環境下使用matplotlib繪圖中文亂碼問題

相關文章
相關標籤/搜索