I’ve already got help with this problem, but stuck again.
I’m trying to pre-generate and store matplotlib figures in python, and then display them on a keyboard event (left-right cursor keys).
It is working fine, but will not fit the figures (after the first one) to the window.
How can I do that?
If I manually resize the window, then the figure will be adjusted to fit, but it is not working automatically.
import matplotlib.pyplot as plt
import numpy as np
def new_figure(title, data):
fig,ax = plt.subplots(figsize=(12, 8), dpi=80)
plt.plot(data, label=title)
ax.set_xlabel('x-axis')
ax.set_ylabel('value')
plt.legend()
plt.title(title)
plt.close(fig)
return fig
def show_figure(fig):
dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)
def redraw(event, cnt):
event.canvas.figure = figs[cnt]
event.canvas.mpl_connect('key_press_event', keypress)
event.canvas.draw()
def keypress(event):
global cnt
if event.key == 'right':
cnt += 1
cnt %= mx
elif event.key == 'left':
cnt -= 1
if cnt < 0:
cnt = mx-1
redraw(event, cnt)
d = range(0, 360)
data = []
data.append(np.sin(np.radians(d)))
data.append(np.cos(np.radians(d)))
data.append(np.tan(np.radians(d)))
titles = ['sin','cos','tan']
mx = len(data)
figs = []
for i in range(mx):
fig = new_figure(titles[i], data[i])
figs.append(fig)
cnt = 0
show_figure(figs[0])
figs[0].canvas.mpl_connect('key_press_event', keypress)
plt.show()
1