我有多個對象可以可視化不同的事物。它們都被傳遞到單個 Figure 實例的引用并創建自己的軸對象(同時只有一個處于活動狀態)演示代碼是:import numpy as npimport matplotlib.pyplot as plt# the visualization objects are jupyter widgets in production code%matplotlib widgetplt.ioff()def update_fig(f): f.canvas.draw() f.canvas.flush_events()f = plt.figure()# first visualizer is created and plots into the figureax1 = f.add_subplot(1,1,1, label="visualizer_1")img1 = np.random.randint(0, 256, (20, 20, 3))vis1 = ax1.imshow(img1)update_fig(f)# second visualizer is created and plots into the figureax2 = f.add_subplot(1,1,1, label="visualizer_2")vis2 = ax2.plot(np.linspace(0, 5), 2 * np.linspace(0, 5))update_fig(f)然后如何切換要在圖中顯示的軸?已經嘗試plt.sca(ax1)返回到第一個可視化,但沒有奏效。當然,我可以為每個可視化對象使用不同的圖形,但由于我有很多,我寧愿避免這種情況。
添加回答
舉報
0/150
提交
取消