Anscombe的四重奏

Anscombe的四重奏示例

输出:

  1. mean=7.50, std=1.94, r=0.82
  2. mean=7.50, std=1.94, r=0.82
  3. mean=7.50, std=1.94, r=0.82
  4. mean=7.50, std=1.94, r=0.82
  1. """
  2. Edward Tufte uses this example from Anscombe to show 4 datasets of x
  3. and y that have the same mean, standard deviation, and regression
  4. line, but which are qualitatively different.
  5. matplotlib fun for a rainy day
  6. """
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
  10. y1 = np.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68])
  11. y2 = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74])
  12. y3 = np.array([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73])
  13. x4 = np.array([8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8])
  14. y4 = np.array([6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89])
  15. def fit(x):
  16. return 3 + 0.5 * x
  17. xfit = np.array([np.min(x), np.max(x)])
  18. plt.subplot(221)
  19. plt.plot(x, y1, 'ks', xfit, fit(xfit), 'r-', lw=2)
  20. plt.axis([2, 20, 2, 14])
  21. plt.setp(plt.gca(), xticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20))
  22. plt.text(3, 12, 'I', fontsize=20)
  23. plt.subplot(222)
  24. plt.plot(x, y2, 'ks', xfit, fit(xfit), 'r-', lw=2)
  25. plt.axis([2, 20, 2, 14])
  26. plt.setp(plt.gca(), xticks=(0, 10, 20), xticklabels=[],
  27. yticks=(4, 8, 12), yticklabels=[], )
  28. plt.text(3, 12, 'II', fontsize=20)
  29. plt.subplot(223)
  30. plt.plot(x, y3, 'ks', xfit, fit(xfit), 'r-', lw=2)
  31. plt.axis([2, 20, 2, 14])
  32. plt.text(3, 12, 'III', fontsize=20)
  33. plt.setp(plt.gca(), yticks=(4, 8, 12), xticks=(0, 10, 20))
  34. plt.subplot(224)
  35. xfit = np.array([np.min(x4), np.max(x4)])
  36. plt.plot(x4, y4, 'ks', xfit, fit(xfit), 'r-', lw=2)
  37. plt.axis([2, 20, 2, 14])
  38. plt.setp(plt.gca(), yticklabels=[], yticks=(4, 8, 12), xticks=(0, 10, 20))
  39. plt.text(3, 12, 'IV', fontsize=20)
  40. # verify the stats
  41. pairs = (x, y1), (x, y2), (x, y3), (x4, y4)
  42. for x, y in pairs:
  43. print('mean=%1.2f, std=%1.2f, r=%1.2f' % (np.mean(y), np.std(y),
  44. np.corrcoef(x, y)[0][1]))
  45. plt.show()

下载这个示例