Skip to content

Commit

Permalink
added showplot as parameter too: issue #109
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Oct 7, 2024
1 parent 46c3378 commit 6a0cb4b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
22 changes: 12 additions & 10 deletions bnlearn/bnlearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1327,24 +1327,25 @@ def plot(model,
pos,
edge_colors,
edge_weights,
showplot=params_static['showplot'],
visible=params_static['visible'],
title=title,
dpi=params_static['dpi'],
edge_labels=edge_labels,
)

# 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)
Expand Down Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions bnlearn/examples.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 6a0cb4b

Please sign in to comment.