error_bars_simple.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2021-2025 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. # Number of samples
  20. max_x = 20
  21. # x values: [0..max_x-1]
  22. x = range(0, max_x)
  23. # Generate random sampling error margins
  24. error_ranges = [random.uniform(0, 5) for _ in x]
  25. # Compute a perfect sine wave
  26. perfect_y = [10 * math.sin(4 * math.pi * i / max_x) for i in x]
  27. # Compute a sine wave impacted by the sampling error
  28. # The error is between ±error_ranges[x]/2
  29. y = [perfect_y[i] + random.uniform(-error_ranges[i] / 2, error_ranges[i] / 2) for i in x]
  30. # The chart data is made of the three series
  31. data = {
  32. "x": x,
  33. "y1": y,
  34. "y2": perfect_y,
  35. }
  36. options = {
  37. # Create the error bar information:
  38. "error_y": {"type": "data", "array": error_ranges}
  39. }
  40. page = """
  41. <|{data}|chart|x=x|y[1]=y1|y[2]=y2|options[1]={options}|>
  42. """
  43. if __name__ == "__main__":
  44. Gui(page).run(title="Chart - Error bars - Simple")