Matplotlib轴标签出现奇怪的错误 [英] Strange error with matplotlib axes labels

查看:136
本文介绍了Matplotlib轴标签出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般对Python和编程都不熟悉,所以如果我遗漏了一些明显的东西,请提前道歉.我试图绘制图形并标记轴,但是每次尝试标记y轴时都会引发异常.我在下面的新脚本中编写了代码,以确保问题不是来自模块中的其他地方.我正在使用Python 3.4.

I'm very new to Python and programming in general, so apologies in advance if I'm missing something obvious. I'm trying to plot a graph and label the axes, but every time I try to label the y axis an exception is raised. I wrote the code below in a new script to make sure the problem wasn't coming from somewhere else in the module. I'm using Python 3.4.

from numpy import *
from matplotlib import *

a = [1, 2, 3, 4, 5]
b = [2, 3, 2, 3, 2]
pyplot.plot(a, b)
pylab.xlabel("Time")
pylab.ylabel("Speed")

每次,我都会在最后一行收到错误"TypeError:'str'对象不可调用".如果我将y更改为x,一切都很好.如果将x更改为y,则会收到相同的错误.但是,ylabel出现在ylabel的下拉列表中,因此该函数确实存在,并且文档说一个字符串是唯一必需的参数,与xlabel(matplotlib.pyplot.ylabel(s,* args,** kwargs)和matplotlib.pyplot.xlabel(s,* args,** kwargs)).这到底是怎么回事?

Every time, I get the error 'TypeError: 'str' object is not callable' for the final line. If I change the y to an x, everything is fine. If I change the x to a y, I get the same error. However, ylabel comes up on the drop down list for ylabel so the function does exist and the documentation says a string is the only necessary argument, exactly as for xlabel (matplotlib.pyplot.ylabel(s, *args, **kwargs) and matplotlib.pyplot.xlabel(s, *args, **kwargs)). What on earth could be going on here?

推荐答案

在iPython笔记本中工作时,我遇到了同样的问题.

I had this same issue when working in iPython notebook.

我认为可以如下重新创建它:

I think it can be re-created as follows:

import matplotlib.pyplot as plt
plt.ylabel = 'somestring' # oh wait this isn't the right syntax.
... 
plt.ylabel('somestring') # now this breaks because the function has been turned into a string

重新启动内核或重新导入库会将plt.ylabel恢复为功能.

Re-starting the kernel or re-importing the libraries restores plt.ylabel to a function.

这篇关于Matplotlib轴标签出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆