Pandas是基于Matplotlib,对Matplotlib二次封装。使用简单,功能稍弱,常用的数据结构Series和Dataframe都有plot()方法,用于绘图
1、单变量可视化
1.1、柱状图
import pandas as pd
reviews = pd.read_csv("../data/winemag-data_first150k.csv", index_col=0)
text_kwargs=dict(figsize=(16,8),fontsize=20,color=['b','orange','g','r','purple','brown','pink','gray','cyan','y'])
reviews['province'].value_counts().head(10).plot.bar(**text_kwargs)

柱状图
1.2、折线图
在折线图的下方填充颜色即为面积图
reviews['points'].value_counts().sort_index().plot.line()
1.3、面积图
reviews['points'].value_counts().sort_index().plot.area()

面积图
1.4、直方图
reviews[reviews['price']<200]['price'].plot.hist()

直方图
1.5、饼图
只适合展示少量分类在整体的占比
reviews['province'].value_counts().head(10).plot.pie()

饼图
2、双变量可视化
2.1、散点图
reviews[reviews['price']<100].plot.scatter(x='price',y = 'points',figsize=(12,8))

散点图
2.2、hexplot
hexplot将数据点聚合为六边形,然后根据其内的值为这些六边形上色:
reviews[reviews['price']<100].plot.hexbin(x='price',y='points',gridsize=15,figsize=(14,8))

hexplot
2.3、堆叠图
种类较多的数据不适合用堆积图,图中显示的数据有五个种类,比较合适,一般不要超过8个种类
reviews.groupby(['variety'])['country'].count().sort_values(ascending=False)
reviews.groupby(['variety'])['country'].count().sort_values(ascending=False)
# 从数据中取出最常见的5中葡萄酒
top_5_wine = reviews[reviews['variety'].isin(['Chardonnay','Pinot Noir','Cabernet Sauvignon','Red Blend','Bordeaux-style Red Blend'])]
# 透视统计表
wine_counts = top_5_wine.pivot_table(index=['points'], columns=['variety'], values='country',aggfunc='count')
# 修改列名
wine_counts.columns=['Bordeaux-style Red Blend','Cabernet Sauvignon','Chardonnay','Pinot Noir','Red Blend']
wine_counts.plot.bar(stacked=True)

柱状堆叠图
wine_counts.plot.area()
面积堆叠图
2.4、折线图
wine_counts.plot.line()

折线图