1. PyWaffle

数据可视化 -- 华夫饼图 - 图1

  1. 图片来源:https://appsource.microsoft.com

TODO: 做一个与此相近的图作为练习

2. 擅长表达类型

华夫饼图(Waffle Chart),有的人也会叫它“Square Pie Chart”,是饼图的一种变形,擅长展示部分在整体中的占比关系。一般来说,华夫饼图是由100个格子组成,一个格子代表“1%”。用不同颜色的格子区分不同的分类数据,以展示各部分在整体中的占比。

3. 代码实例

  1. #! pip install pywaffle
  2. # Reference: https://stackoverflow.com/questions/41400136/how-to-do-waffle-charts-in-python-square-piechart
  3. from pywaffle import Waffle
  4. # Import
  5. df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
  6. # Prepare Data
  7. df = df_raw.groupby('class').size().reset_index(name='counts')
  8. n_categories = df.shape[0]
  9. colors = [plt.cm.inferno_r(i/float(n_categories)) for i in range(n_categories)]
  10. # Draw Plot and Decorate
  11. fig = plt.figure(
  12. FigureClass=Waffle,
  13. plots={
  14. '111': {
  15. 'values': df['counts'],
  16. 'labels': ["{0} ({1})".format(n[0], n[1]) for n in df[['class', 'counts']].itertuples()],
  17. 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12},
  18. 'title': {'label': '# Vehicles by Class', 'loc': 'center', 'fontsize':18}
  19. },
  20. },
  21. rows=7,
  22. colors=colors,
  23. figsize=(16, 9)
  24. )

image.png

4. 好资源

4.1 官方教程