Skip to content

Commit 55a516b

Browse files
committed
adding logging animation example
1 parent 663de78 commit 55a516b

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

circuitpython_uplot/ulogging.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from circuitpython_uplot.uplot import Uplot
1919
except ImportError:
2020
pass
21+
import math
2122
from bitmaptools import draw_line, fill_region
2223
from ulab import numpy as np
2324

@@ -112,9 +113,7 @@ def _draw_ticks(self, plot) -> None:
112113
2,
113114
)
114115
if plot._showtext:
115-
plot.show_text(
116-
"{:.2f}".format(ticksxnorm[i]), tick, plot._newymin, (0.5, 0.0)
117-
)
116+
plot.show_text(f"{self.ticksx[i]:.0f}", tick, plot._newymin, (0.5, 0.0))
118117
for i, tick in enumerate(ticksynorm):
119118
draw_line(
120119
plot._plotbitmap,
@@ -125,9 +124,7 @@ def _draw_ticks(self, plot) -> None:
125124
2,
126125
)
127126
if plot._showtext:
128-
plot.show_text(
129-
"{:.2f}".format(ticksynorm[i]), plot._newxmin, tick, (1.0, 0.5)
130-
)
127+
plot.show_text(f"{self.ticksy[i]:.0f}", plot._newxmin, tick, (1.0, 0.5))
131128

132129
@staticmethod
133130
def clear_plot(plot) -> None:

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ This example shows how to add a data table to the plot
271271
.. image:: ../docs/logging_table.jpg
272272

273273

274+
Logging Animation Example
275+
---------------------------------------
276+
277+
This example shows how to animate a plot
278+
279+
.. literalinclude:: ../examples/uplot_ulogging_animation.py
280+
:caption: examples/uplot_ulogging_animation.py
281+
:lines: 5-
282+
274283
SVG Images examples
275284
---------------------------
276285

examples/uplot_ulogging_animation.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
6+
import time
7+
import random
8+
import board
9+
from circuitpython_uplot.uplot import Uplot, color
10+
from circuitpython_uplot.ulogging import ulogging
11+
12+
# Setting up the display
13+
display = board.DISPLAY
14+
display.auto_refresh = False
15+
16+
# Drawing the graph
17+
my_plot = Uplot(
18+
140,
19+
60,
20+
200,
21+
200,
22+
padding=1,
23+
show_box=True,
24+
box_color=color.WHITE,
25+
)
26+
27+
# Setting the tick parameters
28+
my_plot.tick_params(
29+
tickx_height=4,
30+
ticky_height=4,
31+
show_ticks=True,
32+
tickcolor=color.TEAL,
33+
showtext=True,
34+
)
35+
36+
# Creating the x and y data
37+
x = [
38+
10,
39+
20,
40+
30,
41+
40,
42+
50,
43+
60,
44+
70,
45+
80,
46+
90,
47+
100,
48+
110,
49+
120,
50+
130,
51+
140,
52+
150,
53+
160,
54+
170,
55+
180,
56+
190,
57+
]
58+
y = [26, 22, 24, 30, 28, 35, 26, 25, 24, 23, 20, 27, 26, 33, 24, 23, 19, 27, 26]
59+
60+
# Creating the random numbers
61+
random_numbers = [19, 22, 35, 33, 24, 26, 28, 37]
62+
63+
64+
display.show(my_plot)
65+
display.refresh()
66+
67+
dist = 1
68+
69+
# Creating the loggraph
70+
my_loggraph = ulogging(
71+
my_plot,
72+
x[0:dist],
73+
y[0:dist],
74+
rangex=[0, 210],
75+
rangey=[0, 110],
76+
line_color=color.BLUE,
77+
ticksx=[25, 50, 75, 100, 125, 150, 175, 200],
78+
ticksy=[25, 50, 75, 100],
79+
)
80+
81+
# Showing the loggraph
82+
while True:
83+
if dist > len(x):
84+
y.pop(0)
85+
y.append(random.choice(random_numbers))
86+
87+
my_loggraph.draw_points(my_plot, x[0:dist], y[0:dist])
88+
display.refresh()
89+
dist += 1
90+
time.sleep(0.5)

0 commit comments

Comments
 (0)