Skip to content

Commit 3aae5cb

Browse files
committed
updating ranges, and colors, adding readme example code
1 parent 1fa4dee commit 3aae5cb

File tree

7 files changed

+157
-27
lines changed

7 files changed

+157
-27
lines changed

circuitpython_uplot/ubar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def __init__(self, plot, x: any, y: any, color=0xFFFFFF, fill=False) -> None:
3232
self._graphy = abs(plot._newymax - plot._newymin) // (max(y) + 2)
3333
self._new_min = int(plot.normalize(0, max(y), max(y), 0, 0))
3434
self._new_max = int(plot.normalize(0, max(y), max(y), 0, max(y)))
35-
xstart = self._graphx
35+
3636
bar_space = max(2, plot._width // 30)
37+
xstart = self._graphx + bar_space
3738

3839
plot._plot_palette[plot._index_colorused] = color
3940

circuitpython_uplot/ucartesian.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
`ucartesian`
88
================================================================================
99
10-
CircuitPython pie graph
10+
CircuitPython cartesian graph
1111
1212
* Author(s): Jose D. Montoya
1313

circuitpython_uplot/uplot.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838

3939

4040
# pylint: disable=too-many-arguments, too-many-instance-attributes, too-many-locals
41-
42-
41+
# pylint: disable=too-many-statements
4342
class Uplot(displayio.Group):
4443
"""
4544
Canvas Class to add different elements to the screen.
@@ -50,6 +49,7 @@ class Uplot(displayio.Group):
5049
:param int width: plot box width in pixels
5150
:param int height: plot box height in pixels
5251
:param int padding: padding for the plot box in all directions
52+
:param bool show_box: select if the plot box is displayed
5353
5454
"""
5555

@@ -85,6 +85,9 @@ def __init__(
8585

8686
self._axesparams = "box"
8787

88+
self._width = width
89+
self._height = height
90+
8891
self.padding = padding
8992
self._newxmin = padding
9093
self._newxmax = width - padding
@@ -97,14 +100,14 @@ def __init__(
97100
self._showticks = False
98101
self._tickgrid = False
99102

100-
self._barcolor = 0xFFFFFF
103+
self._grid_espace = self._width // 40
104+
self._grid_lenght = self._width // 20
101105

102-
self._piecolor = 0xFFFFFF
106+
self._barcolor = 0x69FF8F
103107

104-
self._index_colorused = 4
108+
self._piecolor = 0x8B77FF
105109

106-
self._width = width
107-
self._height = height
110+
self._index_colorused = 4
108111

109112
self._plotbitmap = displayio.Bitmap(width, height, 14)
110113

@@ -116,8 +119,8 @@ def __init__(
116119
self._plot_palette[1] = 0xFFFFFF
117120
self._plot_palette[2] = self._tickcolor
118121
self._plot_palette[3] = self._barcolor
119-
self._plot_palette[4] = 0xFFFF00 # Pie Chart color 1
120-
self._plot_palette[5] = 0xFF0000 # Pie Chart color 2
122+
self._plot_palette[4] = 0x149F14 # Pie Chart color 1
123+
self._plot_palette[5] = 0x647182 # Pie Chart color 2
121124
self._plot_palette[6] = 0x7428EF # Pie Chart color 3
122125
self._plot_palette[7] = 0x005E99 # Pie Chart color 4
123126
self._plot_palette[8] = 0x00A76D # Pie Chart color 5
@@ -344,20 +347,18 @@ def _draw_gridx(self, ticks_data: list[int]) -> None:
344347
:return: None
345348
346349
"""
347-
grid_espace = 10
348-
line_lenght = 10
349350
for tick in ticks_data:
350351
start = self._newymin
351-
while start >= self._newymax:
352+
while start - self._grid_lenght >= self._newymax:
352353
draw_line(
353354
self._plotbitmap,
354355
tick,
355356
start,
356357
tick,
357-
start - line_lenght,
358+
start - self._grid_lenght,
358359
2,
359360
)
360-
start = start - grid_espace - line_lenght
361+
start = start - self._grid_espace - self._grid_lenght
361362

362363
def _draw_gridy(self, ticks_data: list[int]) -> None:
363364
"""
@@ -366,20 +367,18 @@ def _draw_gridy(self, ticks_data: list[int]) -> None:
366367
:return: None
367368
368369
"""
369-
grid_espace = 10
370-
line_lenght = 10
371370
for tick in ticks_data:
372371
start = self._newxmin
373372
while start <= self._newxmax:
374373
draw_line(
375374
self._plotbitmap,
376375
start,
377376
tick,
378-
start + line_lenght,
377+
start + self._grid_lenght,
379378
tick,
380379
2,
381380
)
382-
start = start + grid_espace + line_lenght
381+
start = start + self._grid_espace + self._grid_lenght
383382

384383
def update_plot(self) -> None:
385384
"""

circuitpython_uplot/uscatter.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
`uscatter`
99
================================================================================
1010
11-
CircuitPython uscatter graph
11+
CircuitPython scatter graph
1212
1313
* Author(s): Jose D. Montoya
1414
@@ -21,36 +21,59 @@
2121
__version__ = "0.0.0+auto.0"
2222
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
2323

24-
# pylint: disable=too-few-public-methods, invalid-name
24+
# pylint: disable=too-few-public-methods, invalid-name, duplicate-code, too-many-locals, too-many-arguments
2525
class uscatter:
2626
"""
2727
Main class to display different graphics
2828
"""
2929

30-
def __init__(self, plot, x: any, y: any, radius: int = 3) -> None:
30+
def __init__(
31+
self,
32+
plot,
33+
x: any,
34+
y: any,
35+
rangex: any = None,
36+
rangey: any = None,
37+
radius: int = 3,
38+
circle_color=0xFF905D,
39+
) -> None:
3140
"""
3241
33-
:param plot: Plot object for the uscatter to be drawn
42+
:param plot: Plot object for the scatter to be drawn
3443
:param x: x points coordinates
3544
:param y: y points coordinates
3645
:param int radius: circle radius
3746
3847
"""
3948

49+
if rangex is None:
50+
xmin = np.min(x)
51+
xmax = np.max(x)
52+
else:
53+
xmin = min(rangex)
54+
xmax = max(rangex)
55+
56+
if rangey is None:
57+
ymin = np.min(y)
58+
ymax = np.max(y)
59+
else:
60+
ymin = min(rangey)
61+
ymax = max(rangey)
62+
4063
x = np.array(x)
4164
y = np.array(y)
4265

4366
xnorm = np.array(
44-
plot.normalize(np.min(x), np.max(x), plot._newxmin, plot._newxmax, x),
67+
plot.normalize(xmin, xmax, plot._newxmin, plot._newxmax, x),
4568
dtype=np.uint16,
4669
)
4770
ynorm = np.array(
48-
plot.normalize(np.min(y), np.max(y), plot._newymin, plot._newymax, y),
71+
plot.normalize(ymin, ymax, plot._newymin, plot._newymax, y),
4972
dtype=np.uint16,
5073
)
5174

5275
palette = displayio.Palette(1)
53-
palette[0] = 0xFFFFFF
76+
palette[0] = circle_color
5477
for i, _ in enumerate(x):
5578
plot.append(
5679
Circle(pixel_shader=palette, radius=radius, x=xnorm[i], y=ynorm[i])

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,13 @@ Stackplot simple example
100100
:caption: examples/uplot_stackplot.py
101101
:linenos:
102102
.. image:: ../docs/uplot_ex12.jpg
103+
104+
Advanced Example
105+
---------------------------
106+
107+
plot different ulements in a single display
108+
109+
.. literalinclude:: ../examples/uplot_readme_example.py
110+
:caption: examples/uplot_readme_example.py
111+
:linenos:
112+
.. image:: ../docs/readme.png

docs/readme.png

-66.5 KB
Loading

examples/uplot_readme_example.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 circuitpython_uplot.uplot import Uplot
10+
from circuitpython_uplot.ubar import ubar
11+
from circuitpython_uplot.uscatter import uscatter
12+
from circuitpython_uplot.upie import upie
13+
from circuitpython_uplot.ucartesian import ucartesian
14+
15+
16+
display = board.DISPLAY
17+
plot = Uplot(0, 0, display.width, display.height, show_box=False)
18+
19+
group = displayio.Group()
20+
21+
palette = displayio.Palette(1)
22+
palette[0] = 0xFFFFFF
23+
24+
25+
plot2 = Uplot(0, 0, 130, 130)
26+
x = np.linspace(-4, 4, num=25)
27+
constant = 2.0 / np.sqrt(2 * np.pi)
28+
y = constant * np.exp((-(x**2)) / 4.0)
29+
ucartesian(plot2, x, y, rangex=[-5, 5], rangey=[0, 1])
30+
plot.append(plot2)
31+
32+
plot3 = Uplot(130, 0, 160, 160)
33+
34+
# Setting up tick parameters
35+
plot3.tick_params(tickheight=12, tickcolor=0xFF00FF, tickgrid=True)
36+
37+
# Seeting some date to plot
38+
x = np.linspace(-4, 4, num=50)
39+
constant = 1.0 / np.sqrt(2 * np.pi)
40+
y = constant * np.exp((-(x**2)) / 2.0)
41+
42+
# Plotting and showing the plot
43+
ucartesian(plot3, x, y, rangex=[-5, 5], rangey=[0, 0.5])
44+
plot.append(plot3)
45+
46+
plot4 = Uplot(290, 0, 150, 150)
47+
48+
# Setting up tick parameters
49+
plot4.axs_params(axstype="box")
50+
a = ["a", "b", "c", "d"]
51+
b = [3, 5, 1, 7]
52+
ubar(plot4, a, b, 0xFF1000, fill=True)
53+
54+
plot.append(plot4)
55+
56+
# Setting up the display
57+
58+
plot5 = Uplot(0, 180, 120, 120)
59+
60+
# Setting up tick parameters
61+
62+
plot5.axs_params(axstype="cartesian")
63+
a = np.linspace(3, 98)
64+
b = [choice(a) for _ in a]
65+
uscatter(plot5, a, b, rangex=[0, 102], rangey=[0, 102], radius=2)
66+
67+
plot.append(plot5)
68+
69+
plot6 = Uplot(130, 160, 150, 150)
70+
71+
# Setting up tick parameters
72+
plot6.axs_params(axstype="box")
73+
a = [5, 2, 7, 3]
74+
75+
upie(plot6, a, 0, 0)
76+
77+
plot.append(plot6)
78+
79+
80+
plot7 = Uplot(290, 160, 150, 150)
81+
82+
# Creating some points to graph
83+
x = np.linspace(1, 10, num=10)
84+
85+
y = [6, 7, 9, 6, 9, 7, 6, 6, 8, 9]
86+
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0xFF0000, fill=True)
87+
88+
y = [4, 3, 7, 8, 3, 9, 3, 2, 1, 2]
89+
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0xFF00FF, fill=True)
90+
91+
y = [1, 4, 6, 3, 6, 6, 5, 0, 9, 2]
92+
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0x4444FF, fill=True)
93+
94+
plot.append(plot7)
95+
96+
97+
display.show(plot)

0 commit comments

Comments
 (0)