matplotlib轴标签出现奇怪错误

时间:2022-09-25 23:43:39

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.

我对Python和编程很新,所以如果我遗漏了一些明显的东西,请提前道歉。我正在尝试绘制图形并标记轴,但每次我尝试标记y轴时都会引发异常。我在新脚本中编写了下面的代码,以确保问题不是来自模块中的其他位置。我正在使用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")

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?

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

7 个解决方案

#1


7  

I'm afraid I can't tell you what's going wrong because it works fine here. The below code runs without error and shows the plot with correct label.

我担心我不能告诉你出了什么问题,因为它在这里工作得很好。以下代码运行时没有错误,并显示带有正确标签的图。

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

If that doesn't work for you, perhaps you can try using figure and axes objects, like this

如果这对您不起作用,也许您可​​以尝试使用图形和轴对象,就像这样

from matplotlib.pyplot import subplots, show
a = [1, 2, 3, 4, 5]
b = [2, 3, 2, 3, 2]
fig, ax = subplots()
ax.plot(a, b)
ax.set_xlabel("Time")
ax.set_ylabel("Speed")
show()

Doesn't solve the underlying problem (which is hard since I can't reproduce it), but maybe it will achieve your purpose at least.

没有解决潜在的问题(这很难,因为我不能重现它),但也许它至少会实现你的目的。

#2


65  

I had this same issue when working in iPython notebook.

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

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

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

重新启动内核或重新导入库会将plt.ylabel恢复为函数。

#3


4  

I just had this happen to me. It seems that what I did was assign a string to xlab and ylab like:

我发生了这件事。似乎我所做的是为xlab和ylab分配一个字符串,如:

plt.xlab = 'string'
plt.ylab = 'string'

This broke the xlab and ylab such that you cannot call them anymore, since they are now in fact strings, not functions.

这破坏了xlab和ylab,使你无法再调用它们,因为它们现在实际上是字符串,而不是函数。

Similarly, I was using Jupyter, and I had to kill Jupyter and re-run it from scratch to fix this problem.

同样地,我正在使用Jupyter,我不得不杀死Jupyter并从头开始重新运行以解决这个问题。

Oddly, re-importing the libraries while the kernal remained running did NOT work.

奇怪的是,在内核仍在运行时重新导入库不起作用。

#4


2  

I saved the checkpoint and halted the ipython notebook. Then I restarted it which solved the problem.

我保存了检查点并停止了ipython笔记本。然后我重新启动它解决了问题。

#5


1  

I have faced this same issue in jupyter notebook so if you are too facing that just restart the kernel and that would fix it , there isnt any syntactical mistake in your code .

我在jupyter笔记本中遇到了同样的问题,所以如果你太面对只是重新启动内核而且会修复它,你的代码中没有任何语法错误。

#6


0  

This usually Happens, if you assign the Xlabel value instead calling . ex: if you want to set the Xlabel to "X-DUMMY". you need to use

如果您指定Xlabel值而不是调用,这通常会发生。例如:如果你想将Xlabel设置为“X-DUMMY”。你需要使用

plt.xlabel("X-DUMMY")

but by mistake if you do as below. plt.xlabel= "X-DUMMY" you will get this error . Even if you correct it saying . plt.xlabel("X-DUMMY").

但如果你这样做,那就错了。 plt.xlabel =“X-DUMMY”你会得到这个错误。即使你纠正它说。 plt.xlabel( “X-DUMMY”)。

This issue repeats unless you restart you restart the kernal .

除非重新启动重新启动核心,否则会重复此问题。

reason being, plt.xlabel is a function. in python functions are first-class objects.once you assigned plt.xlabel= "X-DUMMY". it get converted to a string.later when ever you try ,it throws error as 'TypeError: 'str' object is not callable'.

原因是,plt.xlabel是一个函数。在python函数中是第一类对象。你分配了plt.xlabel =“X-DUMMY”。它会在你尝试时转换为string.later,因为'TypeError:'str'对象不可调用'而抛出错误。

you can try it by using "type(plt.xlabel)" before and after assignment.to see its datatype.

你可以在赋值之前和之后使用“type(plt.xlabel)”来试试它。看看它的数据类型。

#7


-1  

if you are on win probably you didn't install the proper version of matplotlib

如果你在胜利,可能你没有安装正确版本的matplotlib

you need to be careful which version of python you have and that version of matplotlib you need to have on your computer

你需要注意你拥有哪个版本的python以及你需要在你的计算机上使用的matplotlib版本

download: https://pypi.python.org/pypi/matplotlib/ for windows users: python -m pip install --user matplotlib-2.1.0-cp36-cp36m-win_amd64.whl

下载:https://pypi.python.org/pypi/matplotlib/ for windows users:python -m pip install --user matplotlib-2.1.0-cp36-cp36m-win_amd64.whl

in the file name you will se cp36 == python 3.6, cp27 == python 2.7, always first check which version of python you have on the computer

在文件名中你将cp36 == python 3.6,cp27 == python 2.7,总是首先检查你在计算机上有哪个版本的python

#1


7  

I'm afraid I can't tell you what's going wrong because it works fine here. The below code runs without error and shows the plot with correct label.

我担心我不能告诉你出了什么问题,因为它在这里工作得很好。以下代码运行时没有错误,并显示带有正确标签的图。

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

If that doesn't work for you, perhaps you can try using figure and axes objects, like this

如果这对您不起作用,也许您可​​以尝试使用图形和轴对象,就像这样

from matplotlib.pyplot import subplots, show
a = [1, 2, 3, 4, 5]
b = [2, 3, 2, 3, 2]
fig, ax = subplots()
ax.plot(a, b)
ax.set_xlabel("Time")
ax.set_ylabel("Speed")
show()

Doesn't solve the underlying problem (which is hard since I can't reproduce it), but maybe it will achieve your purpose at least.

没有解决潜在的问题(这很难,因为我不能重现它),但也许它至少会实现你的目的。

#2


65  

I had this same issue when working in iPython notebook.

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

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

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

重新启动内核或重新导入库会将plt.ylabel恢复为函数。

#3


4  

I just had this happen to me. It seems that what I did was assign a string to xlab and ylab like:

我发生了这件事。似乎我所做的是为xlab和ylab分配一个字符串,如:

plt.xlab = 'string'
plt.ylab = 'string'

This broke the xlab and ylab such that you cannot call them anymore, since they are now in fact strings, not functions.

这破坏了xlab和ylab,使你无法再调用它们,因为它们现在实际上是字符串,而不是函数。

Similarly, I was using Jupyter, and I had to kill Jupyter and re-run it from scratch to fix this problem.

同样地,我正在使用Jupyter,我不得不杀死Jupyter并从头开始重新运行以解决这个问题。

Oddly, re-importing the libraries while the kernal remained running did NOT work.

奇怪的是,在内核仍在运行时重新导入库不起作用。

#4


2  

I saved the checkpoint and halted the ipython notebook. Then I restarted it which solved the problem.

我保存了检查点并停止了ipython笔记本。然后我重新启动它解决了问题。

#5


1  

I have faced this same issue in jupyter notebook so if you are too facing that just restart the kernel and that would fix it , there isnt any syntactical mistake in your code .

我在jupyter笔记本中遇到了同样的问题,所以如果你太面对只是重新启动内核而且会修复它,你的代码中没有任何语法错误。

#6


0  

This usually Happens, if you assign the Xlabel value instead calling . ex: if you want to set the Xlabel to "X-DUMMY". you need to use

如果您指定Xlabel值而不是调用,这通常会发生。例如:如果你想将Xlabel设置为“X-DUMMY”。你需要使用

plt.xlabel("X-DUMMY")

but by mistake if you do as below. plt.xlabel= "X-DUMMY" you will get this error . Even if you correct it saying . plt.xlabel("X-DUMMY").

但如果你这样做,那就错了。 plt.xlabel =“X-DUMMY”你会得到这个错误。即使你纠正它说。 plt.xlabel( “X-DUMMY”)。

This issue repeats unless you restart you restart the kernal .

除非重新启动重新启动核心,否则会重复此问题。

reason being, plt.xlabel is a function. in python functions are first-class objects.once you assigned plt.xlabel= "X-DUMMY". it get converted to a string.later when ever you try ,it throws error as 'TypeError: 'str' object is not callable'.

原因是,plt.xlabel是一个函数。在python函数中是第一类对象。你分配了plt.xlabel =“X-DUMMY”。它会在你尝试时转换为string.later,因为'TypeError:'str'对象不可调用'而抛出错误。

you can try it by using "type(plt.xlabel)" before and after assignment.to see its datatype.

你可以在赋值之前和之后使用“type(plt.xlabel)”来试试它。看看它的数据类型。

#7


-1  

if you are on win probably you didn't install the proper version of matplotlib

如果你在胜利,可能你没有安装正确版本的matplotlib

you need to be careful which version of python you have and that version of matplotlib you need to have on your computer

你需要注意你拥有哪个版本的python以及你需要在你的计算机上使用的matplotlib版本

download: https://pypi.python.org/pypi/matplotlib/ for windows users: python -m pip install --user matplotlib-2.1.0-cp36-cp36m-win_amd64.whl

下载:https://pypi.python.org/pypi/matplotlib/ for windows users:python -m pip install --user matplotlib-2.1.0-cp36-cp36m-win_amd64.whl

in the file name you will se cp36 == python 3.6, cp27 == python 2.7, always first check which version of python you have on the computer

在文件名中你将cp36 == python 3.6,cp27 == python 2.7,总是首先检查你在计算机上有哪个版本的python