|
7 | 7 |
|
8 | 8 | from __future__ import annotations
|
9 | 9 |
|
| 10 | +from random import randint |
10 | 11 | from typing import Any, Iterable, overload
|
11 | 12 |
|
12 | 13 | from numpy.typing import NDArray
|
@@ -392,6 +393,27 @@ def plot(self) -> None:
|
392 | 393 | raise ImportError(
|
393 | 394 | "matplotlib not available. Please re-install with 'pip install fuzzylogic[plotting]'"
|
394 | 395 | )
|
| 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) |
395 | 417 |
|
396 | 418 | def array(self) -> Array:
|
397 | 419 | """Return an array of all values for this set within the given domain."""
|
|
0 commit comments