Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you "cache" matplotlib plots and show them dynamically?

I want to create some matplotlib plots in batch, and then display them interactively, e.g something like this? (current code doesn't display the plots)

import matplotlib.pyplot as plt
from ipywidgets import interact
plots = {'a': plt.plot([1,1],[1,2]), 'b': plt.plot([2,2],[1,2])}

def f(x):
    return plots[x]

interact(f, x=['a','b'])  
like image 623
maxymoo Avatar asked Feb 05 '18 22:02

maxymoo


People also ask

Why won't Matplotlib find the correct font for my data?

However, sometimes, Matplotlib won't find the correct, even though it is clearly installed. This happens when Matplotlib's internal font cache is out of date. To refresh the font cache, use

How to refresh the font cache in Matplotlib?

To refresh the font cache, use matplotlib.font_manager._rebuild() Happy Plotting! Tags: python Other posts bastibe.deby Bastian Bechtoldis licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Should the cache be versioned when upgrading Matplotlib?

When upgrading matplotlib, it seems that sometimes the cache needs to be deleted. Otherwise, it becomes unusable and has to be regenerated on every import of matplotlib, which is painfully slow. So, it seems that the cache should be versioned.

How do I save and show a Pyplot at the same time?

Saving figures to file and showing a window at the same time If you want an image file as well as a user interface window, use pyplot.savefig before pyplot.show. At the end of (a blocking) show () the figure is closed and thus unregistered from pyplot. Calling pyplot.savefig afterwards would save a new and thus empty figure.


1 Answers

Redraw figure

Possibly you want something like this, where the figure is cleared on each new selection and the artist in question is readded to the canvas.

%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import interact
plots = {'a': plt.plot([1,1],[1,2]), 'b': plt.plot([2,2],[1,2])}

def f(x):
    plt.gca().clear()
    plt.gca().add_artist(plots[x][0])
    plt.gca().autoscale()
    plt.gcf().canvas.draw_idle()

interact(f, x=['a','b']);

Result in a jupyter notebook:

enter image description here

Blitting

Unfortunately, the notebook backend currently does not support blitting. Using blitting, one could build the the plots beforehands and then just blit them to the axes. This could look like this:

import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons

fig, ax = plt.subplots()
fig.subplots_adjust(left=0.18)
line1, = ax.plot([1,1],[1,2])
line2, = ax.plot([2,2],[1,2], color="crimson")
line2.remove()
fig.canvas.draw()
# store state A, where line1 is present
stateA = fig.canvas.copy_from_bbox(ax.bbox)

line1.remove()
ax.add_artist(line2)
fig.canvas.draw()
# store state B, where line2 is present
stateB = fig.canvas.copy_from_bbox(ax.bbox)


plots = {'a': stateA, 'b': stateB}
rax = plt.axes([0.05, 0.4, 0.1, 0.15])
check = RadioButtons(rax, ('a', 'b'), (False, True))

def f(x):
    fig.canvas.restore_region(plots[x])
    
check.on_clicked(f)

plt.show()

The above runs fine in an normal interactive figure. Once blitting is supported in the notebook backend in some future version of matplotlib, one could replace the RadioButtons and use interact in a notebook.

like image 120
ImportanceOfBeingErnest Avatar answered Oct 15 '22 17:10

ImportanceOfBeingErnest