华夫饼图
华夫饼图(waffle chart)即块状图(tile matrix chart)
华夫饼图是展示总数据的组类别情况的一种有效图表;
华夫饼图的小方格用不同颜色表示不同类别;
适用快速检视数据集中不同类别的分布和比例;
并与其他数据集的分布和比例进行比较,更容易找出其中的规律;
分类:
侧重展示类别数值的堆积型块状华夫饼图;
侧重展示类别占比的百分百华夫饼图;
点状华夫饼图(dot waffte chart)
以点为单位显示离散数据;
每种颜色的点表示一个特定类别,并以矩阵形式组合在一起;
适合用来快速检视数据集中不同类别的分布和比例;
与其它数据集分布和比例比较,更容易找出其中模式;
当只有一个变量、类别时(所有点都是相同颜色),点状华夫饼图就相当于比例面积图;
绘制百分比堆积型华夫饼图
百分比堆积型的块状和点状华夫饼图,使用geomtile()函数和geompoint()函数绘制;
需要对数据进行预处理;
先计算数据的百分比;
再转换到10×10矩阵中;
绘制堆积型华夫饼图
先设定最小元数值;
将数据按最小单元值转换到相应的矩阵中;
再使用geomtile()函数和geompoint()函数绘制块状或点状华夫饼图;
百分比堆积型-块状
import pandas as pd
import numpy as np
from plotnine import *
from plotnine.data import mpg
#百分比堆积型.
nrows=10
categ_table=(np.round(pd.value_counts(mpg['class'] ) * ((nrows*nrows)/(len(mpg['class']))),0)).astype(int)
sort_table=categ_table.sort_values(ascending=False)
a = np.arange(1,nrows+1,1)
b = np.arange(1,nrows+1,1)
X,Y=np.meshgrid(a,b)
df_grid =pd.DataFrame({'x':X.flatten(),'y':Y.flatten()})
df_grid['category']=pd.Categorical(np.repeat(sort_table.index,sort_table[:]),
categories=sort_table.index, ordered=False)
#块状
base_plot=(ggplot(df_grid, aes(x = 'x', y = 'y', fill = 'category')) +
geom_tile(color = "white", size = 0.25) +
coord_fixed(ratio = 1)+
scale_fill_brewer(type='qual',palette="Set2")+
theme_void()+
theme(panel_background = element_blank(),
legend_position = "right",
aspect_ratio =1,
figure_size = (5, 5),
dpi = 100))
print(base_plot)
百分比堆积型--点状
import pandas as pd
import numpy as np
from plotnine import *
from plotnine.data import mpg
#百分比堆积型
nrows=10
categ_table=(np.round(pd.value_counts(mpg['class'] ) * ((nrows*nrows)/(len(mpg['class']))),0)).astype(int)
sort_table=categ_table.sort_values(ascending=False)
a = np.arange(1,nrows+1,1)
b = np.arange(1,nrows+1,1)
X,Y=np.meshgrid(a,b)
df_grid =pd.DataFrame({'x':X.flatten(),'y':Y.flatten()})
df_grid['category']=pd.Categorical(np.repeat(sort_table.index,sort_table[:]),
categories=sort_table.index, ordered=False)
#点状
base_plot1=(ggplot(df_grid, aes(x = 'x', y = 'y', fill = 'category')) +
#geom_tile(color = "white", size = 0.25) +
geom_point(color = "black",shape='o',size=13) +
coord_fixed(ratio = 1)+
scale_fill_brewer(type='qual',palette="Set2")+
theme(#panel.border = element_rect(fill=NA,size = 2),
panel_background = element_blank(),
legend_position = "right",
aspect_ratio =1,
figure_size = (5, 5),
dpi = 100
)
)
print(base_plot1)
堆积型-块状
import pandas as pd
import numpy as np
from plotnine import *
from plotnine.data import mpg
#堆积型
categ_table=(np.round(pd.value_counts(mpg['class'] ),0)).astype(int)
sort_table=categ_table.sort_values(ascending=False)
ndeep= 10
a = np.arange(1,ndeep+1,1)
b = np.arange(1,np.ceil(sort_table.sum()/ndeep)+1,1)
X,Y=np.meshgrid(a,b)
df_grid =pd.DataFrame({'x':X.flatten(),'y':Y.flatten()})
category=np.repeat(sort_table.index,sort_table[:])
df_grid=df_grid.loc[np.arange(0,len(category)),:]
df_grid['category']=pd.Categorical(category,
categories=sort_table.index,
ordered=False)
#块状
base_plot2=(ggplot(df_grid, aes(x = 'y', y = 'x', fill = 'category')) +
geom_tile(color = "white", size = 0.25) +
coord_fixed(ratio = 1)+
xlab("1 square = 100")+
ylab("")+
scale_fill_brewer(type='qual',palette="Set2")+
theme(panel_background = element_blank(),
legend_position = "right",
figure_size = (7, 7),
dpi = 100))
print(base_plot2)
堆积型--点状
import pandas as pd
import numpy as np
from plotnine import *
from plotnine.data import mpg
#堆积型
categ_table=(np.round(pd.value_counts(mpg['class'] ),0)).astype(int)
sort_table=categ_table.sort_values(ascending=False)
ndeep= 10
a = np.arange(1,ndeep+1,1)
b = np.arange(1,np.ceil(sort_table.sum()/ndeep)+1,1)
X,Y=np.meshgrid(a,b)
df_grid =pd.DataFrame({'x':X.flatten(),'y':Y.flatten()})
category=np.repeat(sort_table.index,sort_table[:])
df_grid=df_grid.loc[np.arange(0,len(category)),:]
df_grid['category']=pd.Categorical(category,
categories=sort_table.index,
ordered=False)
#点状
base_plot3=(ggplot(df_grid, aes(x = 'y', y = 'x', fill = 'category')) +
geom_point(color = "black",shape='o',size=7) +
coord_fixed(ratio = 1)+
xlab("1 square = 100")+
ylab("")+
scale_fill_brewer(type='qual',palette="Set2")+
theme(panel_background = element_blank(),
legend_position = "right",
figure_size = (7, 7),
dpi = 100))
print(base_plot3)