Skip to content

Commit 6a4ba89

Browse files
committed
nicer plotting
1 parent f2d5d1e commit 6a4ba89

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/fuzzylogic/classes.py

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from random import randint
1011
from typing import Any, Iterable, overload
1112

1213
from numpy.typing import NDArray
@@ -392,6 +393,27 @@ def plot(self) -> None:
392393
raise ImportError(
393394
"matplotlib not available. Please re-install with 'pip install fuzzylogic[plotting]'"
394395
)
396+
R = self.domain.range # e.g., generated via np.linspace(...)
397+
398+
cog_val = self.center_of_gravity()
399+
400+
diffs = np.diff(R)
401+
tol_value = diffs.min() / 100 if len(diffs) > 0 else 1e-6
402+
403+
if all(abs(x - cog_val) >= tol_value for x in R):
404+
R = sorted(set(R).union({cog_val}))
405+
406+
V = [self.func(x) for x in R]
407+
plot_color = "#{:06x}".format(randint(0, 0xFFFFFF))
408+
plt.plot(R, V, label=str(self), color=plot_color, lw=2)
409+
plt.axvline(cog_val, color=plot_color, linestyle="--", linewidth=1.5, label=f"CoG = {cog_val:.2f}")
410+
plt.plot(cog_val, self.func(cog_val), "o", color=plot_color, markersize=8)
411+
412+
plt.title("Fuzzy Set Membership Function")
413+
plt.xlabel("Domain Value")
414+
plt.ylabel("Membership Degree")
415+
plt.legend()
416+
plt.grid(True)
395417

396418
def array(self) -> Array:
397419
"""Return an array of all values for this set within the given domain."""

0 commit comments

Comments
 (0)