Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit e5f81ef

Browse files
committed
adding fillbetween
1 parent 22860d4 commit e5f81ef

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-1
lines changed

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ MicroPython UPLOT Library
2222

2323
.. automodule:: micropython_uplot.map
2424
:members:
25+
26+
.. automodule:: micropython_uplot.fillbetween
27+
:members:

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,13 @@ Map Simpletest
130130
:caption: examples/map_simpletest.py
131131
:lines: 5-
132132
.. image:: ../docs/map_simpletest.jpg
133+
134+
Fillbetween Simpletest
135+
----------------------------
136+
137+
Fillbetween Simpletest
138+
139+
.. literalinclude:: ../examples/fillbetween_simpletest.py
140+
:caption: examples/fillbetween_simpletest.py
141+
:lines: 5-
142+
.. image:: ../docs/fillbetween.jpg

docs/fillbetween.jpg

14.9 KB
Loading

examples/fillbetween_simpletest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import gc
6+
from machine import Pin, SPI
7+
from ili9486 import ILI9486
8+
from micropython_uplot.plot import PLOT
9+
from micropython_uplot.utils import linspace
10+
from micropython_uplot.fillbetween import Fillbetween
11+
12+
# Pin definition
13+
pdc = Pin(8, Pin.OUT, value=0)
14+
prst = Pin(15, Pin.OUT, value=1)
15+
pcs = Pin(9, Pin.OUT, value=1)
16+
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
17+
gc.collect()
18+
display = ILI9486(spi, pcs, pdc, prst)
19+
20+
plot = PLOT(display, 5, 5, 300, 200, padding=1, box_color=(255, 255, 255))
21+
22+
23+
x = list(linspace(0, 8, 25))
24+
25+
y1 = [value**2 / 2 for value in x]
26+
y2 = [2 + value**2 + 3 * value for value in x]
27+
28+
Fillbetween(plot, x, y1, y2, fill_color=(255, 0, 0))
29+
30+
display.show()

micropython_uplot/fillbetween.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`fillbetween`
8+
================================================================================
9+
10+
MicroPython fillbetween graph
11+
12+
* Author: Jose D. Montoya
13+
14+
15+
"""
16+
try:
17+
from typing import Optional, Union
18+
from micropython_uplot.plot import PLOT
19+
except ImportError:
20+
pass
21+
22+
from array import array
23+
from micropython_uplot.colors import set_color
24+
25+
26+
__version__ = "0.0.0+auto.0"
27+
__repo__ = "https://github.com/adafruit/MicroPython_UPLOT.git"
28+
29+
30+
class Fillbetween:
31+
"""
32+
Class to draw a fillbetween graph
33+
"""
34+
35+
def __init__(
36+
self,
37+
plot: PLOT,
38+
x: list,
39+
y1: list,
40+
y2: list,
41+
rangex: Optional[list] = None,
42+
rangey: Optional[list] = None,
43+
fill_color: int = (0, 255, 0),
44+
pointer_index: Optional[int] = None,
45+
) -> None:
46+
"""
47+
:param Plot plot: Plot object for the scatter to be drawn
48+
:param list x: x points coordinates
49+
:param list y1: y1 points coordinates
50+
:param list y2: y2 points coordinates
51+
:param list|None rangex: x range limits
52+
:param list|None rangey: y range limits
53+
:param int fill_color: filling color. Defaults to (0, 255, 0)
54+
:param int|None pointer_index: Pointer index. Defaults to None
55+
56+
"""
57+
58+
if pointer_index is None:
59+
self._pointer_index = plot._pointer_index
60+
else:
61+
self._pointer_index = pointer_index
62+
63+
self._line_color = set_color(
64+
plot._display,
65+
self._pointer_index,
66+
fill_color[0],
67+
fill_color[1],
68+
fill_color[2],
69+
)
70+
plot._pointer_index += 1
71+
72+
points = []
73+
74+
max_x = max(x)
75+
min_x = min(x)
76+
max_y = max(max(y2), max(y1))
77+
min_y = min(min(y1), min(y2))
78+
79+
if rangex is None:
80+
self.xmin = min_x - (abs(max_x - min_x) / 10)
81+
self.xmax = max_x + (abs(max_x - min_x) / 10)
82+
83+
else:
84+
self.xmin = min(rangex)
85+
self.xmax = max(rangex)
86+
87+
if rangey is None:
88+
self.ymin = min_y - (abs(max_y - min_y) / 10)
89+
self.ymax = max_y + (abs(max_y - min_y) / 10)
90+
else:
91+
self.ymin = min(rangey)
92+
self.ymax = max(rangey)
93+
94+
xnorm = [
95+
int(plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, _))
96+
for _ in x
97+
]
98+
xrev = [
99+
int(plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, _))
100+
for _ in x
101+
]
102+
103+
xrev.reverse()
104+
105+
y1norm = tuple(
106+
[
107+
int(
108+
plot.transform(
109+
self.ymin, self.ymax, plot._newymin, plot._newymax, _
110+
)
111+
)
112+
for _ in y1
113+
]
114+
)
115+
y2norm = [
116+
int(plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, _))
117+
for _ in y2
118+
]
119+
y2norm.reverse()
120+
121+
flip2y = y2norm
122+
123+
for index, item in enumerate(xnorm):
124+
points.extend([item, y1norm[index]])
125+
for index, item in enumerate(xrev):
126+
points.extend([item, flip2y[index]])
127+
128+
array_points = array("i", points)
129+
plot._display.poly(0, 0, array_points, self._line_color, True)

micropython_uplot/plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def _savingppm(
485485
self._color0,
486486
self._color2,
487487
self._color1,
488-
self._color0,
488+
(255, 0, 0),
489489
(237, 0, 86),
490490
(218, 0, 105),
491491
(199, 0, 124),

0 commit comments

Comments
 (0)