光栅化演示

光栅化演示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. d = np.arange(100).reshape(10, 10)
  4. x, y = np.meshgrid(np.arange(11), np.arange(11))
  5. theta = 0.25*np.pi
  6. xx = x*np.cos(theta) - y*np.sin(theta)
  7. yy = x*np.sin(theta) + y*np.cos(theta)
  8. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
  9. ax1.set_aspect(1)
  10. ax1.pcolormesh(xx, yy, d)
  11. ax1.set_title("No Rasterization")
  12. ax2.set_aspect(1)
  13. ax2.set_title("Rasterization")
  14. m = ax2.pcolormesh(xx, yy, d)
  15. m.set_rasterized(True)
  16. ax3.set_aspect(1)
  17. ax3.pcolormesh(xx, yy, d)
  18. ax3.text(0.5, 0.5, "Text", alpha=0.2,
  19. va="center", ha="center", size=50, transform=ax3.transAxes)
  20. ax3.set_title("No Rasterization")
  21. ax4.set_aspect(1)
  22. m = ax4.pcolormesh(xx, yy, d)
  23. m.set_zorder(-20)
  24. ax4.text(0.5, 0.5, "Text", alpha=0.2,
  25. zorder=-15,
  26. va="center", ha="center", size=50, transform=ax4.transAxes)
  27. ax4.set_rasterization_zorder(-10)
  28. ax4.set_title("Rasterization z$<-10$")
  29. # ax2.title.set_rasterized(True) # should display a warning
  30. plt.savefig("test_rasterization.pdf", dpi=150)
  31. plt.savefig("test_rasterization.eps", dpi=150)
  32. if not plt.rcParams["text.usetex"]:
  33. plt.savefig("test_rasterization.svg", dpi=150)
  34. # svg backend currently ignores the dpi

下载这个示例