treemap5.py 842 B

123456789101112131415161718192021222324
  1. # import matplotlib.pyplot as plt
  2. # import squarify
  3. # sizes = [40, 50, 30, 2]
  4. # label = ["a", "b", "c", "d"]
  5. # color = ["red", "blue", "green", "grey"]
  6. # squarify.plot(sizes=sizes, label=label, color=color, alpha=0.6)
  7. # plt.axis('off')
  8. # plt.show()
  9. import matplotlib.pyplot as plt
  10. import seaborn as sns
  11. import squarify
  12. rawdata = sns.load_dataset('titanic')
  13. # print(rawdata.head())
  14. n = rawdata.groupby('class')[['survived']].sum()
  15. # print(n)
  16. a = rawdata.groupby('class')[['survived']].sum().index.get_level_values(0).tolist()#取得index裡的[0]再轉為list
  17. # print(a)
  18. d = rawdata.groupby('class')[['survived']].sum().reset_index().survived.values.tolist()#This gives us the labels in the form of a list. To get the values corresponding to these labels, use :
  19. # print(d)
  20. squarify.plot(sizes=d, label=a, alpha=.8)
  21. plt.axis('off')
  22. plt.show()