Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error with matplotlib axes labels

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?

like image 210
neptune36 Avatar asked Jun 09 '14 12:06

neptune36


People also ask

How do I get rid of Xticks and Yticks in Matplotlib?

To remove the ticks on both the x-axis and y-axis simultaneously, we can pass both left and right attributes simultaneously setting its value to False and pass it as a parameter inside the tick_params() function. It removes the ticks on both x-axis and the y-axis simultaneously.

How do I change axis labels in Pyplot?

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

How do I get rid of Xlabel in Matplotlib?

To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.

What does %Matplotlib mean in Python?

What Does Matplotlib Mean? Matplotlib is a plotting library available for the Python programming language as a component of NumPy, a big data numerical handling resource. Matplotlib uses an object oriented API to embed plots in Python applications.


1 Answers

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.

like image 161
wolfins Avatar answered Oct 24 '22 03:10

wolfins