From 0ed1f1b84416470e6a85d69fead1b7757e78aaed Mon Sep 17 00:00:00 2001 From: Simone Spolaor Date: Thu, 14 Apr 2022 16:04:01 +0200 Subject: [PATCH] added support for log scale xaxis in plot --- setup.py | 2 +- simpful/simpful.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index 811aa4b..63e96a4 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name = 'simpful', packages = ['simpful'], # this must be the same as the name above - version = '2.6.0', + version = '2.6.1', description = 'A user-friendly Python library for fuzzy logic', author = 'Marco S. Nobile', author_email = 'marco.nobile@unive.it', diff --git a/simpful/simpful.py b/simpful/simpful.py index 2293689..dd824f1 100644 --- a/simpful/simpful.py +++ b/simpful/simpful.py @@ -1,6 +1,6 @@ from .fuzzy_sets import FuzzySet, MF_object, Triangular_MF, SingletonsSet from .rule_parsing import recursive_parse, preparse, postparse -from numpy import array, linspace +from numpy import array, linspace, logspace, log10, finfo, float64 from scipy.interpolate import interp1d from copy import deepcopy from collections import defaultdict, OrderedDict @@ -79,7 +79,7 @@ def get_universe_of_discourse(self): return min(mins), max(maxs) - def draw(self, ax, TGT=None, highlight=None): + def draw(self, ax, TGT=None, highlight=None, xscale="linear"): """ This method returns a matplotlib ax, representing all fuzzy sets contained in the liguistic variable. @@ -87,6 +87,7 @@ def draw(self, ax, TGT=None, highlight=None): ax: the matplotlib axis to plot to. TGT: show the memberships of a specific element of discourse TGT in the figure. highlight: string, indicating the linguistic term/fuzzy set to highlight in the plot. + xscale: default "linear", supported scales "log". Changes the scale of the xaxis. Returns: A matplotlib axis, representing all fuzzy sets contained in the liguistic variable. """ @@ -94,7 +95,17 @@ def draw(self, ax, TGT=None, highlight=None): raise Exception("ERROR: please, install matplotlib for plotting facilities") mi, ma = self.get_universe_of_discourse() - x = linspace(mi, ma, 10000) + if xscale == "linear": + x = linspace(mi, ma, 10000) + elif xscale == "log": + if mi < 0: + raise Exception("ERROR: cannot plot in log scale with negative universe of discourse") + elif mi == 0: + x = logspace(log10(finfo(float64).eps), log10(ma), 10000) + else: + x = logspace(log10(mi), log10(ma), 10000) + else: + raise Exception("ERROR: scale "+xscale+" not supported.") if highlight is None: @@ -129,11 +140,14 @@ def draw(self, ax, TGT=None, highlight=None): ax.set_xlabel(self._concept) ax.set_ylabel("Membership degree") ax.set_ylim(bottom=-0.05) + if xscale == "log": + ax.set_xscale("symlog") + ax.set_xlim(x[0], x[-1]) if highlight is None: ax.legend(loc="best") return ax - def plot(self, outputfile="", TGT=None, highlight=None): + def plot(self, outputfile="", TGT=None, highlight=None, xscale="linear"): """ Shows a plot representing all fuzzy sets contained in the liguistic variable. @@ -141,12 +155,13 @@ def plot(self, outputfile="", TGT=None, highlight=None): outputfile: path and filename where the plot must be saved. TGT: show the memberships of a specific element of discourse TGT in the figure. highlight: string, indicating the linguistic term/fuzzy set to highlight in the plot. + xscale: default "linear", supported scales "log". Changes the scale of the xaxis. """ if matplotlib == False: raise Exception("ERROR: please, install matplotlib for plotting facilities") fig, ax = subplots(1,1) - self.draw(ax=ax, TGT=TGT, highlight=highlight) + self.draw(ax=ax, TGT=TGT, highlight=highlight, xscale=xscale) if outputfile != "": fig.savefig(outputfile) @@ -707,7 +722,7 @@ def inference(self, terms=None, subdivisions=1000, ignore_errors=False, ignore_w raise Exception("ERROR: simpful could not detect the model type, please use either Sugeno_inference() or Mamdani_inference() methods.") - def plot_variable(self, var_name, outputfile="", TGT=None, highlight=None, ax=None): + def plot_variable(self, var_name, outputfile="", TGT=None, highlight=None, ax=None, xscale="linear"): """ Plots all fuzzy sets contained in a liguistic variable. Options for saving the figure and draw on a matplotlib ax are provided. @@ -717,14 +732,15 @@ def plot_variable(self, var_name, outputfile="", TGT=None, highlight=None, ax=No TGT: a specific element of the universe of discourse to be highlighted in the figure. highlight: string, indicating the linguistic term/fuzzy set to highlight in the plot. ax: a matplotlib ax where the variable will be plotted. + xscale: default "linear", supported scales "log". Changes the scale of the xaxis. """ if matplotlib == False: raise Exception("ERROR: please, install matplotlib for plotting facilities") if ax != None: - ax = self._lvs[var_name].draw(ax=ax, TGT=TGT, highlight=highlight) + ax = self._lvs[var_name].draw(ax=ax, TGT=TGT, highlight=highlight, xscale=xscale) return ax - self._lvs[var_name].plot(outputfile=outputfile, TGT=TGT, highlight=highlight) + self._lvs[var_name].plot(outputfile=outputfile, TGT=TGT, highlight=highlight, xscale=xscale) def produce_figure(self, outputfile="", max_figures_per_row=4):