Skip to content

Commit

Permalink
updating ranges, and colors, adding readme example code
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Feb 17, 2023
1 parent 1fa4dee commit 3aae5cb
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 27 deletions.
3 changes: 2 additions & 1 deletion circuitpython_uplot/ubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def __init__(self, plot, x: any, y: any, color=0xFFFFFF, fill=False) -> None:
self._graphy = abs(plot._newymax - plot._newymin) // (max(y) + 2)
self._new_min = int(plot.normalize(0, max(y), max(y), 0, 0))
self._new_max = int(plot.normalize(0, max(y), max(y), 0, max(y)))
xstart = self._graphx

bar_space = max(2, plot._width // 30)
xstart = self._graphx + bar_space

plot._plot_palette[plot._index_colorused] = color

Expand Down
2 changes: 1 addition & 1 deletion circuitpython_uplot/ucartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
`ucartesian`
================================================================================
CircuitPython pie graph
CircuitPython cartesian graph
* Author(s): Jose D. Montoya
Expand Down
35 changes: 17 additions & 18 deletions circuitpython_uplot/uplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@


# pylint: disable=too-many-arguments, too-many-instance-attributes, too-many-locals


# pylint: disable=too-many-statements
class Uplot(displayio.Group):
"""
Canvas Class to add different elements to the screen.
Expand All @@ -50,6 +49,7 @@ class Uplot(displayio.Group):
:param int width: plot box width in pixels
:param int height: plot box height in pixels
:param int padding: padding for the plot box in all directions
:param bool show_box: select if the plot box is displayed
"""

Expand Down Expand Up @@ -85,6 +85,9 @@ def __init__(

self._axesparams = "box"

self._width = width
self._height = height

self.padding = padding
self._newxmin = padding
self._newxmax = width - padding
Expand All @@ -97,14 +100,14 @@ def __init__(
self._showticks = False
self._tickgrid = False

self._barcolor = 0xFFFFFF
self._grid_espace = self._width // 40
self._grid_lenght = self._width // 20

self._piecolor = 0xFFFFFF
self._barcolor = 0x69FF8F

self._index_colorused = 4
self._piecolor = 0x8B77FF

self._width = width
self._height = height
self._index_colorused = 4

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

Expand All @@ -116,8 +119,8 @@ def __init__(
self._plot_palette[1] = 0xFFFFFF
self._plot_palette[2] = self._tickcolor
self._plot_palette[3] = self._barcolor
self._plot_palette[4] = 0xFFFF00 # Pie Chart color 1
self._plot_palette[5] = 0xFF0000 # Pie Chart color 2
self._plot_palette[4] = 0x149F14 # Pie Chart color 1
self._plot_palette[5] = 0x647182 # Pie Chart color 2
self._plot_palette[6] = 0x7428EF # Pie Chart color 3
self._plot_palette[7] = 0x005E99 # Pie Chart color 4
self._plot_palette[8] = 0x00A76D # Pie Chart color 5
Expand Down Expand Up @@ -344,20 +347,18 @@ def _draw_gridx(self, ticks_data: list[int]) -> None:
:return: None
"""
grid_espace = 10
line_lenght = 10
for tick in ticks_data:
start = self._newymin
while start >= self._newymax:
while start - self._grid_lenght >= self._newymax:
draw_line(
self._plotbitmap,
tick,
start,
tick,
start - line_lenght,
start - self._grid_lenght,
2,
)
start = start - grid_espace - line_lenght
start = start - self._grid_espace - self._grid_lenght

def _draw_gridy(self, ticks_data: list[int]) -> None:
"""
Expand All @@ -366,20 +367,18 @@ def _draw_gridy(self, ticks_data: list[int]) -> None:
:return: None
"""
grid_espace = 10
line_lenght = 10
for tick in ticks_data:
start = self._newxmin
while start <= self._newxmax:
draw_line(
self._plotbitmap,
start,
tick,
start + line_lenght,
start + self._grid_lenght,
tick,
2,
)
start = start + grid_espace + line_lenght
start = start + self._grid_espace + self._grid_lenght

def update_plot(self) -> None:
"""
Expand Down
37 changes: 30 additions & 7 deletions circuitpython_uplot/uscatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
`uscatter`
================================================================================
CircuitPython uscatter graph
CircuitPython scatter graph
* Author(s): Jose D. Montoya
Expand All @@ -21,36 +21,59 @@
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"

# pylint: disable=too-few-public-methods, invalid-name
# pylint: disable=too-few-public-methods, invalid-name, duplicate-code, too-many-locals, too-many-arguments
class uscatter:
"""
Main class to display different graphics
"""

def __init__(self, plot, x: any, y: any, radius: int = 3) -> None:
def __init__(
self,
plot,
x: any,
y: any,
rangex: any = None,
rangey: any = None,
radius: int = 3,
circle_color=0xFF905D,
) -> None:
"""
:param plot: Plot object for the uscatter to be drawn
:param plot: Plot object for the scatter to be drawn
:param x: x points coordinates
:param y: y points coordinates
:param int radius: circle radius
"""

if rangex is None:
xmin = np.min(x)
xmax = np.max(x)
else:
xmin = min(rangex)
xmax = max(rangex)

if rangey is None:
ymin = np.min(y)
ymax = np.max(y)
else:
ymin = min(rangey)
ymax = max(rangey)

x = np.array(x)
y = np.array(y)

xnorm = np.array(
plot.normalize(np.min(x), np.max(x), plot._newxmin, plot._newxmax, x),
plot.normalize(xmin, xmax, plot._newxmin, plot._newxmax, x),
dtype=np.uint16,
)
ynorm = np.array(
plot.normalize(np.min(y), np.max(y), plot._newymin, plot._newymax, y),
plot.normalize(ymin, ymax, plot._newymin, plot._newymax, y),
dtype=np.uint16,
)

palette = displayio.Palette(1)
palette[0] = 0xFFFFFF
palette[0] = circle_color
for i, _ in enumerate(x):
plot.append(
Circle(pixel_shader=palette, radius=radius, x=xnorm[i], y=ynorm[i])
Expand Down
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ Stackplot simple example
:caption: examples/uplot_stackplot.py
:linenos:
.. image:: ../docs/uplot_ex12.jpg

Advanced Example
---------------------------

plot different ulements in a single display

.. literalinclude:: ../examples/uplot_readme_example.py
:caption: examples/uplot_readme_example.py
:linenos:
.. image:: ../docs/readme.png
Binary file modified docs/readme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions examples/uplot_readme_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

from random import choice
import board
import displayio
from ulab import numpy as np
from circuitpython_uplot.uplot import Uplot
from circuitpython_uplot.ubar import ubar
from circuitpython_uplot.uscatter import uscatter
from circuitpython_uplot.upie import upie
from circuitpython_uplot.ucartesian import ucartesian


display = board.DISPLAY
plot = Uplot(0, 0, display.width, display.height, show_box=False)

group = displayio.Group()

palette = displayio.Palette(1)
palette[0] = 0xFFFFFF


plot2 = Uplot(0, 0, 130, 130)
x = np.linspace(-4, 4, num=25)
constant = 2.0 / np.sqrt(2 * np.pi)
y = constant * np.exp((-(x**2)) / 4.0)
ucartesian(plot2, x, y, rangex=[-5, 5], rangey=[0, 1])
plot.append(plot2)

plot3 = Uplot(130, 0, 160, 160)

# Setting up tick parameters
plot3.tick_params(tickheight=12, tickcolor=0xFF00FF, tickgrid=True)

# Seeting some date to plot
x = np.linspace(-4, 4, num=50)
constant = 1.0 / np.sqrt(2 * np.pi)
y = constant * np.exp((-(x**2)) / 2.0)

# Plotting and showing the plot
ucartesian(plot3, x, y, rangex=[-5, 5], rangey=[0, 0.5])
plot.append(plot3)

plot4 = Uplot(290, 0, 150, 150)

# Setting up tick parameters
plot4.axs_params(axstype="box")
a = ["a", "b", "c", "d"]
b = [3, 5, 1, 7]
ubar(plot4, a, b, 0xFF1000, fill=True)

plot.append(plot4)

# Setting up the display

plot5 = Uplot(0, 180, 120, 120)

# Setting up tick parameters

plot5.axs_params(axstype="cartesian")
a = np.linspace(3, 98)
b = [choice(a) for _ in a]
uscatter(plot5, a, b, rangex=[0, 102], rangey=[0, 102], radius=2)

plot.append(plot5)

plot6 = Uplot(130, 160, 150, 150)

# Setting up tick parameters
plot6.axs_params(axstype="box")
a = [5, 2, 7, 3]

upie(plot6, a, 0, 0)

plot.append(plot6)


plot7 = Uplot(290, 160, 150, 150)

# Creating some points to graph
x = np.linspace(1, 10, num=10)

y = [6, 7, 9, 6, 9, 7, 6, 6, 8, 9]
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0xFF0000, fill=True)

y = [4, 3, 7, 8, 3, 9, 3, 2, 1, 2]
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0xFF00FF, fill=True)

y = [1, 4, 6, 3, 6, 6, 5, 0, 9, 2]
ucartesian(plot7, x, y, rangex=[0, 11], rangey=[0, 12], line_color=0x4444FF, fill=True)

plot.append(plot7)


display.show(plot)

0 comments on commit 3aae5cb

Please sign in to comment.