matplotlib 包含均值和单个值的嵌套分类图

ddhy6vgd  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(131)

我试图找到一种方法来绘制平均值和单个值(不是条形图或箱线图),但使用嵌套的x轴。

  1. {'Product': {0: 'Apple',
  2. 1: 'Apple',
  3. 2: 'Apple',
  4. 3: 'Apple',
  5. 4: 'Apple',
  6. 5: 'Apple',
  7. 6: 'Apple',
  8. 7: 'Apple',
  9. 8: 'Apple',
  10. 9: 'Apple',
  11. 10: 'Apple',
  12. 11: 'Apple',
  13. 12: 'Apple',
  14. 13: 'Apple',
  15. 14: 'Apple',
  16. 15: 'Orange',
  17. 16: 'Orange',
  18. 17: 'Orange',
  19. 18: 'Orange',
  20. 19: 'Orange',
  21. 20: 'Orange',
  22. 21: 'Orange',
  23. 22: 'Orange',
  24. 23: 'Orange',
  25. 24: 'Orange',
  26. 25: 'Orange',
  27. 26: 'Orange',
  28. 27: 'Orange',
  29. 28: 'Orange',
  30. 29: 'Orange',
  31. 30: 'Banana',
  32. 31: 'Banana',
  33. 32: 'Banana',
  34. 33: 'Banana',
  35. 34: 'Banana',
  36. 35: 'Banana',
  37. 36: 'Banana',
  38. 37: 'Banana',
  39. 38: 'Banana',
  40. 39: 'Banana',
  41. 40: 'Banana',
  42. 41: 'Banana',
  43. 42: 'Banana',
  44. 43: 'Banana',
  45. 44: 'Banana'},
  46. 'Tester': {0: 'Anne',
  47. 1: 'Anne',
  48. 2: 'Anne',
  49. 3: 'Anne',
  50. 4: 'Anne',
  51. 5: 'Steve',
  52. 6: 'Steve',
  53. 7: 'Steve',
  54. 8: 'Steve',
  55. 9: 'Steve',
  56. 10: 'Paula',
  57. 11: 'Paula',
  58. 12: 'Paula',
  59. 13: 'Paula',
  60. 14: 'Paula',
  61. 15: 'Anne',
  62. 16: 'Anne',
  63. 17: 'Anne',
  64. 18: 'Anne',
  65. 19: 'Anne',
  66. 20: 'Steve',
  67. 21: 'Steve',
  68. 22: 'Steve',
  69. 23: 'Steve',
  70. 24: 'Steve',
  71. 25: 'Paula',
  72. 26: 'Paula',
  73. 27: 'Paula',
  74. 28: 'Paula',
  75. 29: 'Paula',
  76. 30: 'Anne',
  77. 31: 'Anne',
  78. 32: 'Anne',
  79. 33: 'Anne',
  80. 34: 'Anne',
  81. 35: 'Steve',
  82. 36: 'Steve',
  83. 37: 'Steve',
  84. 38: 'Steve',
  85. 39: 'Steve',
  86. 40: 'Paula',
  87. 41: 'Paula',
  88. 42: 'Paula',
  89. 43: 'Paula',
  90. 44: 'Paula'},
  91. 'Result': {0: 5,
  92. 1: 7,
  93. 2: 4,
  94. 3: 9,
  95. 4: 10,
  96. 5: 3,
  97. 6: 6,
  98. 7: 1,
  99. 8: 9,
  100. 9: 11,
  101. 10: 2,
  102. 11: 3,
  103. 12: 5,
  104. 13: 3,
  105. 14: 2,
  106. 15: 7,
  107. 16: 8,
  108. 17: 7,
  109. 18: 6,
  110. 19: 5,
  111. 20: 9,
  112. 21: 8,
  113. 22: 9,
  114. 23: 6,
  115. 24: 7,
  116. 25: 3,
  117. 26: 7,
  118. 27: 9,
  119. 28: 7,
  120. 29: 1,
  121. 30: 11,
  122. 31: 12,
  123. 32: 11,
  124. 33: 10,
  125. 34: 9,
  126. 35: 12,
  127. 36: 12,
  128. 37: 14,
  129. 38: 8,
  130. 39: 6,
  131. 40: 7,
  132. 41: 4,
  133. 42: 5,
  134. 43: 7,
  135. 44: 8}}

我希望能够在Minitab中重现以下图表:

我试过在各种软件包中查找(seaborn是首选,其次是matplotlib和hvplot),但据我所知,他们不能绘制多个或分层x轴,除非是箱线图或条形图。
我尝试过将参数col设置为'product'sns.catplot,但这会分割图,我希望它都在一个图表中。

qqrboqgw

qqrboqgw1#

更新

您可以使用scatterplot

  1. import seaborn as sns
  2. import matplotlib.pyplot as plt
  3. sns.set_style('whitegrid')
  4. fig, ax = plt.subplots(figsize=(12, 8))
  5. df['Label'] = df['Tester'] + '\n' + df['Product']
  6. sns.scatterplot(data=df, x='Label', y='Result', color='gray', ax=ax)
  7. dfm = df.groupby(['Label'], as_index=False)['Result'].mean()
  8. sns.scatterplot(data=dfm, x='Label', y='Result', color='blue', s=50, ax=ax)
  9. ax.set_title('Individual Value Plot of Result')
  10. ax.set_xlabel(None)

输出:

旧答案

您可以使用catplotswarmplot

  1. sns.set_style('whitegrid')
  2. sns.catplot(data=df, x='Product', y='Result', hue='Tester')

输出:

更新:黑客攻击

  1. sns.catplot(data=df.assign(Label=df['Tester'] + '\n' + df['Product']),
  2. x='Label', y='Result')

输出:

展开查看全部
rnmwe5a2

rnmwe5a22#

如果你不想让它看起来像是有独立的情节(IMO,如果是这样的话,它会使情节更容易解析),你可以删除子情节之间的空间

  1. tester_order = ["Anne", "Steve", "Paula"]
  2. g = sns.catplot(
  3. data=df, kind="swarm",
  4. x="Tester", y="Result", col="Product",
  5. color=".6", size=6, zorder=1,
  6. height=4, aspect=.5, order=tester_order,
  7. )
  8. g.map_dataframe(
  9. sns.pointplot, x="Tester", y="Result",
  10. join=False, errorbar=None,
  11. order=tester_order,
  12. )
  13. g.figure.subplots_adjust(wspace=0)

展开查看全部

相关问题