image.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 os
  17. import matplotlib.pyplot as plt
  18. from taipy.gui import Gui, Markdown
  19. # Generate a scatter plot
  20. x = [1, 2, 3, 4, 5] # x axis values
  21. y = [10, 14, 12, 15, 18] # y axis values
  22. sizes = [100, 100, 100, 100, 100] # Bubble sizes
  23. colors = [30, 40, 50, 60, 70] # Bubble color values that will be mapped to shades of colormap (cmap)
  24. # Scatter plot
  25. # The `c` parameter uses the `colors` list to map values to the colormap (cmap)
  26. # The `edgecolors` parameter sets the color of the edges around the bubbles
  27. plt.scatter(x, y, s=sizes, c=colors, cmap='Greens', edgecolors='black', linewidths=1)
  28. plt.xlabel('X-axis')
  29. plt.ylabel('Y-axis')
  30. plt.title('Matplotlib 2D Scatter Plot')
  31. # Adding labels next to each point
  32. for i in range(len(x)):
  33. plt.text(x[i] + 0.1, y[i] - 0.1, f'Point {i+1}', fontsize=9, ha='left')
  34. # Creating legend entries for each point
  35. # Each entry corresponds to a point, with the color and label indicating the point's position.
  36. # The `markerfacecolor` sets the fill color of the legend marker to match the point's color in the 'Greens' colormap.
  37. # The `markeredgecolor` sets the edge color of the legend marker.
  38. # The `markeredgewidth` sets the width of the edge.
  39. handles = [
  40. plt.Line2D(
  41. [0], [0], marker='o', color='w',
  42. markerfacecolor=plt.cm.Greens(color / max(colors)),
  43. markersize=10, # Adjust the size for visibility
  44. label=f'Point {i+1}',
  45. markeredgewidth=1, # Thickness of the edge color
  46. markeredgecolor='black' # Edge color to match the plot
  47. ) for i, color in enumerate(colors)
  48. ]
  49. # Placing the legend on the left side of the chart
  50. plt.legend(handles=handles, title="Points", loc="center left", bbox_to_anchor=(1, 0.5), frameon=True)
  51. # Adjust the layout to ensure everything fits and nothing is clipped
  52. plt.tight_layout(rect=[0, 0, 1, 1])
  53. # Save the figure as a PNG image
  54. output_dir = './images'
  55. if not os.path.exists(output_dir):
  56. os.makedirs(output_dir)
  57. content = "./images/figure.png"
  58. plt.savefig(content)
  59. # Define Taipy page content
  60. page_content = Markdown("""
  61. # Matplotlib 2D Scatter Plot
  62. <|{content}|image|class_name=scatter-plot|>
  63. """, style={
  64. ".scatter-plot": {
  65. "display": "block",
  66. "margin": "auto",
  67. "max-width": "100% !important",
  68. "width": "max-content !important",
  69. "height": "max-content !important"
  70. }
  71. })
  72. if __name__ == "__main__":
  73. Gui(page_content).run(title="Chart-Scatter-Matplotlib")