Google Colab笔记

 

 

1.Google Colab挂载Google Drive并运行程序

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

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

 

2.利用kaggle的API讲数据集直接下载到Google Colab

登录kaggle在My Account中创建自己的Token:

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

!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/87621396

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

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

 

3.解决google colab环境下使用matplotlib绘图中文乱码问题

从网上下载一个支持中文的字体到系统字体目录:

!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"

引入下载的中文字体

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  # 用来正常显示负号

画坐标及坐标轴文本的时候引用

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()

效果如下:

参考资料:解决google colab环境下使用matplotlib绘图中文乱码问题

 

posted @ 2019-03-13 01:09  人生多风雨  阅读(1404)  评论(0编辑  收藏  举报