heatmap_drawing_on_top.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 numpy
  17. from taipy.gui import Gui
  18. # Return the x and y coordinates of the spiral for the given angle
  19. def spiral(th):
  20. a = 1.120529
  21. b = 0.306349
  22. r = a * numpy.exp(-b * th)
  23. return (r * numpy.cos(th), r * numpy.sin(th))
  24. # Prepare Golden spiral data as a parametric curve
  25. (x, y) = spiral(numpy.linspace(-numpy.pi / 13, 4 * numpy.pi, 1000))
  26. # Prepare the heatmap x and y cell sizes along the axes
  27. golden_ratio = (1 + numpy.sqrt(5)) / 2.0 # Golden ratio
  28. grid_x = [0, 1, 1 + (1 / (golden_ratio**4)), 1 + (1 / (golden_ratio**3)), golden_ratio]
  29. grid_y = [
  30. 0,
  31. 1 / (golden_ratio**3),
  32. 1 / golden_ratio**3 + 1 / golden_ratio**4,
  33. 1 / (golden_ratio**2),
  34. 1,
  35. ]
  36. # Main value is based on the Fibonacci sequence
  37. z = [[13, 3, 3, 5], [13, 2, 1, 5], [13, 10, 11, 12], [13, 8, 8, 8]]
  38. # Group all data sets in a single array
  39. data = [
  40. {
  41. "z": z,
  42. },
  43. {"x": numpy.sort(grid_x), "y": numpy.sort(grid_y)},
  44. {
  45. "xSpiral": -x + x[0],
  46. "ySpiral": y - y[0],
  47. },
  48. ]
  49. # Axis template: hide all ticks, lines and labels
  50. axis = {
  51. "range": [0, 2.0],
  52. "showgrid": False,
  53. "zeroline": False,
  54. "showticklabels": False,
  55. "ticks": "",
  56. "title": "",
  57. }
  58. layout = {
  59. # Use the axis template for both x and y axes
  60. "xaxis": axis,
  61. "yaxis": axis,
  62. }
  63. options = {
  64. # Hide the color scale of the heatmap
  65. "showscale": False
  66. }
  67. # Chart holds two traces, with different types
  68. types = ["heatmap", "scatter"]
  69. # x and y values for both traces
  70. xs = ["1/x", "2/xSpiral"]
  71. ys = ["1/y", "2/ySpiral"]
  72. page = """
  73. <|{data}|chart|type={types}|z[1]=0/z|x={xs}|y={ys}|layout={layout}|options={options}|>
  74. """
  75. if __name__ == "__main__":
  76. Gui(page).run(title="Chart - Heatmap - Drawing on top")