fork download
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4.  
  5. def func1(x):
  6. return math.cos(x) - 3*math.sin(x)
  7.  
  8. def func2(x):
  9. if x>= math.pi and x<= 3*math.pi/2:
  10. tmp = math.cos(x) - 3*math.sin(x)
  11.  
  12. if tmp < 0:
  13. return 0.5
  14. else:
  15. return tmp
  16.  
  17. data_x1 = [i for i in np.arange(0, math.pi, (2*math.pi/36))]
  18. data_y1 = [func1(x) for x in data_x1]
  19. data_x2 = [i for i in np.arange(math.pi, 3*math.pi/2 + 2*math.pi/36, (2*math.pi/36))]
  20. data_y2 = [func2(x) for x in data_x2]
  21. data_x3 = [i for i in np.arange(3*math.pi/2 + 2*math.pi/36, 2*math.pi, (2*math.pi/36))]
  22. data_y3 = [func1(x) for x in data_x3]
  23.  
  24. plt.title('График cos(x) - 3*sin(x)')
  25. plt.xlabel('x')
  26. plt.ylabel('y')
  27.  
  28. plt.plot(data_x1, data_y1, 'r')
  29. plt.plot(data_x2, data_y2, 'r')
  30. plt.plot(data_x3, data_y3, 'r')
  31.  
  32. plt.grid(True, linestyle='-', color='black')
  33.  
  34. data_x1.extend(data_x2)
  35. data_x1.extend(data_x3)
  36. data_y1.extend(data_y2)
  37. data_y1.extend(data_y3)
  38. plt.show()
Runtime error #stdin #stdout #stderr 0.02s 63376KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named matplotlib