Python 3 matplotlib does not support generators as input - python

I am fairly new to Python and it took me hours to figure out what I can do with no success. I am trying an example in plotting from https://jupyter.brynmawr.edu/services/public/dblank/jupyter.cs/FLAIRS-2015/TSPv3.ipynb#Python-and-iPython-Notebook:-Preliminaries. In the section on "Plotting Tours", as labeled as cell 15,16,17,18 and maybe more. The function for plot_tour and plot_lines are using map() and I read that in Python 3 it's a generator instead of a list. This made it confusing so is there any way around this to output the plot? which possibly not using map(). Thank you so much in advance.
First plot code:
def plot_tour(tour):
"Plot the cities as circles and the tour as lines between them."
plot_lines(list(tour) + [tour[0]])
def plot_lines(points, style='bo-'):
"Plot lines to connect a series of points."
plt.plot(map(X, points), map(Y, points), style)
plt.axis('scaled'); plt.axis('off')
plot_tour(alltours_tsp(Cities(8)))
This is the error im getting:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 plot_tour(alltours_tsp(Cities(8)))
Input In [17], in plot_tour(tour)
1 def plot_tour(tour):
2 "Plot the cities as circles and the tour as lines between them."
----> 3 plot_lines(list(tour) + [tour[0]])
Input In [17], in plot_lines(points, style)
5 def plot_lines(points, style='bo-'):
6 "Plot lines to connect a series of points."
----> 7 plt.plot(map(X, points), map(Y, points), style)
8 plt.axis('scaled'); plt.axis('off')
File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:2757, in plot(scalex, scaley, data, *args, **kwargs)
2755 #_copy_docstring_and_deprecators(Axes.plot)
2756 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2757 return gca().plot(
2758 *args, scalex=scalex, scaley=scaley,
2759 **({"data": data} if data is not None else {}), **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
1390 """
1391 Plot y versus x as lines and/or markers.
1392
(...)
1629 (``'green'``) or hex strings (``'#008000'``).
1630 """
1631 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632 lines = [*self._get_lines(*args, data=data, **kwargs)]
1633 for line in lines:
1634 self.add_line(line)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
310 this += args[0],
311 args = args[1:]
--> 312 yield from self._plot_args(this, kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:493, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
490 x, y = index_of(xy[-1])
492 if self.axes.xaxis is not None:
--> 493 self.axes.xaxis.update_units(x)
494 if self.axes.yaxis is not None:
495 self.axes.yaxis.update_units(y)
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1443, in Axis.update_units(self, data)
1437 def update_units(self, data):
1438 """
1439 Introspect *data* for units converter and update the
1440 axis.converter instance if necessary. Return *True*
1441 if *data* is registered for unit conversion.
1442 """
-> 1443 converter = munits.registry.get_converter(data)
1444 if converter is None:
1445 return False
File ~\anaconda3\lib\site-packages\matplotlib\units.py:206, in Registry.get_converter(self, x)
202 else:
203 # ... and avoid infinite recursion for pathological iterables for
204 # which indexing returns instances of the same iterable class.
205 if type(first) is not type(x):
--> 206 return self.get_converter(first)
207 return None
File ~\anaconda3\lib\site-packages\matplotlib\units.py:199, in Registry.get_converter(self, x)
197 pass
198 try: # If cache lookup fails, look up based on first element...
--> 199 first = cbook.safe_first_element(x)
200 except (TypeError, StopIteration):
201 pass
File ~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py:1678, in safe_first_element(obj)
1676 except TypeError:
1677 pass
-> 1678 raise RuntimeError("matplotlib does not support generators "
1679 "as input")
1680 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input
Next plot code:
def plot_tsp(algorithm, cities):
"Apply a TSP algorithm to cities, plot the resulting tour, and print information."
# Find the solution and time how long it takes
t0 = time.clock()
tour = algorithm(cities)
t1 = time.clock()
assert valid_tour(tour, cities)
plot_tour(tour); plt.show()
print("{} city tour with length {:.1f} in {:.3f} secs for {}"
.format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
def valid_tour(tour, cities):
"Is tour a valid tour for these cities?"
return set(tour) == set(cities) and len(tour) == len(cities)
plot_tsp(alltours_tsp, Cities(8))
This is the error im getting:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 plot_tsp(alltours_tsp, Cities(8))
Input In [22], in plot_tsp(algorithm, cities)
6 t1 = time.perf_counter()
7 assert valid_tour(tour, cities)
----> 8 plot_tour(tour); plt.show()
9 print("{} city tour with length {:.1f} in {:.3f} secs for {}"
10 .format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
Input In [17], in plot_tour(tour)
1 def plot_tour(tour):
2 "Plot the cities as circles and the tour as lines between them."
----> 3 plot_lines(list(tour) + [tour[0]])
Input In [17], in plot_lines(points, style)
5 def plot_lines(points, style='bo-'):
6 "Plot lines to connect a series of points."
----> 7 plt.plot(map(X, points), map(Y, points), style)
8 plt.axis('scaled'); plt.axis('off')
File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:2757, in plot(scalex, scaley, data, *args, **kwargs)
2755 #_copy_docstring_and_deprecators(Axes.plot)
2756 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2757 return gca().plot(
2758 *args, scalex=scalex, scaley=scaley,
2759 **({"data": data} if data is not None else {}), **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
1390 """
1391 Plot y versus x as lines and/or markers.
1392
(...)
1629 (``'green'``) or hex strings (``'#008000'``).
1630 """
1631 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632 lines = [*self._get_lines(*args, data=data, **kwargs)]
1633 for line in lines:
1634 self.add_line(line)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
310 this += args[0],
311 args = args[1:]
--> 312 yield from self._plot_args(this, kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:493, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
490 x, y = index_of(xy[-1])
492 if self.axes.xaxis is not None:
--> 493 self.axes.xaxis.update_units(x)
494 if self.axes.yaxis is not None:
495 self.axes.yaxis.update_units(y)
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1443, in Axis.update_units(self, data)
1437 def update_units(self, data):
1438 """
1439 Introspect *data* for units converter and update the
1440 axis.converter instance if necessary. Return *True*
1441 if *data* is registered for unit conversion.
1442 """
-> 1443 converter = munits.registry.get_converter(data)
1444 if converter is None:
1445 return False
File ~\anaconda3\lib\site-packages\matplotlib\units.py:206, in Registry.get_converter(self, x)
202 else:
203 # ... and avoid infinite recursion for pathological iterables for
204 # which indexing returns instances of the same iterable class.
205 if type(first) is not type(x):
--> 206 return self.get_converter(first)
207 return None
File ~\anaconda3\lib\site-packages\matplotlib\units.py:199, in Registry.get_converter(self, x)
197 pass
198 try: # If cache lookup fails, look up based on first element...
--> 199 first = cbook.safe_first_element(x)
200 except (TypeError, StopIteration):
201 pass
File ~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py:1678, in safe_first_element(obj)
1676 except TypeError:
1677 pass
-> 1678 raise RuntimeError("matplotlib does not support generators "
1679 "as input")
1680 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input

I suspect this notebook was made for an earlier version of python and packages. That's why you're having so many problems. I recommend tracking down some specifications of what python and package versions were used in the course. However, I did get a lot of the notebook to run with these changes:
def plot_lines(points, style='bo-'):
"Plot lines to connect a series of points."
plt.plot(list(map(X, points)), list(map(Y, points)), style)
plt.axis('scaled'); plt.axis('off')
Later on, time.time() should be replaced with perf_counter().
def plot_tsp(algorithm, cities):
"Apply a TSP algorithm to cities, plot the resulting tour, and print information."
# Find the solution and time how long it takes
t0 = time.perf_counter()
tour = algorithm(cities)
t1 = time.perf_counter()
assert valid_tour(tour, cities)
plot_tour(tour); plt.show()
print("{} city tour with length {:.1f} in {:.3f} secs for {}"
.format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
def benchmark(function, inputs):
"Run function on all the inputs; return pair of (average_time_taken, results)."
t0 = time.perf_counter()
results = map(function, inputs)
t1 = time.perf_counter()
average_time = (t1 - t0) / len(inputs)
return (average_time, results)
There are other changes that have to be made further down the notebook, however. I had problems with the benchmark function and the USA big map url request. You should really consider digging the package versions though.

Related

Scipy Fourier Transform KeyError: 'ALIGNED'?

I'm trying to run a fast fourier transform on a pandas dataframe that I have. I am using the Kepler exoplanet dataset, here, and a specific notebook for it, here. I recreate the code in cells 27-30 (Note that the code in cell 29 is executed elsewhere, thus both dataframes have the same shape as the original notebook), which looks as follows:
import scipy
def spectrum_getter(X):
Spectrum = scipy.fft.fft(X, n=X.size)
return np.abs(Spectrum)
x_train_OS_FT = x_train_OS.apply(spectrum_getter, axis=1)
x_test_FT = x_test.apply(spectrum_getter, axis=1)
Both x_train_OS and x_test are pandas.core.frame.DataFrame. Running this produces:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [245], in <module>
----> 1 x_train_OS_FT = x_train_OS.apply(spectrum_getter, axis=1)
2 x_test_FT = x_test.apply(spectrum_getter, axis=1)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py:8827, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
8816 from pandas.core.apply import frame_apply
8818 op = frame_apply(
8819 self,
8820 func=func,
(...)
8825 kwargs=kwargs,
8826 )
-> 8827 return op.apply().__finalize__(self, method="apply")
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:727, in FrameApply.apply(self)
724 elif self.raw:
725 return self.apply_raw()
--> 727 return self.apply_standard()
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:851, in FrameApply.apply_standard(self)
850 def apply_standard(self):
--> 851 results, res_index = self.apply_series_generator()
853 # wrap results
854 return self.wrap_results(results, res_index)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:867, in FrameApply.apply_series_generator(self)
864 with option_context("mode.chained_assignment", None):
865 for i, v in enumerate(series_gen):
866 # ignore SettingWithCopy here in case the user mutates
--> 867 results[i] = self.f(v)
868 if isinstance(results[i], ABCSeries):
869 # If we have a view on v, we need to make a copy because
870 # series_generator will swap out the underlying data
871 results[i] = results[i].copy(deep=False)
Input In [244], in spectrum_getter(X)
3 def spectrum_getter(X):
----> 4 Spectrum = scipy.fft.fft(X, n=X.size)
5 return np.abs(Spectrum)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_backend.py:22, in _ScipyBackend.__ua_function__(method, args, kwargs)
20 if fn is None:
21 return NotImplemented
---> 22 return fn(*args, **kwargs)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_pocketfft\basic.py:17, in c2c(forward, x, n, axis, norm, overwrite_x, workers, plan)
14 if plan is not None:
15 raise NotImplementedError('Passing a precomputed plan is not yet '
16 'supported by scipy.fft functions')
---> 17 tmp = _asfarray(x)
18 overwrite_x = overwrite_x or _datacopied(tmp, x)
19 norm = _normalization(norm, forward)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_pocketfft\helper.py:97, in _asfarray(x)
95 dtype = x.dtype.newbyteorder('=')
96 # Always align input
---> 97 copy = not x.flags['ALIGNED']
98 return np.array(x, dtype=dtype, copy=copy)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\flags.py:98, in Flags.__getitem__(self, key)
96 def __getitem__(self, key):
97 if key not in self._keys:
---> 98 raise KeyError(key)
100 return getattr(self, key)
KeyError: 'ALIGNED'
I attempted to convert the dataframe to a numpy array, but ran into other issues. What am I doing wrong here?
I ran to the same error so I converted my datatype to dataframe and it solved my problem.

Why am I getting 'x and y must have same first dimension, but have shapes (1,) and (319,)' when the x and y shapes are same?

I am using the lmfit package to fit a double exponential function to my data. I am first taking in the x and y coordinate values from a CSV file and plotting them. It works fine. Secondly, I am defining a double_exponential function, and using the lmfit package, I am plotting the double_exponential function against my data.
I have printed out the length of my x and y values (length=319) and also its shapes to see if they match (319,). They do!. But I am still getting an 'x and y must have the same first dimension, but have shapes (1,) and (319,) ' error. I have attached the code below. Any suggestions in resolving this problem would help.
xData=[]
yData=[]
for file in files:
basename = os.path.basename(file)
file_name = os.path.splitext(basename)[0]
# print(file_name)
#File_List.append(file_name)
with open(file, "r") as f_in:
reader = csv.reader(f_in)
next(reader)
next(reader)
for line in reader:
try:
float_1, float_2 = float(line[0]), float(line[1])
xData=np.append(xData,float_1)
yData=np.append(yData,float_2)
except ValueError:
continue
xData = np.array(xData)
yData = np.array(yData)
plt.plot(xData,yData)
plt.show()
def double_exp(a,t,T):
return a*np.exp(-t/T)
print(xData.shape)
print(yData.shape)
mod = Model(double_exp)
result = mod.fit(yData,x=xData, a=1, t=1, T=1)
result.plot()
xData=[]
yData=[]
My traceback error is:
(319,)
(319,)
C:\ANACONDA\lib\site-packages\lmfit\model.py:968: UserWarning: The keyword argument x does not match any arguments of the model function. It will be ignored.
warnings.warn("The keyword argument %s does not " % name +
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-085a1a8ec72a> in <module>
35 mod = Model(double_exp)
36 result = mod.fit(yData,x=xData, a=1, t=1, T=1)
---> 37 result.plot()
38
39
C:\ANACONDA\lib\site-packages\lmfit\model.py in wrapper(*args, **kws)
48 #wraps(function)
49 def wrapper(*args, **kws):
---> 50 return function(*args, **kws)
51 return wrapper
52
C:\ANACONDA\lib\site-packages\lmfit\model.py in plot(self, datafmt, fitfmt, initfmt, xlabel, ylabel, yerr, numpoints, fig, data_kws, fit_kws, init_kws, ax_res_kws, ax_fit_kws, fig_kws, show_init, parse_complex)
2115 ax_fit = fig.add_subplot(gs[1], sharex=ax_res, **ax_fit_kws)
2116
-> 2117 self.plot_fit(ax=ax_fit, datafmt=datafmt, fitfmt=fitfmt, yerr=yerr,
2118 initfmt=initfmt, xlabel=xlabel, ylabel=ylabel,
2119 numpoints=numpoints, data_kws=data_kws,
C:\ANACONDA\lib\site-packages\lmfit\model.py in wrapper(*args, **kws)
48 #wraps(function)
49 def wrapper(*args, **kws):
---> 50 return function(*args, **kws)
51 return wrapper
52
C:\ANACONDA\lib\site-packages\lmfit\model.py in plot_fit(self, ax, datafmt, fitfmt, initfmt, xlabel, ylabel, yerr, numpoints, data_kws, fit_kws, init_kws, ax_kws, show_init, parse_complex)
1890 fmt=datafmt, label='data', **data_kws)
1891 else:
-> 1892 ax.plot(x_array, reduce_complex(self.data),
1893 datafmt, label='data', **data_kws)
1894
C:\ANACONDA\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1741 """
1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)]
1744 for line in lines:
1745 self.add_line(line)
C:\ANACONDA\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
271 this += args[0],
272 args = args[1:]
--> 273 yield from self._plot_args(this, kwargs)
274
275 def get_next_color(self):
C:\ANACONDA\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
397
398 if x.shape[0] != y.shape[0]:
--> 399 raise ValueError(f"x and y must have same first dimension, but "
400 f"have shapes {x.shape} and {y.shape}")
401 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (1,) and (319,)
the error message tells you exactly what the problem is:
UserWarning: The keyword argument x does not match any arguments of the model function. It will be ignored.
You supply a value for x=xData to the fit method, but your function doubl_exp does not contain this variable. Here, as in the various other questions you asked about lmfit, the best advice is: read the documentation and look at the provided examples on the website. They'll likely cover >95% of your use-case and after that reading the error message Python gives you is always helpful ;)

How to plot SymPy's built-in functions using sympy.plotting

Hey I am pretty new to SymPy. I am trying to use SymPy's built-in plotting to plot some functions. My codes are as follows:
from sympy import Symbol, sin, Function, pi,exp
from sympy.plotting import plot
t = Symbol('t')
source = t**2
plot(source,(t,0,1))
This works perfectly. However, notice that I have also imported functions such as sin and exp above. However, I am finding anything involving these functions, SymPy will not plot. For example,
source1 = exp(t)
source2= sin(t)
plot(source1,(t,0,1))
plot(source2,(t,0,1))
Neither of these will work. The error I received is as follows, using the case with sin as an example:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args, kwargs)
194 #The result can be sympy.Float. Hence wrap it with complex type.
--> 195 result = complex(self.lambda_func(args))
196 if abs(result.imag) > 1e-7 * abs(result):
TypeError: complex() first argument must be a string or a number, not 'FunctionClass'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-45-85c4f08c25e4> in <module>
1 f0 = 15
2 source = sin(t)
----> 3 plot(exp(t),(t,0,1))
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in plot(*args, **kwargs)
1556 plots = Plot(*series, **kwargs)
1557 if show:
-> 1558 plots.show()
1559 return plots
1560
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in show(self)
189 self._backend.close()
190 self._backend = self.backend(self)
--> 191 self._backend.show()
192
193 def save(self, path):
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in show(self)
1246
1247 def show(self):
-> 1248 self.process_series()
1249 #TODO after fixing https://github.com/ipython/ipython/issues/1255
1250 # you can uncomment the next line and remove the pyplot.show() call
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in process_series(self)
1243 if isinstance(self.parent, PlotGrid):
1244 parent = self.parent.args[i]
-> 1245 self._process_series(series, ax, parent)
1246
1247 def show(self):
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in _process_series(self, series, ax, parent)
1072 # Create the collections
1073 if s.is_2Dline:
-> 1074 collection = self.LineCollection(s.get_segments())
1075 ax.add_collection(collection)
1076 elif s.is_contour:
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in get_segments(self)
661 list_segments.append([p, q])
662
--> 663 f_start = f(self.start)
664 f_end = f(self.end)
665 sample(np.array([self.start, f_start]),
/usr/local/lib/python3.6/dist-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args, kwargs)
234 ' problematic. We are trying a failback method'
235 ' that may still work. Please report this as a bug.')
--> 236 if abs(result.imag) > 1e-7 * abs(result):
237 return None
238 else:
AttributeError: type object 'exp' has no attribute 'imag'
This might be something trivial, but as a beginner I am having quite a hard time figuring these out. Really appreciate your help!!

TypeError: unhashable type: 'numpy.ndarray' when trying to create scatter plot from dataset

I am trying to create a scatter plot using a dataset on movies. The goal is to look at the correlation between the different categories and the target variable, whether or not the movie won an award. I have tried doing a type call on my variables, and neither of them appear to be of type numpy.ndarray as they are both pandas dataframes, yet I still get the following error when I try to create a scatter plot:
TypeError: unhashable type: 'numpy.ndarray'
My code is as follows:
import pandas as pd
import matplotlib.pyplot as plt
file=pd.read_csv('academy_awards.csv',sep=',',error_bad_lines=False,encoding="ISO 8859-1")
print(file)
df=pd.DataFrame(file)
#df=df.dropna(axis=0,how='any')
target=df.Category
X=pd.DataFrame(df.Won)
y=target
#print(type(X))
#print(type(y))
plt.scatter(X,y)
The following are the first 5 lines of the dataset I am using:
Year,Category,Nominee,Additional Info,Won
2010 (83rd),Actor -- Leading Role,Javier Bardem,Biutiful
{'Uxbal'},NO
2010 (83rd),Actor -- Leading Role,Jeff Bridges,True Grit {'Rooster
Cogburn'},NO
2010 (83rd),Actor -- Leading Role,Jesse Eisenberg,The Social
Network {'Mark Zuckerberg'},NO
2010 (83rd),Actor -- Leading Role,Colin Firth,The King's Speech
{'King George VI'},YES
2010 (83rd),Actor -- Leading Role,James Franco,127 Hours {'Aron
Ralston'},NO
2010 (83rd),Actor -- Supporting Role,Christian Bale,The Fighter
{'Dicky Eklund'},YES
Any help or suggestions are greatly appreciated!
Edit:
The following is the full traceback--
-----------------------------------------------------------------------
TypeError Traceback (most recent call
last)
<ipython-input-211-efcb7c41bca1> in <module>
14 print(y.shape)
15
---> 16 plt.scatter(X,y)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap,
norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
2862 vmin=vmin, vmax=vmax, alpha=alpha,
linewidths=linewidths,
2863 verts=verts, edgecolors=edgecolors, **({"data": data}
if data
-> 2864 is not None else {}), **kwargs)
2865 sci(__ret)
2866 return __ret
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer,
func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker,
cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4170 edgecolors = 'face'
4171
-> 4172 self._process_unit_info(xdata=x, ydata=y,
kwargs=kwargs)
4173 x = self.convert_xunits(x)
4174 y = self.convert_yunits(y)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/axes/_base.py in _process_unit_info(self, xdata,
ydata, kwargs)
2133 return kwargs
2134
-> 2135 kwargs = _process_single_axis(xdata, self.xaxis,
'xunits', kwargs)
2136 kwargs = _process_single_axis(ydata, self.yaxis,
'yunits', kwargs)
2137 return kwargs
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/axes/_base.py in _process_single_axis(data, axis,
unit_name, kwargs)
2116 # We only need to update if there is nothing
set yet.
2117 if not axis.have_units():
-> 2118 axis.update_units(data)
2119
2120 # Check for units in the kwargs, and if present
update axis
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/axis.py in update_units(self, data)
1471 neednew = self.converter != converter
1472 self.converter = converter
-> 1473 default = self.converter.default_units(data, self)
1474 if default is not None and self.units is None:
1475 self.set_units(default)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/category.py in default_units(data, axis)
101 # default_units->axis_info->convert
102 if axis.units is None:
--> 103 axis.set_units(UnitData(data))
104 else:
105 axis.units.update(data)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/category.py in __init__(self, data)
167 self._counter = itertools.count()
168 if data is not None:
--> 169 self.update(data)
170
171 def update(self, data):
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/matplotlib/category.py in update(self, data)
184 data = np.atleast_1d(np.array(data, dtype=object))
185
--> 186 for val in OrderedDict.fromkeys(data):
187 if not isinstance(val, (str, bytes)):
188 raise TypeError("{val!r} is not a
string".format(val=val))
TypeError: unhashable type: 'numpy.ndarray'
First, you don't need to: df=pd.DataFrame(file). After opening the CSV file with pandas and saved in the file variable, you already get the data as dataFrame.
Then, you can easily call the scatter and choose the x-axis and y-axis with
df.plot(kind ="scatter", x= "Won", y = "Category")
You don't need to preprocess the data, because of it's already preprocessed after opened the file with pandas.
Arrays are unhashable because they're mutable. You can hash it by converting it to an immutable tuple (by wrapping it with tuple()) but you usually shouldn't be trying to hash arrays anyways. Your data is probably of the wrong shape.

Value Error: x and y must have the same first dimension

Let me quickly brief you first, I am working with a .txt file with 5400 data points. Each is a 16 second average over a 24 hour period (24 hrs * 3600 s/hr = 86400...86400/16 = 5400). In short this is the average magnetic strength in the z direction for an inbound particle field curtsy of the Advanced Composition Experiment satellite. Data publicly available here. So when I try to plot it says the error
Value Error: x and y must have the same first dimension
So I created a numpy lin space of 5400 points broken apart by 16 units. I did this because I thought that my dimensions didn't match with my previous array that I had defined. But now I am sure these two array's are of the same dimension and yet it still gives back that Value Error. The code is as follows:
First try (without the linspace):
import numpy as np
import matplotlib as plt
Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)
Start_ACE = dt.date(2015,12,2)
Finish_ACE = dt.date(2015,12,2)
dt_Mag = 16
time_Mag = np.arange(Start_ACE, Finish_ACE, dt_Mag)
plt.subplot(3,1,1)
plt.plot(time_Mag, Bz)
plt.title('Bz 2015 12 02')
Second Try (with linspace):
import numpy as np
import matplotlib as plt
Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)
Mag_time = np.linspace(0,5399,16, dtype = float)
plt.subplot(3,1,1)
plt.plot(Mag_time, Bz)
plt.title('Bz 2015 12 02')
Other than it being a dimensional problem I don't know what else could be holding back this plotting procedure back.
Full traceback:
ValueError Traceback (most recent call last)
<ipython-input-68-c5dc0bdf5117> in <module>()
1 plt.subplot(3,1,1)
----> 2 plt.plot(Mag_time, Bz)
3 plt.title('Bz 2015 12 02')
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
3152 ax.hold(hold)
3153 try:
-> 3154 ret = ax.plot(*args, **kwargs)
3155 finally:
3156 ax.hold(washold)
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1809 warnings.warn(msg % (label_namer, func.__name__),
1810 RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
1812 pre_doc = inner.__doc__
1813 if pre_doc is None:
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
1422 kwargs['color'] = c
1423
-> 1424 for line in self._get_lines(*args, **kwargs):
1425 self.add_line(line)
1426 lines.append(line)
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
384 return
385 if len(remaining) <= 3:
--> 386 for seg in self._plot_args(remaining, kwargs):
387 yield seg
388 return
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
362 x, y = index_of(tup[-1])
363
--> 364 x, y = self._xy_from_xy(x, y)
365
366 if self.command == 'plot':
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
221 y = _check_1d(y)
222 if x.shape[0] != y.shape[0]:
--> 223 raise ValueError("x and y must have same first dimension")
224 if x.ndim > 2 or y.ndim > 2:
225 raise ValueError("x and y can be no greater than 2-D")
ValueError: x and y must have same first dimension
The problem was the selection of array creation. Instead of linspace, I should have used arange.
Mag_time = np.arange(0,86400, 16, dtype = float)

Categories

Resources