散点图

散点图示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.axes_grid1 import make_axes_locatable
  4. # Fixing random state for reproducibility
  5. np.random.seed(19680801)
  6. # the random data
  7. x = np.random.randn(1000)
  8. y = np.random.randn(1000)
  9. fig, axScatter = plt.subplots(figsize=(5.5, 5.5))
  10. # the scatter plot:
  11. axScatter.scatter(x, y)
  12. axScatter.set_aspect(1.)
  13. # create new axes on the right and on the top of the current axes
  14. # The first argument of the new_vertical(new_horizontal) method is
  15. # the height (width) of the axes to be created in inches.
  16. divider = make_axes_locatable(axScatter)
  17. axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
  18. axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
  19. # make some labels invisible
  20. axHistx.xaxis.set_tick_params(labelbottom=False)
  21. axHisty.yaxis.set_tick_params(labelleft=False)
  22. # now determine nice limits by hand:
  23. binwidth = 0.25
  24. xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
  25. lim = (int(xymax/binwidth) + 1)*binwidth
  26. bins = np.arange(-lim, lim + binwidth, binwidth)
  27. axHistx.hist(x, bins=bins)
  28. axHisty.hist(y, bins=bins, orientation='horizontal')
  29. # the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
  30. # thus there is no need to manually adjust the xlim and ylim of these
  31. # axis.
  32. axHistx.set_yticks([0, 50, 100])
  33. axHisty.set_xticks([0, 50, 100])
  34. plt.show()

下载这个示例