plot_fxy0.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import numpy as np
  2. import vedo
  3. def update_plot(widget=None, evt=""):
  4. k = widget.value if evt else kinit
  5. #################################################
  6. y = 2 * k * x / (1 + (k * x)**2) # hill function
  7. # y = (k*x)**2 * np.sign(k*x) # another function
  8. # y = 2 * (k * x)**3 / (1 + (k * x)**2)
  9. #################################################
  10. # use jet colormap to color the line
  11. # color = "blue4" if k>0 else "red5"
  12. color = vedo.color_map(k, name="winter_r", vmin=x[0], vmax=x[-1])
  13. title = (f"y-mean : {np.mean(y):.3f}, std: {np.std(y):.3f},\n"
  14. f"y-range: [{np.min(y):.3f}, {np.max(y):.3f}], "
  15. f"integral: {np.trapezoid(y, x):.3f}")
  16. p = vedo.pyplot.plot(
  17. x, y,
  18. xlim=xrange,
  19. ylim=yrange,
  20. c=color, # line color
  21. lw=3, # line width
  22. aspect=1.0, # aspect ratio of the plot
  23. axes={"number_of_divisions":10, "htitle":title, "htitle_size":0.015},
  24. )
  25. plt.remove("PlotXY").add(p)
  26. if yrange[0] is None or yrange[1] is None:
  27. plt.reset_camera(tight=0.05)
  28. #####################################################################
  29. kinit = 0.1 # initial value of the parameter
  30. xrange = (-1, 1) # set a fixed x axis range
  31. yrange = (-1, 1) # set a fixed y axis range
  32. # yrange = (None, None) # uncomment to autoscale y axis
  33. vedo.settings.default_font = "Roboto"
  34. x = np.linspace(*xrange, 200, endpoint=True)
  35. plt = vedo.Plotter(size=(900, 1100))
  36. plt.add_slider(
  37. update_plot,
  38. x[0], x[-1],
  39. value=kinit,
  40. title="parameter value",
  41. pos=5,
  42. )
  43. update_plot() # update initial plot
  44. plt.show("1D Function Viewer\nMove the slider to modify the parameter",
  45. mode="2d", zoom="tight").close()