bar_stacked.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 pandas
  17. from taipy.gui import Gui
  18. # Source https://en.wikipedia.org/wiki/List_of_United_States_presidential_elections_by_popular_vote_margin
  19. percentages = [
  20. (1852, 50.83),
  21. (1856, 45.29),
  22. (1860, 39.65),
  23. (1864, 55.03),
  24. (1868, 52.66),
  25. (1872, 55.58),
  26. (1876, 47.92),
  27. (1880, 48.31),
  28. (1884, 48.85),
  29. (1888, 47.80),
  30. (1892, 46.02),
  31. (1896.0, 51.02),
  32. (1900, 51.64),
  33. (1904, 56.42),
  34. (1908, 51.57),
  35. (1912, 41.84),
  36. (1916, 49.24),
  37. (1920, 60.32),
  38. (1924, 54.04),
  39. (1928, 58.21),
  40. ]
  41. # Lost percentage = 100-Won percentage
  42. full = [(t[0], t[1], 100 - t[1]) for t in percentages]
  43. data = pandas.DataFrame(full, columns=["Year", "Won", "Lost"])
  44. layout = {"barmode": "stack"}
  45. page = """
  46. <|{data}|chart|type=bar|x=Year|y[1]=Won|y[2]=Lost|layout={layout}|>
  47. """
  48. if __name__ == "__main__":
  49. Gui(page).run(title="Chart - Bar stacked")