Python matplotlib 轴标签的奇怪错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24120023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:01:37  来源:igfitidea点击:

Strange error with matplotlib axes labels

pythonmatplotlibaxes

提问by neptune36

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' object is not callable'。如果我将 y 更改为 x,则一切正常。如果我将 x 更改为 ay,则会出现相同的错误。但是, ylabel 出现在 ylabel 的下拉列表中,因此该函数确实存在,并且文档说字符串是唯一必要的参数,与 xlabel 完全一样 (matplotlib.pyplot.ylabel(s, *args, **kwargs) 和matplotlib.pyplot.xlabel(s, *args, **kwargs))。这到底是怎么回事?

采纳答案by wolfins

I had this same issue when working in iPython notebook.

我在 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

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

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

回答by Mark

EDIT: This code works fine for clean runs, but you might have changed ylabel, in which case restarting should fix it, as @wolfins answered (check that answer).

编辑:此代码适用于干净运行,但您可能已更改ylabel,在这种情况下重新启动应该修复它,正如@wolfins回答(检查该答案)

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.

不能解决根本问题(这很难,因为我无法重现它),但也许它至少可以达到您的目的。

回答by Mreno sert

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

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

回答by Slavo7

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/ Windows 用户: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

在文件名中,您将 se cp36 == python 3.6, cp27 == python 2.7,始终首先检查计算机上的 python 版本

回答by Jordan

I just had this happen to me. It seems that what I did was assigna 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 NOTwork.

奇怪的是,重新导入库,而籽粒仍然运行做工作。

回答by sayantan ghosh

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 notebook 中也遇到过同样的问题,所以如果你也遇到过这样的问题,只需重新启动内核就可以修复它,你的代码中没有任何语法错误。

回答by Venkat Rajanala

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") 

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 是一个函数。在 python 函数中是一流的对象。一旦你分配

 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'.

'TypeError: 'str' 对象不可调用'。

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

您可以通过使用type(plt.xlabel)before 和 after assignment来尝试。查看它的数据类型。