Skip to content

Commit

Permalink
adding logging animation example
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Jul 27, 2023
1 parent 663de78 commit 55a516b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
9 changes: 3 additions & 6 deletions circuitpython_uplot/ulogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from circuitpython_uplot.uplot import Uplot
except ImportError:
pass
import math
from bitmaptools import draw_line, fill_region
from ulab import numpy as np

Expand Down Expand Up @@ -112,9 +113,7 @@ def _draw_ticks(self, plot) -> None:
2,
)
if plot._showtext:
plot.show_text(
"{:.2f}".format(ticksxnorm[i]), tick, plot._newymin, (0.5, 0.0)
)
plot.show_text(f"{self.ticksx[i]:.0f}", tick, plot._newymin, (0.5, 0.0))
for i, tick in enumerate(ticksynorm):
draw_line(
plot._plotbitmap,
Expand All @@ -125,9 +124,7 @@ def _draw_ticks(self, plot) -> None:
2,
)
if plot._showtext:
plot.show_text(
"{:.2f}".format(ticksynorm[i]), plot._newxmin, tick, (1.0, 0.5)
)
plot.show_text(f"{self.ticksy[i]:.0f}", plot._newxmin, tick, (1.0, 0.5))

@staticmethod
def clear_plot(plot) -> None:
Expand Down
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ This example shows how to add a data table to the plot
.. image:: ../docs/logging_table.jpg


Logging Animation Example
---------------------------------------

This example shows how to animate a plot

.. literalinclude:: ../examples/uplot_ulogging_animation.py
:caption: examples/uplot_ulogging_animation.py
:lines: 5-

SVG Images examples
---------------------------

Expand Down
90 changes: 90 additions & 0 deletions examples/uplot_ulogging_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
#
# SPDX-License-Identifier: MIT


import time
import random
import board
from circuitpython_uplot.uplot import Uplot, color
from circuitpython_uplot.ulogging import ulogging

# Setting up the display
display = board.DISPLAY
display.auto_refresh = False

# Drawing the graph
my_plot = Uplot(
140,
60,
200,
200,
padding=1,
show_box=True,
box_color=color.WHITE,
)

# Setting the tick parameters
my_plot.tick_params(
tickx_height=4,
ticky_height=4,
show_ticks=True,
tickcolor=color.TEAL,
showtext=True,
)

# Creating the x and y data
x = [
10,
20,
30,
40,
50,
60,
70,
80,
90,
100,
110,
120,
130,
140,
150,
160,
170,
180,
190,
]
y = [26, 22, 24, 30, 28, 35, 26, 25, 24, 23, 20, 27, 26, 33, 24, 23, 19, 27, 26]

# Creating the random numbers
random_numbers = [19, 22, 35, 33, 24, 26, 28, 37]


display.show(my_plot)
display.refresh()

dist = 1

# Creating the loggraph
my_loggraph = ulogging(
my_plot,
x[0:dist],
y[0:dist],
rangex=[0, 210],
rangey=[0, 110],
line_color=color.BLUE,
ticksx=[25, 50, 75, 100, 125, 150, 175, 200],
ticksy=[25, 50, 75, 100],
)

# Showing the loggraph
while True:
if dist > len(x):
y.pop(0)
y.append(random.choice(random_numbers))

my_loggraph.draw_points(my_plot, x[0:dist], y[0:dist])
display.refresh()
dist += 1
time.sleep(0.5)

0 comments on commit 55a516b

Please sign in to comment.