matplotlib plots plots with formulas in legend labels

a code

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. x = np.linspace(0,2*np.pi,500)
  4. y = np.sin(x)
  5. z = np.cos(x*x)
  6. plt.figure(figsize=(8,4))
  7. #标签前后加$将使用内嵌的LaTex引擎将其显示为公式
  8. plt.plot(x,y,label='$sin(x)$',color='red',linewidth=2)#红色,2个像素宽
  9. plt.plot(x,z,'b--',label='$cos(x^2)$')#蓝色,虚线
  10. plt.xlabel('Time(s)')
  11. plt.ylabel('Volt')
  12. plt.title('Sin and Cos figure using pyplot')
  13. plt.ylim(-1.2,1.2)
  14. plt.legend()#显示图示
  15. plt.show()
Second run results

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327044300&siteId=291194637