heatmap-drawing-on-top.py 2.8 KB

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