twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
1.06k stars 67 forks source link

Matplotlib Embedding fails when being called inside a function #310

Open zhubiii opened 9 months ago

zhubiii commented 9 months ago

On MacOS 13.4.1 Obsidian 1.4.16 execute-code 1.9.0 Python 3.10 Matplotlib 3.6.0

When putting code inside of a function, embedding is throwing an error of <code block>:11: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

For some reason if it is out of a function, it works nominally. If I do matplotlib.use('Tkagg') or any other interactive back end. It will open a separate window and not embed regardless of the embed or notebook setting

def demo():
    sns.set_style("darkgrid")
    iris = sns.load_dataset('iris')
    sns.FacetGrid(iris, hue ="species", height = 5).map(plt.scatter, 'sepal_length', 'petal_length').add_legend()
    plt.show()

Would greatly appreciate a patch or some guidance. Let me know if there is anything I can do