可视化基本图形及用法 常见的可视化图:加载可视化包 :import matplotlib.pyplot as plt;
可视化显示在页码,jupyter内置函数,有些可能没有:%matplotlib inline;
更改设计风格:plt.style.use('ggplot')。
- 折线图 plot
- 柱形图 bar
- 条形图 barh
- 直方图 hist
- 箱线图 box
- 密度图 kde
- 面积图 area
- 散点图 scatter
- 散点矩阵图 scatter_matrix
- 饼图 pie
折线图 plot
#plot一般索引变成x轴,值是y轴,而value_counts()后是无序的,sort_index使索引升序 df.companyId.value_counts().sort_index().plot()
柱形图 bar
#柱形图绘制的两种方法:plot(kind='bar');plot.bar(),二者等价 df.companyId.value_counts().sort_index().plot(kind='bar') #堆积柱形图plot.bar(stacked=True) df.pivot_table(index='city',columns='education',values='companyId',aggfunc='count').plot.bar(stacked=True) #barh横向柱形图(条形图) df.pivot_table(index='city',columns='education',values='companyId',aggfunc='count').plot.barh(stacked=True)
直方图 hist
df.companyId.plot.hist()
#多重直方图unstack():是针对pandas的轴进行重新排列的一种方法
#具体用途:将数据的行index旋转成列columns
df.groupby('education').apply(lambda x:x.companyId).unstack().T.hist()
# hist(alpha=0.5)透明度
df.groupby('education').apply(lambda x:x.companyId).unstack().T.plot.hist(alpha=0.5,stacked=True,bins=8)
箱线图 box
#箱线图和多重直方图类似,需要多个维度数据框。有两种方法
#方法一
df.groupby('education').apply(lambda x:x.companyId).unstack().T.plot.box()
#方法二
df.boxplot(column='companyId',by='education')
密度图 kde
df.companyId.plot.kde()
面积图 area
#面积图(先将数据进行简单的分类) df.pivot_table(index='city',columns='education',aggfunc='count',values='companyId').plot.area()
散点图 scatter
#散点图 x y
df.groupby('city').aggregate(['max','mean','min']).companyId.plot.scatter(x = 'min', y = 'max')
#散点矩阵图
matrix=df.groupby('positionId').aggregate(['max','mean','count']).companyId
pd.plotting.scatter_matrix(matrix[matrix['max']>1000],diagonal='kde')
饼图 pie
#饼图 figsize 大小 df.city.value_counts().plot.pie(figsize=(6,6))可视化图的美化和丰富
import matplotlib.pyplot as plt import numpy as np
#figure整个画布
plt.figure(1,figsize=(10,5))
#随机生成的折线图
plt.plot(np.random.random_integers(-20,20,20))
#多个title时,显示最下边的
plt.title('这是一条折线图')
#x轴的刻度,y轴用yticks
plt.xticks([0,10,20])
#x轴的标签,y轴用ylabel
plt.xlabel('时间')
#去掉[]
plt.show()
#图例的两种展示方法 #方法一 plt.plot(np.random.random_integers(-20,20,20)) plt.plot(np.random.random_integers(-20,20,20)) plt.legend(['hh1','hh2']) plt.show() #方法二 plt.plot(np.random.random_integers(-20,20,20),label='no1') plt.plot(np.random.random_integers(-20,20,20),label='no2') plt.legend() plt.show()
散点图的叠加
data=df.groupby(['education','positionId']).aggregate(['mean','count']).companyId.reset_index()
data.head()
#得到几个散点图
for edu,grouped in data.groupby('education'):
x = grouped['mean']
y = grouped['count']
plt.scatter(x,y)
plt.show()
#得到一个叠加起来的散点图
#层层插入需要 循环
for edu,grouped in data.groupby('education'):
x = grouped['mean']
y = grouped['count']
plt.scatter(x,y,label=edu)
plt.legend()
plt.show()
绘制子图
#绘制子图(左右两个) plt.figure(figsize=(12,4)) #绘制第一张图 1,2,1表示1行2列的第1个图 plt.subplot(1,2,1) plt.plot(np.random.random_integers(-20,20,20),label='no1',color='g') #绘制第二张图 #1,2,2可合并为122 plt.subplot(122) plt.plot(np.random.random_integers(-20,20,20),label='no2') plt.plot(np.random.random_integers(-20,20,20),label='no3') plt.legend() plt.show()
#绘制子图(第一行2个,第二行1个) plt.figure(figsize=(12,4)) plt.subplot(221) plt.plot(np.random.random_integers(-20,20,20),label='no1') plt.plot(np.random.random_integers(-20,20,20),label='no3') plt.subplot(223) plt.plot(np.random.random_integers(-20,20,20),label='no2') plt.plot(np.random.random_integers(-20,20,20),label='no4') #第三张宽图绘制,看成2行1列的情况 plt.subplot(122) plt.plot(np.random.random_integers(-20,20,20),label='no5') plt.legend() plt.show()



