Skip to content

Commit 151e04b

Browse files
committed
adding shade utility
1 parent b070434 commit 151e04b

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

circuitpython_uplot/shade.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`shade`
8+
================================================================================
9+
10+
CircuitPython shade utility for uPlot.
11+
12+
* Author(s): Jose D. Montoya
13+
14+
15+
"""
16+
try:
17+
from typing import Optional, Union
18+
from circuitpython_uplot.uplot import Uplot
19+
except ImportError:
20+
pass
21+
from ulab import numpy as np
22+
from bitmaptools import fill_region
23+
24+
25+
__version__ = "0.0.0+auto.0"
26+
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
27+
28+
29+
class shade:
30+
"""
31+
Class to draw shade between two lines
32+
"""
33+
34+
def __init__(
35+
self,
36+
plot: Uplot,
37+
x: Union[list, np.linspace, np.ndarray],
38+
y1: list,
39+
y2: list,
40+
rangex: Optional[list] = None,
41+
rangey: Optional[list] = None,
42+
fill_color: int = 0xF6FF41,
43+
nudge: bool = True,
44+
) -> None:
45+
"""
46+
:param Uplot plot: Plot object for the scatter to be drawn
47+
:param list|ulab.numpy.linspace|ulab.numpy.ndarray x: x points coordinates
48+
:param list y1: y1 points coordinates
49+
:param list y2: y2 points coordinates
50+
:param list|None rangex: x range limits
51+
:param list|None rangey: y range limits
52+
:param int fill_color: filling color. Defaults to 0xF6FF41
53+
:param bool nudge: moves the graph a little for better displaying
54+
55+
"""
56+
57+
if nudge:
58+
nudge_factor = 1
59+
else:
60+
nudge_factor = 0
61+
62+
plot._plot_palette[plot._index_colorused] = fill_color
63+
64+
if rangex is None:
65+
xmin = np.min(x) - nudge_factor * (abs(np.max(x) - np.min(x)) / 10)
66+
xmax = np.max(x) + nudge_factor * (abs(np.max(x) - np.min(x)) / 10)
67+
else:
68+
xmin = min(rangex)
69+
xmax = max(rangex)
70+
71+
if rangey is None:
72+
ymin = min(np.min(y1), np.min(y2)) - nudge_factor * (
73+
abs(np.max(y1) - np.min(y1)) / 10
74+
)
75+
ymax = max(np.max(y1), np.max(y2)) + nudge_factor * (
76+
abs(np.max(y1) - np.min(y1)) / 10
77+
)
78+
else:
79+
ymin = min(rangey)
80+
ymax = max(rangey)
81+
82+
x = np.array(x)
83+
y1 = np.array(y1)
84+
y2 = np.array(y2)
85+
86+
xnorm = np.array(
87+
plot.transform(xmin, xmax, plot._newxmin, plot._newxmax, x),
88+
dtype=np.int16,
89+
)
90+
y1norm = np.array(
91+
plot.transform(ymin, ymax, plot._newymin, plot._newymax, y1),
92+
dtype=np.int16,
93+
)
94+
y2norm = np.array(
95+
plot.transform(ymin, ymax, plot._newymin, plot._newymax, y2),
96+
dtype=np.int16,
97+
)
98+
99+
fill_region(
100+
plot._plotbitmap,
101+
min(xnorm),
102+
min(y1norm),
103+
max(xnorm),
104+
max(y2norm),
105+
plot._index_colorused,
106+
)
107+
108+
plot._index_colorused += 1

circuitpython_uplot/ucartesian.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def __init__(
121121
for index, _ in enumerate(xnorm):
122122
if index + 1 >= len(xnorm):
123123
break
124+
if y[index] >= ymax:
125+
continue
124126
draw_line(
125127
plot._plotbitmap,
126128
xnorm[index],

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ Uplot Library
2727

2828
.. automodule:: circuitpython_uplot.usvg
2929
:members:
30+
31+
.. automodule:: circuitpython_uplot.shade
32+
:members:

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,13 @@ SVG Images example
260260
:caption: examples/uplot_usvg_example.py
261261
:lines: 5-
262262
.. image:: ../docs/uplot_ex23.jpg
263+
264+
Shade examples
265+
---------------------------
266+
267+
Shade example
268+
269+
.. literalinclude:: ../examples/uplot_shade_example.py
270+
:caption: examples/uplot_shade_example.py
271+
:lines: 5-
272+
.. image:: ../docs/uplot_shade.jpg

docs/uplot_shade.jpg

64 KB
Loading

examples/uplot_shade_example.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 circuitpython_uplot.uplot import Uplot
9+
from circuitpython_uplot.ucartesian import ucartesian
10+
from circuitpython_uplot.shade import shade
11+
12+
# This example is based in the following Library
13+
# https://github.com/CedarGroveStudios/CircuitPython_TemperatureTools
14+
# And this Wikipedia Article
15+
# https://en.wikipedia.org/wiki/Heat_index
16+
17+
18+
# Setting up the display
19+
display = board.DISPLAY
20+
21+
# Adding the plot area
22+
plot = Uplot(0, 0, display.width, display.height, padding=5)
23+
24+
25+
def heat_index(temp, humidity):
26+
"""
27+
Inspired by
28+
https://github.com/CedarGroveStudios/CircuitPython_TemperatureTools
29+
30+
"""
31+
32+
heat_index_value = (
33+
-8.78469475556
34+
+ 1.61139411 * temp
35+
+ 2.33854883889 * humidity
36+
- 0.14611605 * temp * humidity
37+
- 0.012308094 * temp**2
38+
- 0.0164248277778 * humidity**2
39+
+ 0.002211732 * temp**2 * humidity
40+
+ 0.00072546 * temp * humidity**2
41+
- 0.000003582 * temp**2 * humidity**2
42+
)
43+
return heat_index_value
44+
45+
46+
x = np.linspace(25, 50, num=2)
47+
48+
# Drawing the Shades
49+
shade(
50+
plot,
51+
x,
52+
[25, 25],
53+
[26.3, 26.3],
54+
rangex=[25, 50],
55+
rangey=[25, 60],
56+
fill_color=0x59FF33,
57+
)
58+
shade(
59+
plot,
60+
x,
61+
[26.3, 26.3],
62+
[30.5, 30.5],
63+
rangex=[25, 50],
64+
rangey=[25, 60],
65+
fill_color=0xFFFDD0,
66+
)
67+
shade(
68+
plot,
69+
x,
70+
[30.5, 30.5],
71+
[40.5, 40.5],
72+
rangex=[25, 50],
73+
rangey=[25, 60],
74+
fill_color=0xFFEB99,
75+
)
76+
shade(
77+
plot,
78+
x,
79+
[40.5, 40.5],
80+
[53.5, 53.5],
81+
rangex=[25, 50],
82+
rangey=[25, 60],
83+
fill_color=0xFFDB58,
84+
)
85+
shade(
86+
plot,
87+
x,
88+
[53.5, 53.5],
89+
[60, 60],
90+
rangex=[25, 50],
91+
rangey=[25, 60],
92+
fill_color=0xFF9D5C,
93+
)
94+
95+
# Creating some points to graph
96+
x = np.linspace(25, 50, num=25)
97+
98+
# Drawing the graphs
99+
for i in range(40, 110, 10):
100+
ucartesian(plot, x, heat_index(x, i), rangex=[25, 50], rangey=[25, 60])
101+
102+
display.show(plot)
103+
while True:
104+
time.sleep(1)

0 commit comments

Comments
 (0)