Skip to content

Commit baf6294

Browse files
committed
adding sparkline example
1 parent 25dc1dc commit baf6294

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,12 @@ umap simple example
140140
:caption: examples/uplot_umap.py
141141
:linenos:
142142
.. image:: ../docs/uplot_ex17.jpg
143+
144+
Sparkline Animation Example
145+
---------------------------
146+
147+
Sparkline animation example
148+
149+
.. literalinclude:: ../examples/uplot_sparkline.py
150+
:caption: examples/uplot_sparkline.py
151+
:linenos:

examples/uplot_sparkline.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
from ulab import numpy as np
8+
from adafruit_display_shapes.sparkline import Sparkline
9+
from circuitpython_uplot.uplot import Uplot, color
10+
11+
12+
# Setting up the display
13+
display = board.DISPLAY
14+
15+
# Adding the plot area
16+
plot = Uplot(0, 0, display.width, display.height)
17+
18+
# 500 linearly spaced numbers
19+
x = np.linspace(-10 * np.pi, 10 * np.pi, 500)
20+
21+
# the function, which is y = sin(x) here
22+
y = np.sin(x)
23+
24+
# Creating the sparkline
25+
sparkline = Sparkline(
26+
width=200,
27+
height=200,
28+
max_items=80,
29+
y_min=np.min(y),
30+
y_max=np.max(y),
31+
x=100,
32+
y=40,
33+
color=color.TEAL,
34+
)
35+
36+
# Adding shapes to the plot
37+
plot.append(sparkline)
38+
39+
# Plotting and showing the plot
40+
display.show(plot)
41+
42+
for element in y:
43+
display.auto_refresh = False
44+
sparkline.add_value(element)
45+
display.auto_refresh = True
46+
47+
time.sleep(0.1)

0 commit comments

Comments
 (0)