Strange error with matplotlib axes labels
Asked Answered
C

8

72

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

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?

Corkhill answered 9/6, 2014 at 12:10 Comment(2)
I was unable to reproduce this issue. Your code runs fine for me, with those strings in either the xlabel or ylabel interchangeably.Marquetry
I solved the same issue by opening a fresh terminal and rerunning the codeFreedom
P
194

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

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

Pearce answered 25/11, 2015 at 19:51 Comment(2)
I find the fact that so many people had exactly this error pretty funny :))Hornbeam
Good answer; However, I found re-importing the library doesn't fix when using iPython. I had to restart the kernel.Flowerless
F
10

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

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.

Fasciate answered 9/6, 2014 at 12:32 Comment(3)
Thank you, that works. As for my code, after all that, restarting the interpreter has done the trick. Sorry about that!Corkhill
@Corkhill Maybe best change the accepted answer, wolfins seems to have managed to break this in the same way so that's probably more usefulFasciate
Thanks for tagging me. I had no idea this 5 year old question had gotten so much attention!Corkhill
J
8

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

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.

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

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

Jellify answered 28/11, 2017 at 16:22 Comment(0)
A
7

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

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= "X-DUMMY" 

it get converted to a string. later when ever you try, it throws error as

'TypeError: 'str' object is not callable'.

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

Assumptive answered 15/9, 2018 at 15:41 Comment(0)
G
3

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

Gauthier answered 5/7, 2017 at 17:28 Comment(0)
S
2

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 .

Susceptive answered 5/7, 2018 at 3:1 Comment(0)
H
-1

This might help you:

# 0. Import matplotlib and get it ready
%matplotlib inline
import matplotlib.pyplot as plt

# 1. Prepare data
x = [1,2,3,4]
y = [10,20,30,40]

# 2. Setup plot
fig, ax = plt.subplots(figsize=(8,8)) # width & height 

# 3. Plot the data
ax.plot(x,y)

# 4. Customize Plot
ax.set(title="Simple Plot", 
        xlabel="X - Put your X label TITLE here",
        ylabel="Y - Put your y label TITLE here")


# 5. Display
plt.show()
Howardhowarth answered 17/9, 2020 at 6:16 Comment(1)
Code answers are fine, but it is best if you include some description of why your code works. You also aren't actually helping anyone. The question had an error in it, you haven't explained why that happened or what in your code fixes to change that.Mesosphere
T
-2

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

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

download: 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

Thermodynamics answered 18/11, 2017 at 22:18 Comment(1)
This is completely unrelated to the question.Hawkinson

© 2022 - 2024 — McMap. All rights reserved.