bar-facing.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Face-to-face bar charts example
  17. import numpy
  18. from taipy.gui import Gui
  19. if __name__ == "__main__":
  20. n_years = 10
  21. proportions_female = numpy.zeros(n_years)
  22. proportions_male = numpy.zeros(n_years)
  23. # Prepare the data set with random variations
  24. proportions_female[0] = 0.4
  25. proportions_male[0] = proportions_female[0] * (1 + numpy.random.normal(0, 0.1))
  26. for i in range(1, n_years):
  27. mean_i = (0.5 - proportions_female[i - 1]) / 5
  28. new_value = proportions_female[i - 1] + numpy.random.normal(mean_i, 0.1)
  29. new_value = min(max(0, new_value), 1)
  30. proportions_female[i] = new_value
  31. proportions_male[i] = proportions_female[i] * (1 + numpy.random.normal(0, 0.1))
  32. data = {
  33. "Hobbies": [
  34. "Archery",
  35. "Tennis",
  36. "Football",
  37. "Basket",
  38. "Volley",
  39. "Golf",
  40. "Video-Games",
  41. "Reading",
  42. "Singing",
  43. "Music",
  44. ],
  45. "Female": proportions_female,
  46. # Negate these values so they appear to the left side
  47. "Male": -proportions_male,
  48. }
  49. properties = {
  50. # Shared y values
  51. "y": "Hobbies",
  52. # Bars for the female data set
  53. "x[1]": "Female",
  54. "color[1]": "#c26391",
  55. # Bars for the male data set
  56. "x[2]": "Male",
  57. "color[2]": "#5c91de",
  58. # Both data sets are represented with an horizontal orientation
  59. "orientation": "h",
  60. #
  61. "layout": {
  62. # This makes left and right bars aligned on the same y value
  63. "barmode": "overlay",
  64. # Set a relevant title for the x axis
  65. "xaxis": {"title": "Gender"},
  66. # Hide the legend
  67. "showlegend": False,
  68. "margin": {"l": 100},
  69. },
  70. }
  71. page = """
  72. # Bar - Facing
  73. <|{data}|chart|type=bar|properties={properties}|>
  74. """
  75. Gui(page).run()