%matplotlib、%load_ext autoreload 、%autoreload 2 作用

本文解析了在Jupyter Notebook中特有的魔法命令%matplotlib inline和%autoreload的作用,解释了它们为何在Spyder或PyCharm等IDE中会引发语法错误,并提供了在不同环境下正确使用这些命令的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其中%matplotlib、 inline%load_ext autoreload 、%autoreload 2 比较奇怪,而且无论你是用哪个python的IDE如spyder或者pycharm,这个地方都会报错,显示是invalid syntax(无效语法)。

以%matplotlib 为例

1、是在使用jupyter notebook 或者 jupyter qtconsole的时候,才会经常用到%matplotlib,也就是说那一份代码可能就是别人使用jupyter notebook 或者 jupyter qtconsole进行编辑的

import numpy as np
import h5py
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) 
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'


#ipython很好用,但是如果在ipython里已经import过的模块修改后需要重新reload就需要这样
#在执行用户代码前,重新装入软件的扩展和模块。
%load_ext autoreload   
#autoreload 2:装入所有 %aimport 不包含的模块。
%autoreload 2          

np.random.seed(1)      #指定随机种子

2、而%matplotlib具体作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。
而我们在spyder或者pycharm实际运行代码的时候,可以直接注释掉这一句,也是可以运行成功的。如下示例:

import numpy as np
import h5py
import matplotlib.pyplot as plt

#%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) 
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

#ipython很好用,但是如果在ipython里已经import过的模块修改后需要重新reload就需要这样
#在执行用户代码前,重新装入软件的扩展和模块。
#%load_ext autoreload   
#autoreload 2:装入所有 %aimport 不包含的模块。
#%autoreload 2          

np.random.seed(1)      #指定随机种子
Setup cell. import torch import torchvision import numpy as np import random import matplotlib.pyplot as plt from PIL import Image from cs231n.image_utils import SQUEEZENET_MEAN, SQUEEZENET_STD from cs231n.net_visualization_pytorch import * %matplotlib inline plt.rcParams[‘figure.figsize’] = (10.0, 8.0) # Set default size of plots. plt.rcParams[‘image.interpolation’] = ‘nearest’ plt.rcParams[‘image.cmap’] = ‘gray’ %load_ext autoreload %autoreload 2 AttributeError Traceback (most recent call last) Cell In[3], line 3 1 # Setup cell. 2 import torch ----> 3 import torchvision 4 import numpy as np 5 import random File D:\miniconda\lib\site-packages\torchvision_init_.py:6 3 from modulefinder import Module 5 import torch ----> 6 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils 8 from .extension import _HAS_OPS 10 try: File D:\miniconda\lib\site-packages\torchvision_meta_registrations.py:26 20 return fn 22 return wrapper 25 @register_meta(“roi_align”) —> 26 def meta_roi_align(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned): 27 torch._check(rois.size(1) == 5, lambda: “rois must have shape as Tensor[K, 5]”) 28 torch._check( 29 input.dtype == rois.dtype, 30 lambda: ( (…) 33 ), 34 ) File D:\miniconda\lib\site-packages\torchvision_meta_registrations.py:18, in register_meta..wrapper(fn) 17 def wrapper(fn): —> 18 if torchvision.extension._has_ops(): 19 get_meta_lib().impl(getattr(getattr(torch.ops.torchvision, op_name), overload_name), fn) 20 return fn AttributeError: partially initialized module ‘torchvision’ has no attribute ‘extension’ (most likely due to a circular import)
最新发布
07-19
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值