2
votes

I tried to run the code below,

import math
import matplotlib.pyplot as plt
from functools import partial

def difference_quotient(f,x,h):
    return(f(x+h)-f(x))/h


def square(x):
    return x*x

def derivative(x):
    return 2*x


derivative_estimate = partial(difference_quotient,square,h=0.0001)
x = range(-10,10)
y = range(-10,10)
plt.title("actual vs estimation")
plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")
plt.show()
print(len(list(map(derivative,x))))

but it shows error below

Traceback (most recent call last): File "C:\Program Files\Python37\lib\site-packages\matplotlib\units.py", line 168, in get_converter if not np.all(xravel.mask): AttributeError: 'numpy.ndarray' object has no attribute 'mask'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\asus\Documents\Sublime\dataScience\gradient.py", line 20, in plt.plot(x,map(derivative,x),'rx',label="actual") File "C:\Program Files\Python37\lib\site-packages\matplotlib\pyplot.py", line 2811, in plot is not None else {}), **kwargs) File "C:\Program Files\Python37\lib\site-packages\matplotlib__init__.py", line 1810, in inner return func(ax, *args, **kwargs) File "C:\Program Files\Python37\lib\site-packages\matplotlib\axes_axes.py", line 1611, in plot for line in self._get_lines(*args, **kwargs): File "C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py", line 393, in _grab_next_args yield from self._plot_args(this, kwargs) File "C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py", line 370, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py", line 205, in _xy_from_xy by = self.axes.yaxis.update_units(y) File "C:\Program Files\Python37\lib\site-packages\matplotlib\axis.py", line 1467, in update_units converter = munits.registry.get_converter(data) File "C:\Program Files\Python37\lib\site-packages\matplotlib\units.py", line 181, in get_converter converter = self.get_converter(next_item) File "C:\Program Files\Python37\lib\site-packages\matplotlib\units.py", line 187, in get_converter thisx = safe_first_element(x) File "C:\Program Files\Python37\lib\site-packages\matplotlib\cbook__init__.py", line 1635, in safe_first_element raise RuntimeError("matplotlib does not support generators " RuntimeError: matplotlib does not support generators as input [Finished in 0.7s]

my suspect was on these line,

plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")

when I tried to change the map(derivative,x) and map(derivative_estimate,x) with y which is range(-10,10), it works.

what should I do so the code can show the plot when I use the map function like above?

2
please place back the original traceback. Use >python /n traceback textZF007

2 Answers

1
votes

You will need to turn the generator into a list of values. E.g. instead of map(func, values), use list(map(func, values)). In your case:

plt.plot(x, list(map(derivative,x)), 'rx', label="actual")
plt.plot(x, list(map(derivative_estimate,x)), 'b+', label="estimate")
0
votes

RuntimeError: matplotlib does not support generators as input

Means you cannot use put as argument something called a python generator
You need to assign the real values of map(derivative,x) and map(derivative_estimate,x) to actual variables.

Try this:

import math
import matplotlib.pyplot as plt
from functools import partial

def difference_quotient(f,x,h):
    return(f(x+h)-f(x))/h


def square(x):
    return x*x

def derivative(x):
    return 2*x

derivative_estimate = partial(difference_quotient,square,h=0.0001)
x = range(-10,10)
y = range(-10,10)

a = map(derivative,x)
b = map(derivative_estimate,x)

plt.title("actual vs estimation")
plt.plot(x,a,'rx',label="actual")
plt.plot(x,b,'b+',label="estimate")
plt.show()
print(len(list(map(derivative,x))))

However, your code is working perfectly fine for me using python 3.4.3 and matplotlib==2.2.4. What version are you using yourself?