Skip to content

Commit

Permalink
added support for log scale xaxis in plot
Browse files Browse the repository at this point in the history
  • Loading branch information
sspola committed Apr 14, 2022
1 parent 20168d8 commit 0ed1f1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
32 changes: 24 additions & 8 deletions simpful/simpful.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -79,22 +79,33 @@ 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.
Args:
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.
"""
if matplotlib == False:
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:
Expand Down Expand Up @@ -129,24 +140,28 @@ 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.
Args:
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)
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down

0 comments on commit 0ed1f1b

Please sign in to comment.