【附代码】python绘图集锦-组成(Composition)关系图
华夫饼图(Waffle Chart)
饼图(Pie Chart)
树状图(Treemap)
柱状图(Bar Chart
1.华夫饼图(Waffle Chart)
展示较大数据集中的各个组的组成。
! pip install pywaffle#安装依赖包
# Reference: https://stackoverflow.com/questions/41400136/how-to-do-waffle-charts-in-python-square-piechart
from pywaffle import Waffle
# Import
df_raw = pd.read_csv("./datasets/mpg_ggplot2.csv")
# Prepare Data
df = df_raw.groupby('class').size().reset_index(name='counts')
n_categories = df.shape[0]
colors = [plt.cm.Set1(i / float(n_categories)) for i in range(n_categories)]
# Draw Plot and Decorate
fig = plt.figure(FigureClass=Waffle,
plots={
'111': {
'values':
df['counts'],
'labels': [
"{0} ({1})".format(n[0], n[1])
for n in df[['class', 'counts']].itertuples()
],
'legend': {
'loc': 'upper left',
'bbox_to_anchor': (1.05, 1),
'fontsize': 12
},
'title': {
'label': 'Vehicles by Class',
'loc':