我可以在jupyterlab上的另一个文件中显示一个图吗?
我希望拥有一张交互式地图,您可以单击一旦单击的位置,将绘制Skewt和Hodograph的位置,显示该位置的信息。因此,我创建了一个类,在其中使用Metpy库添加了所有必要的信息,并且能够成功创建这些图表:
我试图导入我创建的类以生成这些图为Jupyterlab的类时的问题。由于实际制作这些图的代码非常麻烦,我宁愿 将代码保留在单独的文件中,并导入我的soundinggraphs
类,但它不起作用。这些图永远不会在单元格内绘制,而是在日志中以警告和信息出现,我不知道为什么:
尝试使用plt.show()
在我的文件中,尝试返回plt
,然后使用plt.show()在笔记本电池单元格中,使用
%matplotlib widget
,%matplotlib笔记本
和%matplotlib inline
,尝试更改jupyterlab版本,无这些改变了任何事情。
我找到了一种我不喜欢的解决方案,但是确实有效,而不是做plt.show()
,而是在我的课堂内执行此操作:
buffer = BytesIO()
plt.savefig(buffer, format='png')
return buffer
在笔记本中,我会做:
image = Image()
display(image)
def on_generate_button_clicked(b):
buffer = SoundingGraphs(infos)
buffer.seek(0)
image.value=buffer.read()
image.format='png'
generate_button.on_click(on_generate_button_clicked)
我不太喜欢这种方法,因为进一步的线路我想在我的情节中添加互动性,例如盘旋时的图案值和类似的事物,因此我不仅想显示图像。因此,我想知道是否确实可以plt.show()
在单元格中另一个文件中创建的绘图。
使用:
Python 3.6.9
jupyterlab==3.2.9
jupyterlab-pygments==0.1.2
jupyterlab-server==2.10.3
jupyterlab-widgets==1.1.0
ipykernel==5.5.6
ipyleaflet==0.14.0
ipympl==0.8.8
ipython==7.16.3
ipython-genutils==0.2.0
ipywidgets==7.7.0
matplotlib==3.3.4
谢谢!
I wish to have an interactive map that you can click where, once clicked, a SkewT and Hodograph will be plotted showing the information for that location. I have thus created a class where I add all the necessary informations using the metpy library and I am able to successfully create these graphs:
The problem comes when I'm trying to import the classes I've created to generate these plots into jupyterlab. Since the code to actually make these plots is quite cumbersome, I'd rather
keep the code in a separate file and import my SoundingGraphs
class, but it's not working. The graphs never get plotted inside a cell, they instead appear in the logs as a Warning and as an Info and I have no idea why:
Tried to use plt.show()
inside my file, tried returning plt
to then use plt.show()
inside a cell of the notebook, tried using %matplotlib widget
, %matplotlib notebook
and %matplotlib inline
, tried changing jupyterlab versions, none of these changed anything.
I have found one solution that I disliked, but that does work, which is rather than doing a plt.show()
, to instead do this inside my class:
buffer = BytesIO()
plt.savefig(buffer, format='png')
return buffer
And in the notebook I would do:
image = Image()
display(image)
def on_generate_button_clicked(b):
buffer = SoundingGraphs(infos)
buffer.seek(0)
image.value=buffer.read()
image.format='png'
generate_button.on_click(on_generate_button_clicked)
I don't quite like this approach because further down the line I would like to add interactivity to my plots, like show values of plot when hovered and things like that, thus I don't just want to show an image. So I'd like to know if it is indeed possible to plt.show()
a plot created inside another file in a cell.
Using:
Python 3.6.9
jupyterlab==3.2.9
jupyterlab-pygments==0.1.2
jupyterlab-server==2.10.3
jupyterlab-widgets==1.1.0
ipykernel==5.5.6
ipyleaflet==0.14.0
ipympl==0.8.8
ipython==7.16.3
ipython-genutils==0.2.0
ipywidgets==7.7.0
matplotlib==3.3.4
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,毕竟有可能!
%matplotlib widtget
需要在笔记本开始时使用,并且由于类方法将从另一个函数中调用(在button.on_click
事件上),所以可能要使用上方的@out.capture()
装饰器,以便显示plt.show()
。也可以使该图成为类属性,以便能够拥有更多的控制。因此,如果有人愿意复制,这里有一些工作代码:
笔记本
myfile.py.py
Yes, it is possible after all!
%matplotlib widget
needs to be used at the start of the notebook and since the class method will be called from another function (on abutton.on_click
event), it is possible to use the@out.capture()
decorator above it so that theplt.show()
gets displayed. It's also possible to make the figure a class attribute to be able to have more control.So here's a bit of working code if someone would like to replicate:
Notebook
myfile.py