散点图自定义样式

演示散点图与不同的标记颜色和大小。

散点图自定义样式图示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cbook as cbook
  4. # Load a numpy record array from yahoo csv data with fields date, open, close,
  5. # volume, adj_close from the mpl-data/example directory. The record array
  6. # stores the date as an np.datetime64 with a day unit ('D') in the date column.
  7. with cbook.get_sample_data('goog.npz') as datafile:
  8. price_data = np.load(datafile)['price_data'].view(np.recarray)
  9. price_data = price_data[-250:] # get the most recent 250 trading days
  10. delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1]
  11. # Marker size in units of points^2
  12. volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
  13. close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]
  14. fig, ax = plt.subplots()
  15. ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
  16. ax.set_xlabel(r'$\Delta_i$', fontsize=15)
  17. ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
  18. ax.set_title('Volume and percent change')
  19. ax.grid(True)
  20. fig.tight_layout()
  21. plt.show()

下载这个示例