error-bars-simple.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. import math
  17. import random
  18. from taipy.gui import Gui
  19. if __name__ == "__main__":
  20. # Number of samples
  21. max_x = 20
  22. # x values: [0..max_x-1]
  23. x = range(0, max_x)
  24. # Generate random sampling error margins
  25. error_ranges = [random.uniform(0, 5) for _ in x]
  26. # Compute a perfect sine wave
  27. perfect_y = [10 * math.sin(4 * math.pi * i / max_x) for i in x]
  28. # Compute a sine wave impacted by the sampling error
  29. # The error is between ±error_ranges[x]/2
  30. y = [perfect_y[i] + random.uniform(-error_ranges[i] / 2, error_ranges[i] / 2) for i in x]
  31. # The chart data is made of the three series
  32. data = {
  33. "x": x,
  34. "y1": y,
  35. "y2": perfect_y,
  36. }
  37. options = {
  38. # Create the error bar information:
  39. "error_y": {"type": "data", "array": error_ranges}
  40. }
  41. page = """
  42. # Error bars - Simple
  43. <|{data}|chart|x=x|y[1]=y1|y[2]=y2|options[1]={options}|>
  44. """
  45. Gui(page).run()