python - 如何将matplotlib对象传递/返回python中的函数
问题描述
我正在尝试创建一个包含简单函数的模块,用于创建已经应用了一些常见格式的绘图。其中一些函数将应用于已经存在的 matplotlib 对象,并将其他 matplotlib 对象返回到主程序。
第一段代码是我当前如何生成绘图的示例,它按原样工作。
# Include relevant python libraries
from matplotlib import pyplot as plt
# Define plot formatting
axesSize = [0, 0, 1, 1]
axesStyle = ({'facecolor':(0.95, 0.95, 0.95)})
gridStyle = ({'color':'k',
'linestyle':':',
'linewidth':1})
xString = "Independent Variable"
xLabelStyle = ({'fontsize':18,
'color':'r'})
# Create figure and axes objects with appropriate style
figureHandle = plt.figure()
axesHandle = figureHandle.add_axes(axesSize, **axesStyle)
axesHandle.grid(**gridStyle)
axesHandle.set_xlabel(xString, **xLabelStyle)
我想创建一个将 add_axes() 命令与 grid() 和 set_xlabel() 命令结合起来的函数。作为第一次尝试,忽略所有样式,我在我的 NTPlotTools.py 模块中提出了以下函数。
def CreateAxes(figureHandle, **kwargs):
axesHandle = figureHandle.add_axes()
return axesHandle
调用函数的脚本如下所示:
# Include relevant python libraries
from matplotlib import pyplot as plt
from importlib.machinery import SourceFileLoader as fileLoad
# Include module with my functions
pathName = "/absolute/file/path/NTPlotTools.py"
moduleName = "NTPlotTools.py"
pt = fileLoad(moduleName, pathName).load_module()
# Define plot formatting
gridStyle = ({'color':'k',
'linestyle':':',
'linewidth':1})
# Create figure and axes objects with appropriate style
figureHandle = plt.figure()
axesHandle = pt.CreateAxes(figureHandle)
axesHandle.grid(**gridStyle)
但是,当我运行主代码时,我收到以下错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-73802a54b21a> in <module>()
17 axesHandle = pt.CreateAxes(figureHandle)
18
---> 19 axesHandle.grid(**gridStyle)
AttributeError: 'NoneType' object has no attribute 'grid'
这对我来说,axesHandle 不是 matplotlib 轴对象,并且通过扩展, CreateAxes() 函数调用没有返回 matplotlib 轴对象。将 matplotlib 对象传入/传出函数有技巧吗?
解决方案
你快到了。问题在于这条线。
def CreateAxes(figureHandle, **kwargs)
axesHandle = figureHandle.add_axes() # Here
return axesHandle
从方法看起来source code
如下add_axes
def add_axes(self, *args, **kwargs):
if not len(args):
return
# rest of the code ...
因此,当您在figureHandle.add_axes()
没有任何参数的情况下调用两者args
时都kwrags
将为空。从源代码中 ifargs
为空add_axes
方法返回None
。因此,此None
值被分配给axesHandle
,当您尝试调用时,axesHandle.grid(**gridStyle)
您将获得
AttributeError: 'NoneType' object has no attribute 'grid'
例子
>>> def my_demo_fun(*args, **kwrags):
... if not len(args):
... return
... return args
...
>>> print(my_demo_fun())
None
>>> print(my_demo_fun(1, 2))
(1, 2)
add_axes
因此,通过将参数传递给方法来重新编写函数。
def create_axes(figure_handle, **kwargs):
axes_handle = figure_handle.add_axes(axes_size, **axes_style)
return axes_handle
推荐阅读
- group-by - DAX:列是否包含特定值,使用 GROUP BY
- ios - UICollectionview 自定尺寸单元格自动布局,间距恒定
- ignite - 设置 loadBalancingSpi 后,Ignite Cluster 无法启动
- php - 如何在本周和下周按日期对帖子进行排序
- .net-core - 如何告诉 Swashbuckle 5 在 dotnetcore 3.0 中需要正文内容?
- bash - bash 读取:`vs.txt':不是有效的标识符
- javascript - 错误 - 对象作为 React 子项无效
- javascript - 为什么 ` 与 JavaScript 中的 ' 不同?
- java - 如何将活动 UI 的点击传递到地图片段以将地图更改为 MAP_TYPE_HYBRID
- c# - C# Selenium Chrome 单击默认 chrome 主页中的链接