在这里我介绍一下plot方法的函数的使用。
Series.plot方法的函数:
参数 | 说明 |
---|---|
label | 用于图例的标签 |
ax | 要在其上进行绘制的matplotlib subplot对象。如果没有设置,则使用当前matplotlib subplot |
style | 将要传给matplotlib的风格字符串(for example: ‘ko–’) |
alpha | 图表的填充不透明(0-1) |
kind | 可以是’line’, ‘bar’, ‘barh’, ‘kde’ |
logy | 在Y轴上使用对数标尺 |
use_index | 将对象的索引用作刻度标签 |
rot | 旋转刻度标签(0-360) |
xticks | 用作X轴刻度的值 |
yticks | 用作Y轴刻度的值 |
xlim | X轴的界限 |
ylim | Y轴的界限 |
grid | 显示轴网格线 |
在这里强调一下 kind参数——'line', 'bar', 'barh', 'kde':
直方图:是一种可以对值频率离散化显示的柱状图。通过调用Series.hist()方法即可创建。
密度图:与直方图相关的一种类型图,是通过计算“可能会产生观测数据的连续概率分布的估计”而产生的,通过给plot传入参数kind = 'kde' 即可。
散布图:是观测两个一维数据序列之间关系的有效手段,使用pd.scatter_matrix()即可建立。
部分代码:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数
plt.subplot2grid((2,3),(0,0)) # 在一张大图里分列几个小图,位置是(0,0)
data_train.Survived.value_counts().plot(kind='bar')# plots a bar graph of those who surived vs those who did not.
plt.title(u"获救情况 (1为获救)") # puts a title on our graph
plt.ylabel(u"人数")
plt.subplot2grid((2,3),(0,1)) # 在一张大图(2 * 3)中的位置是(0,1)
data_train.Pclass.value_counts().plot(kind="bar")
plt.ylabel(u"人数")
plt.title(u"乘客等级分布")
plt.subplot2grid((2,3),(0,2)) # 在一张大图(2 * 3)中的位置是(0, 2)
plt.scatter(data_train.Survived, data_train.Age) # 找出存活人数和年龄的散点图
plt.ylabel(u"年龄") # sets the y axis lable
plt.grid(b=True, which='major', axis='y') # formats the grid line style of our graphs
plt.title(u"按年龄看获救分布 (1为获救)")
plt.subplot2grid((2,3),(1,0), colspan=2) # colspan = 2 表示横向跨度是 2
# plots a kernel desity estimate(核密度估计) of the subset of the 1st class passanges's age
data_train.Age[data_train.Pclass == 1].plot(kind='kde')
data_train.Age[data_train.Pclass == 2].plot(kind='kde')
data_train.Age[data_train.Pclass == 3].plot(kind='kde')
plt.xlabel(u"年龄")# plots an axis lable
plt.ylabel(u"密度")
plt.title(u"各等级的乘客年龄分布")
plt.legend((u'头等舱', u'2等舱',u'3等舱'),loc='best') # sets our legend for our graph. 显示图示
plt.subplot2grid((2,3),(1,2))
data_train.Embarked.value_counts().plot(kind='bar')
plt.title(u"各登船口岸上船人数")
plt.ylabel(u"人数")
plt.show()
显示的图形如下所示:
bingo,通过这个简单的示例,以后画图之类的都可以搞定了。