diff options
Diffstat (limited to 'cogs/plot.py')
| -rw-r--r-- | cogs/plot.py | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/cogs/plot.py b/cogs/plot.py index 4fcde44..ee19de9 100644 --- a/cogs/plot.py +++ b/cogs/plot.py | |||
| @@ -20,10 +20,10 @@ _plot_available = False | |||
| 20 | 20 | ||
| 21 | def _ensure_plot(): | 21 | def _ensure_plot(): |
| 22 | global _plt_module, _plot_available | 22 | global _plt_module, _plot_available |
| 23 | 23 | ||
| 24 | if _plt_module is not None: | 24 | if _plt_module is not None: |
| 25 | return _plot_available | 25 | return _plot_available |
| 26 | 26 | ||
| 27 | try: | 27 | try: |
| 28 | import matplotlib | 28 | import matplotlib |
| 29 | matplotlib.use("Agg") | 29 | matplotlib.use("Agg") |
| @@ -61,12 +61,12 @@ class Plot(commands.Cog): | |||
| 61 | 61 | ||
| 62 | def _generate_plot(self, func_expr: str, output_path: str): | 62 | def _generate_plot(self, func_expr: str, output_path: str): |
| 63 | global _plt_module | 63 | global _plt_module |
| 64 | 64 | ||
| 65 | if not _ensure_plot(): | 65 | if not _ensure_plot(): |
| 66 | raise RuntimeError("matplotlib not available") | 66 | raise RuntimeError("matplotlib not available") |
| 67 | 67 | ||
| 68 | plt = _plt_module | 68 | plt = _plt_module |
| 69 | 69 | ||
| 70 | if is_implicit(func_expr): | 70 | if is_implicit(func_expr): |
| 71 | self._generate_implicit_plot(func_expr, output_path) | 71 | self._generate_implicit_plot(func_expr, output_path) |
| 72 | return | 72 | return |
| @@ -100,6 +100,10 @@ class Plot(commands.Cog): | |||
| 100 | except Exception: | 100 | except Exception: |
| 101 | y.append(float("nan")) | 101 | y.append(float("nan")) |
| 102 | 102 | ||
| 103 | valid_y = [yi for yi in y if not math.isnan(yi)] | ||
| 104 | if not valid_y: | ||
| 105 | raise ValueError("Brak danych do wykreślenia dla podanej funkcji") | ||
| 106 | |||
| 103 | fig, ax = plt.subplots(figsize=(10, 6)) | 107 | fig, ax = plt.subplots(figsize=(10, 6)) |
| 104 | ax.plot(x, y, linewidth=2, color="#00ff00") | 108 | ax.plot(x, y, linewidth=2, color="#00ff00") |
| 105 | ax.set_title(f"y = {func_expr}", fontsize=14) | 109 | ax.set_title(f"y = {func_expr}", fontsize=14) |
| @@ -117,15 +121,15 @@ class Plot(commands.Cog): | |||
| 117 | def _generate_implicit_plot(self, func_expr: str, output_path: str): | 121 | def _generate_implicit_plot(self, func_expr: str, output_path: str): |
| 118 | global _plt_module | 122 | global _plt_module |
| 119 | plt = _plt_module | 123 | plt = _plt_module |
| 120 | 124 | ||
| 121 | lhs, rhs = parse_implicit(func_expr) | 125 | lhs, rhs = parse_implicit(func_expr) |
| 122 | 126 | ||
| 123 | x_range = 20 | 127 | x_range = 20 |
| 124 | y_range = 20 | 128 | y_range = 20 |
| 125 | 129 | ||
| 126 | x_vals = [] | 130 | x_vals = [] |
| 127 | y_vals = [] | 131 | y_vals = [] |
| 128 | 132 | ||
| 129 | for xi in range(-x_range * 10, x_range * 10 + 1): | 133 | for xi in range(-x_range * 10, x_range * 10 + 1): |
| 130 | x = xi / 10.0 | 134 | x = xi / 10.0 |
| 131 | for yi in range(-y_range * 10, y_range * 10 + 1): | 135 | for yi in range(-y_range * 10, y_range * 10 + 1): |
| @@ -139,7 +143,10 @@ class Plot(commands.Cog): | |||
| 139 | y_vals.append(y) | 143 | y_vals.append(y) |
| 140 | except Exception: | 144 | except Exception: |
| 141 | pass | 145 | pass |
| 142 | 146 | ||
| 147 | if not x_vals or not y_vals: | ||
| 148 | raise ValueError("Brak danych do wykreślenia dla podanej funkcji") | ||
| 149 | |||
| 143 | fig, ax = plt.subplots(figsize=(10, 10)) | 150 | fig, ax = plt.subplots(figsize=(10, 10)) |
| 144 | ax.scatter(x_vals, y_vals, s=1, color="#00ff00", alpha=0.5) | 151 | ax.scatter(x_vals, y_vals, s=1, color="#00ff00", alpha=0.5) |
| 145 | ax.set_title(f"{func_expr}", fontsize=14) | 152 | ax.set_title(f"{func_expr}", fontsize=14) |
| @@ -153,7 +160,7 @@ class Plot(commands.Cog): | |||
| 153 | ax.set_aspect("equal") | 160 | ax.set_aspect("equal") |
| 154 | ax.set_facecolor("#1a1a1a") | 161 | ax.set_facecolor("#1a1a1a") |
| 155 | fig.patch.set_facecolor("#1a1a1a") | 162 | fig.patch.set_facecolor("#1a1a1a") |
| 156 | 163 | ||
| 157 | plt.savefig(output_path, dpi=100, bbox_inches="tight") | 164 | plt.savefig(output_path, dpi=100, bbox_inches="tight") |
| 158 | plt.close() | 165 | plt.close() |
| 159 | 166 | ||
| @@ -185,4 +192,4 @@ class Plot(commands.Cog): | |||
| 185 | 192 | ||
| 186 | 193 | ||
| 187 | def setup(bot): | 194 | def setup(bot): |
| 188 | bot.add_cog(Plot(bot)) \ No newline at end of file | 195 | bot.add_cog(Plot(bot)) |
