pandas可视化:各种图的简单使用

一、Matplotlib中几种图的名字

  1. 折线图:plot
  2. 柱形图:bar
  3. 直方图:hist
  4. 箱线图:box
  5. 密度图:kde
  6. 面积图:area
  7. 散点图:scatter
  8. 散点图矩阵:scatter_matrix
  9. 饼图:pie

二、折线图:plot

  平均值需要先排序后出出图

  df.avg.value_counts().sort_index().plot()

三、柱形图:bar

  可先做数据透视,然后生成柱形图

  df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot.bar()

  如果是要做堆叠柱形图,则可设置bar()的参数

  df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot.bar(stacked=True) 

  

  如果是要做成条形图,则可修改bar()方法为bar()

  df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot.barh()

四、直方图:hist

  df.avg.plot.hist()

  以“education”字段多维分析,对平均值绘制直方图,

  alpha:图形透明度;

  stacked:是否堆叠;

  bins:密度;

  df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.hist(alpha=0.5,stacked=True,bins=30)

五、箱线图:box

  用法一:与“直方图”类似

  df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.box()

  用法二:

  df.boxplot(column='avg',by='education')

六、密度图:kde

  df.avg.plot.kde()

七、面积图:area

  一般将数据进行分类(数据透视)

  df.pivot_table(index='avg',columns='education',values='positonId',aggfunc='count').plot.area()

 八、散点图:scatter

  按公司分类,以平均值为x轴,数量为y轴

  df.groupby('companyId').aggregate(['mean','count']).avg.plot.scatter(x='mean',y='count')

九、散点图矩阵:scatter_matrix(Pandas的函数)

  适用于两个以上的参数,两两组合

  matrix=df.groupby('companyId').aggregate(['mean','count',max]).avg

  pd.plotting.scatter_matrix(matrix.query('count<50'),diagonal='kde')

  查询条件:计数小于50

  diagonal:修改图的类型(kde:密度图)

  

十、饼图:pie

  df.city.value_counts().plot.pie(figsize=(6,6))

  figsize:图的长宽

猜你喜欢

转载自www.cnblogs.com/hankh/p/11525096.html