透明、花式的图形

有时您在绘制数据之前就知道数据的样子,并且可能知道例如右上角没有太多数据。然后,您可以安全地创建不覆盖数据的图例:

  1. ax.legend(loc='upper right')

其他时候你不知道你的数据在哪里,默认的loc =’best’会尝试放置图例:

  1. ax.legend()

但是,您的图例可能会与您的数据重叠,在这些情况下,使图例框架透明是很好的。

透明、花式的图形示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. np.random.seed(1234)
  4. fig, ax = plt.subplots(1)
  5. ax.plot(np.random.randn(300), 'o-', label='normal distribution')
  6. ax.plot(np.random.rand(300), 's-', label='uniform distribution')
  7. ax.set_ylim(-3, 3)
  8. ax.legend(fancybox=True, framealpha=0.5)
  9. ax.set_title('fancy, transparent legends')
  10. plt.show()

下载这个示例