From 6a0cb4bba70708634cd778b9aff32b4dda72af1d Mon Sep 17 00:00:00 2001 From: erdogant Date: Mon, 7 Oct 2024 22:35:10 +0200 Subject: [PATCH] added showplot as parameter too: issue #109 --- bnlearn/bnlearn.py | 22 ++++++++++++---------- bnlearn/examples.py | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/bnlearn/bnlearn.py b/bnlearn/bnlearn.py index ad149a3..bedb727 100644 --- a/bnlearn/bnlearn.py +++ b/bnlearn/bnlearn.py @@ -1137,7 +1137,7 @@ def plot(model, edge_properties=None, edge_labels='weight', params_interactive={'minmax_distance': [100, 250], 'figsize': (1500, 800), 'notebook': False, 'font_color': '#000000', 'bgcolor': '#ffffff', 'show_slider': True, 'filepath': None}, - params_static={'minscale': 1, 'maxscale': 5, 'figsize': (10, 10), 'width': None, 'height': None, 'font_size': 10, 'font_family': 'sans-serif', 'alpha': 0.8, 'node_shape': 'o', 'layout': 'graphviz_layout', 'font_color': '#000000', 'facecolor': 'white', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 20, 'visible': True, 'dpi': 200}, + params_static={'minscale': 1, 'maxscale': 5, 'figsize': (10, 10), 'width': None, 'height': None, 'font_size': 10, 'font_family': 'sans-serif', 'alpha': 0.8, 'node_shape': 'o', 'layout': 'graphviz_layout', 'font_color': '#000000', 'facecolor': 'white', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 20, 'visible': True, 'showplot': True, 'dpi': 200}, verbose=3, ): """Plot the learned stucture. @@ -1235,7 +1235,7 @@ def plot(model, # Plot properties defaults = {'minmax_distance': [100, 250], 'figsize': (1500, 800), 'notebook': False, 'font_color': 'node_color', 'bgcolor': '#ffffff', 'directed': True, 'show_slider': True, 'filepath': None} params_interactive = {**defaults, **params_interactive} - defaults = {'minscale': 1, 'maxscale': 5, 'figsize': (15, 10), 'height': None, 'width': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'layout': 'graphviz_layout', 'font_color': 'k', 'facecolor': '#ffffff', 'node_shape': 'o', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 20, 'visible': True, 'dpi': 200} + defaults = {'minscale': 1, 'maxscale': 5, 'figsize': (15, 10), 'height': None, 'width': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'layout': 'graphviz_layout', 'font_color': 'k', 'facecolor': '#ffffff', 'node_shape': 'o', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 20, 'visible': True, 'showplot': True, 'dpi': 200} params_static = {**defaults, **params_static} # DEPRECATED IN LATER VERSION @@ -1327,6 +1327,7 @@ def plot(model, pos, edge_colors, edge_weights, + showplot=params_static['showplot'], visible=params_static['visible'], title=title, dpi=params_static['dpi'], @@ -1334,17 +1335,17 @@ def plot(model, ) # Store - out['fig']=fig - out['ax']=fig # Should be removed in later releases - out['pos']=pos - out['G']=G - out['node_properties']=node_properties - out['edge_properties']=edge_properties + out['fig'] = fig + out['ax'] = fig # Should be removed in later releases + out['pos'] = pos + out['G'] = G + out['node_properties'] = node_properties + out['edge_properties'] = edge_properties return out # %% Plot interactive -def _plot_static(model, params_static, nodelist, node_colors, node_sizes, G, pos, edge_colors, edge_weights, title, visible=True, dpi=100, edge_labels='weight'): +def _plot_static(model, params_static, nodelist, node_colors, node_sizes, G, pos, edge_colors, edge_weights, title, visible=True, showplot=True, dpi=100, edge_labels='weight'): # Initialize fig = plt.figure(figsize=params_static['figsize'], facecolor=params_static['facecolor'], dpi=dpi) @@ -1376,7 +1377,8 @@ def _plot_static(model, params_static, nodelist, node_colors, node_sizes, G, pos # fig = plt.gca() # fig.set_axis_off() plt.title(title) - if visible: plt.show() + if showplot: + plt.show() # Return return fig diff --git a/bnlearn/examples.py b/bnlearn/examples.py index f6bff15..be51ff2 100644 --- a/bnlearn/examples.py +++ b/bnlearn/examples.py @@ -1,3 +1,26 @@ +# %% Issue #103 + +import bnlearn as bn +import matplotlib.pyplot as plt + +df = bn.import_example('sprinkler') +model = bn.structure_learning.fit(df) + +# Figure is visible and created +fig = bn.plot(model, params_static={'visible': True, 'showplot': True}) + +# Figure is visible but not created +fig = bn.plot(model, params_static={'visible': True, 'showplot': False}) +plt.show() + +# Figure is not visible but but created +fig = bn.plot(model, params_static={'visible': False, 'showplot': True}) + +# Figure is not visible but but created +fig = bn.plot(model, params_static={'visible': False, 'showplot': False}) +plt.show() + +# %% import matplotlib.pyplot as plt import bnlearn as bn