bar_facing.py 2.6 KB

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