Skip to content

Commit 5f8ca00

Browse files
committed
adding tick format option and example
1 parent 8fdac51 commit 5f8ca00

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

circuitpython_uplot/plot.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def __init__(
109109

110110
self._cartesianfirst = True
111111
self._loggingfirst = True
112+
self._scatterfirst = True
112113

113114
self._showtext = False
114115

@@ -127,7 +128,7 @@ def __init__(
127128

128129
self._index_colorused = 4
129130

130-
self._plotbitmap = displayio.Bitmap(width, height, 20)
131+
self._plotbitmap = displayio.Bitmap(width, height, 10)
131132

132133
if show_box:
133134
self._drawbox()
@@ -323,7 +324,10 @@ def _draw_ticks(self, x: int, y: int) -> None:
323324
)
324325
if self._showtext:
325326
self.show_text(
326-
"{:.2f}".format(ticksxnorm[i]), tick, self._newymin, (0.5, 0.0)
327+
"{:.{}f}".format(ticksxnorm[i], self._decimal_points),
328+
tick,
329+
self._newymin,
330+
(0.5, 0.0),
327331
)
328332
for i, tick in enumerate(ticksyrenorm):
329333
draw_line(
@@ -336,7 +340,10 @@ def _draw_ticks(self, x: int, y: int) -> None:
336340
)
337341
if self._showtext:
338342
self.show_text(
339-
"{:.2f}".format(ticksynorm[i]), self._newxmin, tick, (1.0, 0.5)
343+
"{:.{}f}".format(ticksynorm[i], self._decimal_points),
344+
self._newxmin,
345+
tick,
346+
(1.0, 0.5),
340347
)
341348
for tick in subticksxrenorm:
342349
draw_line(
@@ -369,26 +376,34 @@ def tick_params(
369376
tickcolor: int = 0xFFFFFF,
370377
tickgrid: bool = False,
371378
showtext: bool = False,
379+
decimal_points: int = 0,
372380
) -> None:
373381
"""
374382
Function to set ticks parameters
375383
384+
:param bool show_ticks: Show ticks. Defaults to `True`
376385
:param int tickx_height: X axes tick height in pixels. Defaults to 8
377386
:param int ticky_height: Y axes tick height in pixels. Defaults to 8
378387
:param int tickcolor: tick color in hex. Defaults to white. ``0xFFFFFF``
379388
:param bool tickgrid: defines if the grid is to be shown. Defaults to `False`
380389
:param bool showtext: Show Axes text. Defaults to `False`
390+
:param int decimal_points: Number of decimal points to show. Defaults to 0
381391
382392
:return: None
383393
384394
"""
395+
if showtext and self.padding < 20:
396+
raise ValueError(
397+
"Please select a padding that allows to show the tick text"
398+
)
385399

386400
self._showticks = show_ticks
387401
self._tickheightx = tickx_height
388402
self._tickheighty = ticky_height
389403
self._plot_palette[2] = tickcolor
390404
self._tickgrid = tickgrid
391405
self._showtext = showtext
406+
self._decimal_points = decimal_points
392407
if self._showtext:
393408
from adafruit_display_text import bitmap_label
394409

@@ -460,6 +475,7 @@ def show_text(
460475
:param Tuple anchorpoint: Display_text anchor point. Defaults to (0.5, 0.0)
461476
:return: None
462477
"""
478+
463479
if self._showtext:
464480
text_toplot = self.bitmap_label.Label(terminalio.FONT, text=text, x=x, y=y)
465481
text_toplot.anchor_point = anchorpoint

circuitpython_uplot/scatter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ def __init__(
7474
else:
7575
self._pointer = pointer
7676

77+
7778
self._radius = radius
7879
self._pointer_color = pointer_color
7980

81+
if isinstance(self._radius, list) and self._pointer != "circle":
82+
raise ValueError(f"Pointer paramater is {self._pointer}. Variable Radius are not accepted")
83+
8084
if rangex is None:
8185
xmin = np.min(x) - nudge_factor * (abs(np.max(x) - np.min(x)) / 10)
8286
xmax = np.max(x) + nudge_factor * (abs(np.max(x) - np.min(x)) / 10)
@@ -105,8 +109,12 @@ def __init__(
105109

106110
self._draw_pointer(plot)
107111

108-
if plot._showticks:
109-
plot._draw_ticks(x, y)
112+
if plot._scatterfirst:
113+
if plot._showticks:
114+
plot._draw_ticks(x, y)
115+
116+
plot._scatterfirst = False
117+
plot._showticks = False
110118

111119
def _draw_pointer(self, plot: Plot) -> None:
112120
"""

docs/quick_start.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ the following parameters:
123123
:param int ticky_height: ticky_height in pixels
124124
:param int tickcolor: tickcolor in Hex format
125125
:param bool tickgrid: displays the tickgrid. Defaults to `False`
126+
:param bool showtext: displays the tick text. Defaults to `False`
127+
:param int decimal_points: number of decimal points to show. Defaults to `0`
126128

127129
.. code-block:: python
128130
@@ -136,6 +138,20 @@ Gridlines are normally ``OFF``. If you want visible gridlines then use:
136138
plot.tick_params(tickgrid=True)
137139
138140
141+
If you want to show the axes text. You can use:
142+
143+
.. code-block:: python
144+
145+
plot.tick_params(showtext=True)
146+
147+
If you want to show some decimal places in the text. use:
148+
149+
.. code-block:: python
150+
151+
plot.tick_params(decimal_points=2)
152+
153+
154+
139155
Colors
140156
===============
141157
You can choose some colors directly from the library. This can be done by importing the color class:
99.7 KB
Loading
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
from ulab import numpy as np
8+
from circuitpython_uplot.plot import Plot
9+
from circuitpython_uplot.scatter import Scatter
10+
11+
12+
# Setting up the display
13+
display = board.DISPLAY
14+
15+
# Adding the plot area
16+
plot = Plot(0, 0, display.width, display.height, padding=25)
17+
plot.tick_params(
18+
tickx_height=12,
19+
ticky_height=12,
20+
tickcolor=0x939597,
21+
tickgrid=True,
22+
showtext=True,
23+
decimal_points=0,
24+
)
25+
26+
display.show(plot)
27+
28+
a = np.linspace(4, 200, 50)
29+
z = [4, 5, 6, 7, 8]
30+
radi = [choice(z) for _ in a]
31+
b = [choice(a) for _ in a]
32+
Scatter(
33+
plot, a, b, rangex=[0, 210], rangey=[0, 210], radius=radi, pointer_color=0xF456F3
34+
)
35+
a = np.linspace(50, 170, 50)
36+
radi = [choice(z) for _ in a]
37+
b = [choice(a) for _ in a]
38+
Scatter(
39+
plot, a, b, rangex=[0, 210], rangey=[0, 210], radius=radi, pointer_color=0x00FF00
40+
)
41+
a = np.linspace(50, 100, 25)
42+
z = [
43+
4,
44+
5,
45+
6,
46+
]
47+
radi = [choice(z) for _ in a]
48+
b = [int(choice(a) / 1.2) for _ in a]
49+
Scatter(
50+
plot,
51+
a,
52+
b,
53+
rangex=[0, 210],
54+
rangey=[0, 210],
55+
pointer="triangle",
56+
pointer_color=0x00FFFF,
57+
)

0 commit comments

Comments
 (0)