plotter_9.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. def renderPlot(workspace, graphVars, func, variables, tokens=None):
  2. """Renders plot for functions in 2D and 3D
  3. Maps points from the numpy arrays for variables in given equation on the 2D/3D plot figure
  4. Arguments:
  5. workspace {QtWidgets.QWidget} -- main layout
  6. graphVars {list} -- variables for plotting
  7. dim {int} -- dimenion of plot
  8. variables {list} -- variables in equation
  9. """
  10. if len(graphVars) == 2:
  11. X, Y = graphVars[0], graphVars[1]
  12. ax = workspace.figure2D.add_subplot(111)
  13. ax.clear()
  14. ax.contour(X, Y, func, [0])
  15. ax.grid()
  16. ax.set_xlabel(r'$' + variables[0] + '$')
  17. ax.set_ylabel(r'$' + variables[1] + '$')
  18. workspace.figure2D.set_tight_layout({"pad": 1}) # removes extra padding
  19. workspace.canvas2D.draw()
  20. workspace.tabPlot.setCurrentIndex(0)
  21. elif len(graphVars) == 3:
  22. xrange = graphVars[0]
  23. yrange = graphVars[1]
  24. zrange = graphVars[2]
  25. ax = Axes3D(workspace.figure3D)
  26. for z in zrange:
  27. X, Y = np.meshgrid(xrange, yrange)
  28. Z = func(X, Y, z)
  29. ax.contour(X, Y, Z + z, [z], zdir='z')
  30. for y in yrange:
  31. X, Z = np.meshgrid(xrange, zrange)
  32. Y = func(X, y, Z)
  33. ax.contour(X, Y + y, Z, [y], zdir='y')
  34. for x in xrange:
  35. Y, Z = np.meshgrid(yrange, zrange)
  36. X = func(x, Y, Z)
  37. ax.contour(X + x, Y, Z, [x], zdir='x')
  38. if tokens is None:
  39. axisRange = workspace.axisRange
  40. else:
  41. axisRange = [10, 10, 10, 30]
  42. xmin = -axisRange[0]
  43. xmax = axisRange[0]
  44. ymin = -axisRange[1]
  45. ymax = axisRange[1]
  46. zmin = -axisRange[2]
  47. zmax = axisRange[2]
  48. ax.set_xlim3d(xmin, xmax)
  49. ax.set_ylim3d(ymin, ymax)
  50. ax.set_zlim3d(zmin, zmax)
  51. ax.set_xlabel(r'$' + variables[0] + '$')
  52. ax.set_ylabel(r'$' + variables[1] + '$')
  53. ax.set_zlabel(r'$' + variables[2] + '$')
  54. workspace.canvas3D.draw()
  55. workspace.tabPlot.setCurrentIndex(1)