注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Matplotlib (Stateful vs stateless(OOP) approach)
. Χ
1.Stateful approach 线图点图:
. 1point 3 acres
plt.plot(x=,y=, color=, marker=,linestyle=,linewidth,markersize=)
bar chart: plt.bar(x, y, align='center', alpha=0.5)
优点:简单快捷 缺点:缺乏灵活性
2.Stateless(OOP) approach 作图的元素: . From 1point 3acres bbs
. Waral dи,
- fig, ax = plt.subplots(nrow,ncol,figsize=(5, 3))
fig: 给我一张画布; ax:画布上要有几个图
- 将ax看成object, 添加plot,legend, set_xlabel, set_ylabel
Seaborn. 1point3acres
基于Matplotlib的高级作图工具,特别适合做统计分析。 基本思路:
- 用Seaborn作图
- 用Matplotlib微调
- (Optional) Theme
以下是在Google Colab 的 code:
(# 去掉将实现各行的code)
先导入所需的libraries
|--------------------------------------------------------
| import pandas as pd, numpy as np |
| import matplotlib as mpl | ..
| import matplotlib.pyplot as plt |
| mpl.rcParams.update(mpl.rcParamsDefault) |
| import seaborn as sns |
--------------------------------------------------------|
所需data还得导入, 不了解的同学可以看 数据 导入 Google Colab 的方法 https://www.1point3acres.com/bbs/thread-681995-1-1.html
----------------------------------------------------------------------------------------------------------------------------------------------------.--
fig=plt.figure(figsize=(8,10)) # 新建一张画布(设定尺寸)
#sns.set_style('whitegrid') #设定风格
..
ax0=fig.add_subplot(211) # 添加一个图框
#sns.barplot(x="year", y="population", data=d6)
#sns.barplot(x="year", y="population", data=d3)
ax0.bar(CN.year,CN.population/10**6,width=3)
ax0.bar(US.year,US.population/10**6,width=3) #制作柱状图 (x轴, y轴, 宽度)其它图形可以Google matplotlib & seaborn 跟多了解
ax0.bar(UK.year,UK.population/10**6,width=3)
#plt.bar(UK.year,UK.population/10**6,width=3,color = (0.3,0.9,0.4,0.6), label='UK')
ax0.set_title("Population Growth",fontsize=20) #设定画框名字(字体大小)
#plt.title("Population Growth")
ax0.set_xlabel('year') # 设定x轴名字-baidu 1point3acres
plt.ylabel('population in million', size=15) # 设定y轴名字
#ax0.xaxis.label.set_size(15) # 设定x轴名字字体大小
#ax0.yaxis.label.set_size(15) # 设定y轴名字字体大小. 1point 3acres
ax0.legend(['China','United States', 'United Kingdom']) # 命名在画框中图名
. 1point3acres.com #ax0.set_ylim(top=1500) # 设定y轴最大刻度值
ax0.set_xticks(np.arange(min(CN.year),max(CN.year)+1, 5)) # 调整刻度. From 1point 3acres bbs
# or e.g. ax0.set_yticks([0.2, 0.55, 0.76]). 1point 3 acres
#plt.xticks(rotation=45)
#plt.axhline(y=300,color='purple',linestyle='--') # 可以放其他数值,比如平均数,中位数之类的。
.--
.google и
ax0.grid(False) # 画框背景刻度虚线(True/False)False:隐藏. 1point3acres
#ax0.grid(color='gray', alpha=0.5, linestyle='dotted') # 调整刻度虚线
#plt.subplot(2,1,2)
#plt.subplots_adjust(bottom= 0.2, top = 0.98) # Adjust the margins
. 1point3acres.com
sns.despine(ax=ax0, right=True, top=True) # 增减边框线
plt.show() ..
#plt.savefig()
==============================================================
以上就是Python作图的基本所需, 跟多了解Google matplotlib & seaborn.
|