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?
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.
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.
To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With