700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > TF:jupyter notebook中plt.grid设置后不显示网格以及“TypeError: ‘bool‘ object is not callable”错误

TF:jupyter notebook中plt.grid设置后不显示网格以及“TypeError: ‘bool‘ object is not callable”错误

时间:2019-08-14 02:19:17

相关推荐

TF:jupyter notebook中plt.grid设置后不显示网格以及“TypeError: ‘bool‘ object is not callable”错误

1 问题背景

在TF学习过程中,使用jupyter notebook,plt绘制序列并显示网格。但是在运行过程中,发现,代码可以正常运行,但是网格无法显示。

完整代码如下:

import matplotlib.pyplot as pltimport numpy as npdef plot_series(time, series, format="-", start=0, end=None, label=None):plt.plot(time[start:end], series[start:end], label=label)plt.xlabel("Time")plt.ylabel("Value")if label:plt.legend(fontsize=14)plt.grid=Truetime = np.arange(4 * 100 + 1)series = np.ones((4 * 100 + 1)plt.figure(figsize=(10,6))plot_series(time, series)plt.show()

绘制如下:

网格未显示。

查阅资料后,对plt.grid进行修改

def plot_series(time, series, format="-", start=0, end=None, label=None):plt.plot(time[start:end], series[start:end], label=label)plt.xlabel("Time")plt.ylabel("Value")if label:plt.legend(fontsize=14)plt.grid(True)

或者修改为

plt.grid(b='True')

会出现如下错误:

TypeError: 'bool' object is not callable

2 参考

查阅资料,参考

plt绘图--‘str’ object is not callable 解决_南风慕雨的博客-CSDN博客/weixin_43725328/article/details/107788766matplotlib.pyplot.grid — Matplotlib 3.5.1 documentation/stable/api/_as_gen/matplotlib.pyplot.grid.html感谢南风慕雨。

按照matplotlib文档的说明进行修改。

3 解决方案

重启jupyter notebook内核(很关键)

具体代码修改如下:

plt.grid(visible='True')

需要补充的是,通过如下方式也可以执行网格显示,但是会提醒:

plt.grid(b='True')

C:\Users\P14s\AppData\Local\Temp/ipykernel_7624/682568141.py:7: MatplotlibDeprecationWarning: The 'b' parameter of grid() has been renamed 'visible' since Matplotlib 3.5; support for the old name will be dropped two minor releases later.plt.grid(b='True')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。