Skip to content

Commit 461cd48

Browse files
committed
adding Polyfit example
1 parent 05b0bec commit 461cd48

File tree

4 files changed

+147
-3
lines changed

4 files changed

+147
-3
lines changed

circuitpython_uplot/plot.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,13 @@ def update_plot(self) -> None:
464464
self._drawbox()
465465

466466
def show_text(
467-
self, text: str, x: int, y: int, anchorpoint: Tuple = (0.5, 0.0)
467+
self,
468+
text: str,
469+
x: int,
470+
y: int,
471+
anchorpoint: Tuple = (0.5, 0.0),
472+
text_color: int = 0xFFFFFF,
473+
free_text: bool = False,
468474
) -> None:
469475
"""
470476
@@ -473,11 +479,20 @@ def show_text(
473479
:param int x: x coordinate
474480
:param int y: y coordinate
475481
:param Tuple anchorpoint: Display_text anchor point. Defaults to (0.5, 0.0)
482+
:param int color: text color. Defaults to :const:`0xFFFFFF`
483+
:param bool free_text: Select to show free text
476484
:return: None
477485
"""
486+
try:
487+
self.bitmap_label
488+
except AttributeError:
489+
from adafruit_display_text import bitmap_label
478490

479-
if self._showtext:
480-
text_toplot = self.bitmap_label.Label(terminalio.FONT, text=text, x=x, y=y)
491+
self.bitmap_label = bitmap_label
492+
if self._showtext or free_text:
493+
text_toplot = self.bitmap_label.Label(
494+
terminalio.FONT, text=text, x=x, y=y, color=text_color
495+
)
481496
text_toplot.anchor_point = anchorpoint
482497
text_toplot.anchored_position = (x, y)
483498
self.append(text_toplot)

docs/cartesian_scatter_polyfit.jpg

84.4 KB
Loading

docs/examples.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ Example showing how to use different datasets
9292
.. image:: ../docs/scatter_using_different_datasets.jpg
9393

9494

95+
Cartesian and Scatter Example
96+
------------------------------
97+
98+
Example showing how to use cartesian and scatter in the same plot
99+
100+
.. literalinclude:: ../examples/cartersian_and_scatter_polyfit_example.py
101+
:caption: examples/cartersian_and_scatter_polyfit_example.py
102+
:lines: 5-
103+
.. image:: ../docs/cartesian_logging_data.gif
104+
105+
95106
Display_shapes Example
96107
-----------------------
97108

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)