如何在Google Colab中使用matplotlib绘制交互式可绘制图像?
我正在尝试绘制一个交互式图像,它可以让我在GoogleColab笔记本的输出中画线。我试着使用下面的代码。它在我本地的Jupyter笔记本中运行良好,但在Google colab中不起作用 有人能建议解决这个问题吗 还尝试添加如何在Google Colab中使用matplotlib绘制交互式可绘制图像?,matplotlib,mouse,draw,interactive,google-colaboratory,Matplotlib,Mouse,Draw,Interactive,Google Colaboratory,我正在尝试绘制一个交互式图像,它可以让我在GoogleColab笔记本的输出中画线。我试着使用下面的代码。它在我本地的Jupyter笔记本中运行良好,但在Google colab中不起作用 有人能建议解决这个问题吗 还尝试添加%matplotlib inline,但显示了静止图像 from matplotlib.lines import Line2D %pylab notebook %matplotlib inline #This is needed for plot widgets cla
%matplotlib inline
,但显示了静止图像
from matplotlib.lines import Line2D
%pylab notebook
%matplotlib inline
#This is needed for plot widgets
class Annotator(object):
def __init__(self, axes):
self.axes = axes
self.xdata = []
self.ydata = []
self.xy = []
self.drawon = False
def mouse_move(self, event):
if not event.inaxes:
return
x, y = event.xdata, event.ydata
if self.drawon:
self.xdata.append(x)
self.ydata.append(y)
self.xy.append((int(x),int(y)))
line = Line2D(self.xdata,self.ydata)
line.set_color('r')
self.axes.add_line(line)
plt.draw()
def mouse_release(self, event):
# Erase x and y data for new line
self.xdata = []
self.ydata = []
self.drawon = False
def mouse_press(self, event):
self.drawon = True
img = np.zeros((28,28,3),dtype='uint8')
fig, axes = plt.subplots(figsize=(3,3))
axes.imshow(img)
plt.axis("off")
plt.gray()
annotator = Annotator(axes)
plt.connect('motion_notify_event', annotator.mouse_move)
plt.connect('button_release_event', annotator.mouse_release)
plt.connect('button_press_event', annotator.mouse_press)
axes.plot()
plt.show()
我希望Google Colab中的输出与我电脑中的Jupyter笔记本一样是交互式的,但输出仍然是图像,无法在上面绘制任何内容。首先,您必须在Google Drive中安装带有目录的Colab。您可以尝试:
fig = plt.figure()
fig.savefig("your_file_name.png")
from IPython.display import Image
Image("your_file_name.png")
这些文件包含。(不确定matplotlib需要什么。)我认为Google Colab不支持任何交互式后端(如%matplotlib笔记本或%matplotlib ipympl)。Jupyter笔记本或jupyterhub可能更适合这种情况。谢谢,@BobSmith,但我需要Colab中的交互式输出,我想这是不可能的。嗨@ImportanceOfBeingErnest,我想利用Colab GPU来维护我的模型。如果Jupyter笔记本或jupyterhub只能提供交互式后端。您能建议我如何将经过培训的Colab模型与Jupyter笔记本连接起来,以便使用
matplotlib
。