heatmap-drawing-on-top.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. # 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 = [0, 1 / (golden_ratio ** 3), 1 / golden_ratio ** 3 + 1 / golden_ratio ** 4, 1 / (golden_ratio ** 2), 1]
  30. # Main value is based on the Fibonacci sequence
  31. z = [[13, 3, 3, 5], [13, 2, 1, 5], [13, 10, 11, 12], [13, 8, 8, 8]]
  32. # Group all data sets in a single array
  33. data = [
  34. {
  35. "z": z,
  36. },
  37. {"x": numpy.sort(grid_x), "y": numpy.sort(grid_y)},
  38. {
  39. "xSpiral": -x + x[0],
  40. "ySpiral": y - y[0],
  41. },
  42. ]
  43. # Axis template: hide all ticks, lines and labels
  44. axis = {"range": [0, 2.0], "showgrid": False, "zeroline": False, "showticklabels": False, "ticks": "", "title": ""}
  45. layout = {
  46. # Use the axis template for both x and y axes
  47. "xaxis": axis,
  48. "yaxis": axis,
  49. }
  50. options = {
  51. # Hide the color scale of the heatmap
  52. "showscale": False
  53. }
  54. # Chart holds two traces, with different types
  55. types = ["heatmap", "scatter"]
  56. # x and y values for both traces
  57. xs = ["1/x", "2/xSpiral"]
  58. ys = ["1/y", "2/ySpiral"]
  59. page = """
  60. ## Heatmap - Drawing on top
  61. <|{data}|chart|type={types}|z[1]=0/z|x={xs}|y={ys}|layout={layout}|options={options}|>
  62. """
  63. Gui(page).run()