|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from random import choice |
| 6 | +import board |
| 7 | +import displayio |
| 8 | +from ulab import numpy as np |
| 9 | +from table import Table |
| 10 | +from circuitpython_uplot.plot import Plot |
| 11 | +from circuitpython_uplot.scatter import Scatter |
| 12 | +from circuitpython_uplot.cartesian import Cartesian |
| 13 | + |
| 14 | +# In order to run this example you need to install the following libraries: |
| 15 | +# - adafruit_display_text |
| 16 | +# - adafruit_bitmap_font |
| 17 | +# - CircuitPython_TABLE (from https://github.com/jposada202020/CircuitPython_TABLE) |
| 18 | + |
| 19 | +g = displayio.Group() |
| 20 | + |
| 21 | +table_width = 125 |
| 22 | + |
| 23 | +# Setting up the display |
| 24 | +display = board.DISPLAY |
| 25 | + |
| 26 | +# Adding the plot area |
| 27 | +plot = Plot(0, 0, display.width - table_width, display.height, padding=1) |
| 28 | +plot.tick_params(tickx_height=12, ticky_height=12, tickcolor=0x939597, tickgrid=True) |
| 29 | +plot_table = Plot( |
| 30 | + display.width - table_width - 1, 0, table_width - 1, display.height, padding=1 |
| 31 | +) |
| 32 | + |
| 33 | +display.show(g) |
| 34 | +g.append(plot) |
| 35 | +g.append(plot_table) |
| 36 | + |
| 37 | + |
| 38 | +general_rangex = [0, 17] |
| 39 | +general_rangey = [0, 70] |
| 40 | + |
| 41 | +# Creating the values |
| 42 | +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) |
| 43 | +y = np.array([3, 14, 23, 25, 23, 15, 9, 5, 9, 13, 17, 24, 32, 36, 46]) |
| 44 | + |
| 45 | +# Creating the table |
| 46 | +# To use the font, create a fonts directory in the root of the CIRCUITPY drive, |
| 47 | +# and add the font file from the fonts folder |
| 48 | +# fmt: off |
| 49 | +my_table = Table( |
| 50 | + 10, |
| 51 | + 10, |
| 52 | + 140, |
| 53 | + 315, |
| 54 | + [("-----------", "-----------",)], |
| 55 | + [("Value X", "Value Y",), ("1", "3",), ("2", "14",),("3", "23",),("4", "25",),("5", "23",),("6", "15",), |
| 56 | + ("7", "9",),("8", "5",),("9", "9",),("10", "13",),("11", "17",),("12", "24",),("13", "32",),("14", "36",), |
| 57 | + ("15", "46",)], |
| 58 | + "fonts/LibreBodoniv2002-Bold-10.bdf", |
| 59 | + text_color = 0xFFFFFF, |
| 60 | +) |
| 61 | +# fmt: on |
| 62 | +plot_table.append(my_table) |
| 63 | + |
| 64 | +# Polyfit Curve third degree |
| 65 | +z = np.polyfit(x, y, 3) |
| 66 | +new_x = np.linspace(0, 16, 50) |
| 67 | +fit = z[0] * new_x**3 + z[1] * new_x**2 + z[2] * new_x + z[3] |
| 68 | +Cartesian(plot, new_x, fit, rangex=general_rangex, rangey=general_rangey) |
| 69 | + |
| 70 | +# Polyfit Curve Second degree |
| 71 | +z = np.polyfit(x, y, 2) |
| 72 | +new_x = np.linspace(0, 16, 50) |
| 73 | +fit = z[0] * new_x**2 + z[1] * new_x + z[2] |
| 74 | +Cartesian(plot, new_x, fit, rangex=general_rangex, rangey=general_rangey) |
| 75 | + |
| 76 | +# Polyfit Curve First degree |
| 77 | +z = np.polyfit(x, y, 1) |
| 78 | +new_x = np.linspace(0, 16, 50) |
| 79 | +fit = z[0] * new_x + z[1] |
| 80 | +Cartesian(plot, new_x, fit, rangex=general_rangex, rangey=general_rangey) |
| 81 | + |
| 82 | +# Adding the Scatter Plot |
| 83 | +Scatter( |
| 84 | + plot, |
| 85 | + x, |
| 86 | + y, |
| 87 | + rangex=general_rangex, |
| 88 | + rangey=general_rangey, |
| 89 | + pointer="triangle", |
| 90 | + pointer_color=0x00FFFF, |
| 91 | +) |
| 92 | + |
| 93 | +# Adding the labels for the Polylines |
| 94 | +# change the x and y values to move the text according to your needs |
| 95 | +plot.show_text( |
| 96 | + "Polyfit 1", |
| 97 | + x=300, |
| 98 | + y=10, |
| 99 | + anchorpoint=(0.5, 0.0), |
| 100 | + text_color=0x149F14, |
| 101 | + free_text=True, |
| 102 | +) |
| 103 | +plot.show_text( |
| 104 | + "Polyfit 2", |
| 105 | + x=72, |
| 106 | + y=270, |
| 107 | + anchorpoint=(0.0, 0.0), |
| 108 | + text_color=0x647182, |
| 109 | + free_text=True, |
| 110 | +) |
| 111 | +plot.show_text( |
| 112 | + "Polyfit 3", |
| 113 | + x=175, |
| 114 | + y=200, |
| 115 | + anchorpoint=(0.5, 0.0), |
| 116 | + text_color=0x7428EF, |
| 117 | + free_text=True, |
| 118 | +) |
0 commit comments