DBSCAN plot - The color values passed in plt.plot() is throwing ValueError - numpy

I am using DBSCAN to perform clustering on a dataset. I think it is because of the color argument passed to the markerfacecolor in plt.plot() which is not a single value. Please let me know if am wrong here. My features are latitude, longitude,speed_mph,speedlimit_mph,vehicle_id,driver_id.
Here is my clustering code
dbsc = DBSCAN(eps = .5, min_samples = 5).fit(df_cont)
labels = dbsc.labels_
print(labels)
num_clusters = len(set(labels))
clusters = pd.Series([df_cont[labels == n] for n in range(num_clusters)])
print('Number of clusters: {}'.format(num_clusters))
# No of clusters : 5687
core_samples = np.zeros_like(labels, dtype = bool)
core_samples[dbsc.core_sample_indices_] = True
unique_labels = np.unique(labels)
colors = plt.cm.Spectral(np.linspace(0,1, len(unique_labels)))
for (label, color) in zip(unique_labels, colors):
class_member_mask = (labels == label)
xy = df_cont[class_member_mask & core_samples]
print("color:",color)
# color: [ 0.61960784 0.00392157 0.25882353 1. ]
plt.plot(xy.values[:,0],xy.values[:,1], marker='o', markerfacecolor = color, markersize = 10)
xy2 = df_cont[class_member_mask & ~core_samples]
plt.plot(xy2.values[:,0],xy2.values[:,1], 'o', markerfacecolor = color, markersize = 5)
plt.title("DBSCAN Driver - Speed MPH")
plt.xlabel("driver")
plt.ylabel("Speed")
plt.show()
Here is the error message thrown
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-105-0192647e6baf> in <module>()
3 xy = df_cont[class_member_mask & core_samples]
4 print("color:",color)
----> 5 plt.plot(xy.values[:,0],xy.values[:,1], marker='o', markerfacecolor = color, markersize = 10)
6
7 xy2 = df_cont[class_member_mask & ~core_samples]
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
3315 mplDeprecation)
3316 try:
-> 3317 ret = ax.plot(*args, **kwargs)
3318 finally:
3319 ax._hold = washold
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1896 warnings.warn(msg % (label_namer, func.__name__),
1897 RuntimeWarning, stacklevel=2)
-> 1898 return func(ax, *args, **kwargs)
1899 pre_doc = inner.__doc__
1900 if pre_doc is None:
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
1404 kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
1405
-> 1406 for line in self._get_lines(*args, **kwargs):
1407 self.add_line(line)
1408 lines.append(line)
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs)
405 return
406 if len(remaining) <= 3:
--> 407 for seg in self._plot_args(remaining, kwargs):
408 yield seg
409 return
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
393 ncx, ncy = x.shape[1], y.shape[1]
394 for j in xrange(max(ncx, ncy)):
--> 395 seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
396 ret.append(seg)
397 return ret
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in _makeline(self, x, y, kw, kwargs)
300 default_dict = self._getdefaults(None, kw)
301 self._setdefaults(default_dict, kw)
--> 302 seg = mlines.Line2D(x, y, **kw)
303 return seg
304
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in __init__(self, xdata, ydata, linewidth, linestyle, color, marker, markersize, markeredgewidth, markeredgecolor, markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle, solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery, **kwargs)
418 self._markerfacecoloralt = None
419
--> 420 self.set_markerfacecolor(markerfacecolor)
421 self.set_markerfacecoloralt(markerfacecoloralt)
422 self.set_markeredgecolor(markeredgecolor)
/home/radiance/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in set_markerfacecolor(self, fc)
1204 if fc is None:
1205 fc = 'auto'
-> 1206 if self._markerfacecolor != fc:
1207 self.stale = True
1208 self._markerfacecolor = fc
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Also I tried to do clustering taking my lat, long with other features. DBSCAN throwed error that only two features are allowed. Should I ask this as a separate question?
dbsc = DBSCAN(eps = .5, min_samples = 5, algorithm='ball_tree', metric='haversine').fit(np.radians(df_cont))
The contents of df_cont are-
{'Day': [1, 1, 1, 1, 1],
'Month': [6, 6, 6, 6, 6],
'Year': [2015, 2015, 2015, 2015, 2015],
'driver_id': [5693, 5693, 916461, 1145487, 1145487],
'latitude': [34.640141, 34.64373, 34.551254, 35.613663, 35.614525],
'longitude': [-77.938721,
-77.9394,
-78.78463,
-78.470596,
-78.47466999999999],
'speed_mph': [64, 64, 1, 62, 61],
'speedlimit_mph': [70, 70, 55, 70, 70],
'vehicle_id': [1208979, 1208979, 1262441, 1280223, 1280223]}

I got the error fixed by using a scatter plot. plt.scatter(xy.values[:,0],xy.values[:,1],s=10,c=color,marke‌​r='o')

Related

Error resulting from ImageDataGenerator during data augmentation

Can someone please help me in fixing the error? The code works fine before the for loop. Before the for loop, an array of the image was printed. Is there something wrong with the for loop? The output should be a file stored with augmented images of the input image. The input image is a jpg image.
The code I wrote:
import keras
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=45,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='contrast',
cval=125
)
x = io.imread('mona.jpg')
x = x.reshape((1, ) + x.shape) #Array with shape (1, 256, 256, 3)
i = 0
for batch in data_gen.flow(x, batch_size=16, save_to_dir='/Users/ghad/Desktop',
save_prefix='aug',
save_format='jpg'):
i += 1
if i > 20:
The generated error:
RuntimeError Traceback (most recent call last)
Input In [14], in <cell line: 31>()
28 x = x.reshape((1, ) + x.shape) #Array with shape (1, 256, 256, 3)
30 i = 0
---> 31 for batch in data_gen.flow(x, batch_size=16,
32 save_to_dir='/Users/ghadahalhabib/Desktop',
33 save_prefix='aug',
34 save_format='jpg'):
35 i += 1
36 if i > 20:
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:148, in Iterator.__next__(self, *args, **kwargs)
147 def __next__(self, *args, **kwargs):
--> 148 return self.next(*args, **kwargs)
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:160, in Iterator.next(self)
157 index_array = next(self.index_generator)
158 # The transformation of images is not under thread lock
159 # so it can be done in parallel
--> 160 return self._get_batches_of_transformed_samples(index_array)
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:709, in NumpyArrayIterator._get_batches_of_transformed_samples(self, index_array)
707 x = self.x[j]
708 params = self.image_data_generator.get_random_transform(x.shape)
--> 709 x = self.image_data_generator.apply_transform(
710 x.astype(self.dtype), params)
711 x = self.image_data_generator.standardize(x)
712 batch_x[i] = x
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:1800, in ImageDataGenerator.apply_transform(self, x, transform_parameters)
1797 img_col_axis = self.col_axis - 1
1798 img_channel_axis = self.channel_axis - 1
-> 1800 x = apply_affine_transform(
1801 x,
1802 transform_parameters.get('theta', 0),
1803 transform_parameters.get('tx', 0),
1804 transform_parameters.get('ty', 0),
1805 transform_parameters.get('shear', 0),
1806 transform_parameters.get('zx', 1),
1807 transform_parameters.get('zy', 1),
1808 row_axis=img_row_axis,
1809 col_axis=img_col_axis,
1810 channel_axis=img_channel_axis,
1811 fill_mode=self.fill_mode,
1812 cval=self.cval,
1813 order=self.interpolation_order)
1815 if transform_parameters.get('channel_shift_intensity') is not None:
1816 x = apply_channel_shift(x,
1817 transform_parameters['channel_shift_intensity'],
1818 img_channel_axis)
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in apply_affine_transform(x, theta, tx, ty, shear, zx, zy, row_axis, col_axis, channel_axis, fill_mode, cval, order)
2321 final_affine_matrix = transform_matrix[:2, :2]
2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform( # pylint: disable=g-complex-comprehension
2325 x_channel,
2326 final_affine_matrix,
2327 final_offset,
2328 order=order,
2329 mode=fill_mode,
2330 cval=cval) for x_channel in x]
2331 x = np.stack(channel_images, axis=0)
2332 x = np.rollaxis(x, 0, channel_axis + 1)
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in <listcomp>(.0)
2321 final_affine_matrix = transform_matrix[:2, :2]
2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform( # pylint: disable=g-complex-comprehension
2325 x_channel,
2326 final_affine_matrix,
2327 final_offset,
2328 order=order,
2329 mode=fill_mode,
2330 cval=cval) for x_channel in x]
2331 x = np.stack(channel_images, axis=0)
2332 x = np.rollaxis(x, 0, channel_axis + 1)
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/interpolation.py:574, in affine_transform(input, matrix, offset, output_shape, output, order, mode, cval, prefilter)
572 npad = 0
573 filtered = input
--> 574 mode = _ni_support._extend_mode_to_code(mode)
575 matrix = numpy.asarray(matrix, dtype=numpy.float64)
576 if matrix.ndim not in [1, 2] or matrix.shape[0] < 1:
File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/_ni_support.py:54, in _extend_mode_to_code(mode)
52 return 6
53 else:
---> 54 raise RuntimeError('boundary mode not supported')
RuntimeError: boundary mode not supported
for the code
for batch in data_gen.flow(x, batch_size=16, save_to_dir='/Users/ghad/Desktop', save_prefix='aug', save_format='jpg'):
you are inputting only a single image but asking to produce 16 augmented images. That won't work. Normal the length of x is LARGER than the batch size. Set the batch size to 1. That way you will produce 1 augment image each time you feed a new image into the generator

ValueError: Number of columns must be a positive integer, not 0

when i want to execute below code and plot figer
scatter_matrix(total_frame)
total_frame is a dataframe like this
the error like this:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_11336\1619863705.py in <module>
1 total_frame.dropna(how='any')
----> 2 scatter_matrix(total_frame)
3 plt.show()
~\.conda\envs\env2\lib\site-packages\pandas\plotting\_misc.py in scatter_matrix(frame, alpha, figsize, ax, grid, diagonal, marker, density_kwds, hist_kwds, range_padding, **kwargs)
137 hist_kwds=hist_kwds,
138 range_padding=range_padding,
--> 139 **kwargs,
140 )
141
~\.conda\envs\env2\lib\site-packages\pandas\plotting\_matplotlib\misc.py in scatter_matrix(frame, alpha, figsize, ax, grid, diagonal, marker, density_kwds, hist_kwds, range_padding, **kwds)
48 n = df.columns.size
49 naxes = n * n
---> 50 fig, axes = create_subplots(naxes=naxes, figsize=figsize, ax=ax, squeeze=False)
51
52 # no gaps between subplots
~\.conda\envs\env2\lib\site-packages\pandas\plotting\_matplotlib\tools.py in create_subplots(naxes, sharex, sharey, squeeze, subplot_kw, ax, layout, layout_type, **fig_kw)
265
266 # Create first subplot separately, so we can share it if requested
--> 267 ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
268
269 if sharex:
~\.conda\envs\env2\lib\site-packages\matplotlib\figure.py in add_subplot(self, *args, **kwargs)
770 projection_class, pkw = self._process_projection_requirements(
771 *args, **kwargs)
--> 772 ax = subplot_class_factory(projection_class)(self, *args, **pkw)
773 key = (projection_class, pkw)
774 return self._add_axes_internal(ax, key)
~\.conda\envs\env2\lib\site-packages\matplotlib\axes\_subplots.py in __init__(self, fig, *args, **kwargs)
34 self._axes_class.__init__(self, fig, [0, 0, 1, 1], **kwargs)
35 # This will also update the axes position.
---> 36 self.set_subplotspec(SubplotSpec._from_subplot_args(fig, args))
37
38 #_api.deprecated(
~\.conda\envs\env2\lib\site-packages\matplotlib\gridspec.py in _from_subplot_args(figure, args)
595 f"{len(args)} were given")
596
--> 597 gs = GridSpec._check_gridspec_exists(figure, rows, cols)
598 if gs is None:
599 gs = GridSpec(rows, cols, figure=figure)
~\.conda\envs\env2\lib\site-packages\matplotlib\gridspec.py in _check_gridspec_exists(figure, nrows, ncols)
223 return gs
224 # else gridspec not found:
--> 225 return GridSpec(nrows, ncols, figure=figure)
226
227 def __getitem__(self, key):
~\.conda\envs\env2\lib\site-packages\matplotlib\gridspec.py in __init__(self, nrows, ncols, figure, left, bottom, right, top, wspace, hspace, width_ratios, height_ratios)
385 super().__init__(nrows, ncols,
386 width_ratios=width_ratios,
--> 387 height_ratios=height_ratios)
388
389 _AllowedKeys = ["left", "bottom", "right", "top", "wspace", "hspace"]
~\.conda\envs\env2\lib\site-packages\matplotlib\gridspec.py in __init__(self, nrows, ncols, height_ratios, width_ratios)
51 if not isinstance(ncols, Integral) or ncols <= 0:
52 raise ValueError(
---> 53 f"Number of columns must be a positive integer, not {ncols!r}")
54 self._nrows, self._ncols = nrows, ncols
55 self.set_height_ratios(height_ratios)
ValueError: Number of columns must be a positive integer, not 0
<Figure size 432x288 with 0 Axes>
i search such error and don't find anything,please help me!!!!!
i have solved it,
my data's class is object,the function need num,
so i use pd.convert_dtypes() and it works

pcolormesh: 'NoneType' object has no attribute 'spines'

I want to build a figure showing 6 rasters (.tif) using pcolormesh. I calculated lat lon coordinates from the UTM coordinates using rasterio
with rio.open(T_files[0]) as r:
T0 = r.transform
p1 = Proj(r.crs)
T = r.read()
Turbidity = r.read(1)
cols, rows = np.meshgrid(np.arange(T.shape[2]), np.arange(T.shape[1]))
T1 = T0*Affine.translation(0.5,0.5)
rc2en = lambda r, c: (c, r)*T1
eastings, northings = np.vectorize(rc2en, otypes=[np.float, np.float])(rows, cols)
p2 = Proj(proj='latlong', datum='WGS84')
longs_CD, lats_CD = transform(p1, p2, eastings, northings)
This piece of code was used for each raster since they all have slightly different dimensions due to different satellite orbits. I tried to plot the figure afterwards using
fig1 = plt.figure(figsize=(30, 10), dpi=150)
fig1.patch.set_alpha(1)
#fig.add_subplot(ROW,COLUMN,POSITION)
ax0 = plt.subplot(131, projection=ccrs.Mercator())
ax1 = plt.subplot(132, projection=ccrs.Mercator())
ax2 = plt.subplot(133, projection=ccrs.Mercator())
ax3 = plt.subplot(231, projection=ccrs.Mercator())
ax4 = plt.subplot(232, projection=ccrs.Mercator())
ax5 = plt.subplot(233, projection=ccrs.Mercator())
f0 = ax0.pcolormesh(longs_CD, lats_CD, np.ma.masked_invalid(TCD),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
f1 = ax1.pcolormesh(longs_ND, lats_ND, np.ma.masked_invalid(TND),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
f2 = ax2.pcolormesh(longs_ED, lats_ED, np.ma.masked_invalid(TED),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
f3 = ax3.pcolormesh(longs_CK, lats_CK, np.ma.masked_invalid(TCK),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
f4 = ax4.pcolormesh(longs_NK, lats_NK, np.ma.masked_invalid(TNK),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
f5 = ax5.pcolormesh(longs_EK, lats_EK, np.ma.masked_invalid(TEK),
shading='auto', transform = ccrs.PlateCarree(),
vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
g1 = ax0.gridlines(draw_labels = True)
g1.ylabels_right = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
g1 = ax1.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
g1 = ax2.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
g1 = ax3.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
g1 = ax4.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
g1 = ax5.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20, 'color': 'k'}
g1.ylabel_style = {'size': 20, 'color': 'k'}
cb1 = fig1.colorbar(f1, orientation="horizontal", fraction=0.05, pad=0.07)
cb1.set_label('mean Turbidity [FNU]', fontsize=18)
cb1.tick_params(labelsize=20)
plt.show()
plt.clf()
Similar code snippets run usually fine for me, but this time an error: AttributeError: 'NoneType' object has no attribute 'spines'. I am pretty sure this is easy to fix but i cant figure it out. This is the complete error message
AttributeError Traceback (most recent call last)
<ipython-input-12-ced9235ef932> in <module>
11 f0 = ax0.pcolormesh(longs_CD, lats_CD, np.ma.masked_invalid(TCD),
12 shading='auto', transform = ccrs.PlateCarree(),
---> 13 vmin=1, vmax=200, cmap=plt.cm.rainbow, norm=colors.LogNorm(vmin=1, vmax=200))
14
15 f1 = ax1.pcolormesh(longs_ND, lats_ND, np.ma.masked_invalid(TND),
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in wrapper(self, *args, **kwargs)
308
309 kwargs['transform'] = transform
--> 310 return func(self, *args, **kwargs)
311 return wrapper
312
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in pcolormesh(self, *args, **kwargs)
1559
1560 """
-> 1561 result = self._pcolormesh_patched(*args, **kwargs)
1562 self.autoscale_view()
1563 return result
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in _pcolormesh_patched(self, *args, **kwargs)
1657 self.update_datalim(corners)
1658 self.add_collection(collection)
-> 1659 self.autoscale_view()
1660
1661 ########################
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in autoscale_view(self, tight, scalex, scaley)
853 def autoscale_view(self, tight=None, scalex=True, scaley=True):
854 matplotlib.axes.Axes.autoscale_view(self, tight=tight,
--> 855 scalex=scalex, scaley=scaley)
856 # Limit the resulting bounds to valid area.
857 if scalex and self._autoscaleXon:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in autoscale_view(self, tight, scalex, scaley)
2579 handle_single_axis(
2580 scalex, self._autoscaleXon, self._shared_x_axes, 'intervalx',
-> 2581 'minposx', self.xaxis, self._xmargin, x_stickies, self.set_xbound)
2582 handle_single_axis(
2583 scaley, self._autoscaleYon, self._shared_y_axes, 'intervaly',
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in handle_single_axis(scale, autoscaleon, shared_axes, interval, minpos, axis, margin, stickies, set_bound)
2574 if not self._tight:
2575 x0, x1 = locator.view_limits(x0, x1)
-> 2576 set_bound(x0, x1)
2577 # End of definition of internal function 'handle_single_axis'.
2578
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in set_xbound(self, lower, upper)
3170 self.set_xlim(sorted((lower, upper),
3171 reverse=bool(self.xaxis_inverted())),
-> 3172 auto=None)
3173
3174 def get_xlim(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in set_xlim(self, left, right, emit, auto, xmin, xmax)
3338
3339 if emit:
-> 3340 self.callbacks.process('xlim_changed', self)
3341 # Call all of the other x-axes that are shared with this one
3342 for other in self._shared_x_axes.get_siblings(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self, s, *args, **kwargs)
227 except Exception as exc:
228 if self.exception_handler is not None:
--> 229 self.exception_handler(exc)
230 else:
231 raise
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _exception_printer(exc)
79 def _exception_printer(exc):
80 if _get_running_interactive_framework() in ["headless", None]:
---> 81 raise exc
82 else:
83 traceback.print_exc()
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self, s, *args, **kwargs)
222 if func is not None:
223 try:
--> 224 func(*args, **kwargs)
225 # this does not capture KeyboardInterrupt, SystemExit,
226 # and GeneratorExit
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in _trigger_patch_reclip(event)
2060 axes = event.axes
2061 # trigger the outline and background patches to be re-clipped
-> 2062 axes.spines['geo'].stale = True
2063 axes.patch.stale = True
AttributeError: 'NoneType' object has no attribute 'spines'

matplotlib issue: how to erase this one?

import maplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(np.random.randn(30,3)*100+1000,
index=pd.date_range(start='2018-09-01', periods=30, freq='D'),
columns=['1', '2', 3'])
df[:5].plot.bar()
a Seeing the graph, each x label has '00:00:00', which is unnecessary.
So I tried to delete these by writing this code.
df[:5].plot.bar(x=df[:5].index.date
But it has an error like this.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-52-92dd89374fec> in <module>
----> 1 df[:5].plot.bar(x=df[:5].index.date, stacked=True)
~\anaconda3\lib\site-packages\pandas\plotting\_core.py in bar(self, x, y, **kwargs)
1001 >>> ax = df.plot.bar(x='lifespan', rot=0)
1002 """
-> 1003 return self(kind="bar", x=x, y=y, **kwargs)
1004
1005 def barh(self, x=None, y=None, **kwargs):
~\anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
810 if is_integer(x) and not data.columns.holds_integer():
811 x = data_cols[x]
--> 812 elif not isinstance(data[x], ABCSeries):
813 raise ValueError("x must be a label or position")
814 data = data.set_index(x)
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2804 if is_iterator(key):
2805 key = list(key)
-> 2806 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
2807
2808 # take() does not accept boolean indexers
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1550 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
1551
-> 1552 self._validate_read_indexer(
1553 keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
1554 )
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1638 if missing == len(indexer):
1639 axis_name = self.obj._get_axis_name(axis)
-> 1640 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
1641
1642 # We (temporarily) allow for some missing keys with .loc, except in
KeyError: "None of [Index([2018-09-01, 2018-09-02, 2018-09-03, 2018-09-04, 2018-09-05], dtype='object')] are in the [columns]"
What's the problem?? I just followed the book, but it did come out.
You can change index values before selecting first 5 rows:
df.index = df.index.date
df[:5].plot.bar()
Or:
df.rename(lambda x: x.date())[:5].plot.bar()

pandas groupby got KeyError

I am using pandas to calculate some stats of a data file and got some error. It's reproducible by this simple sample code:
import pandas as pd
df = pd.DataFrame({'A': [1,2,3,4,5,6,7,8,9],
'B': [1,2,3,1,2,3,1,2,3],
'C': ['a', 'b', 'a', 'b', 'a', 'b', 'a','a', 'b']})
def testFun2(x):
return pd.DataFrame({'xlen': x.shape[0]})
def testFun(x):
b = x['B']
print "b equals to {}".format(b) # This line prints okay
c = x['C']
out = pd.DataFrame()
for a in x['A'].unique():
subx = x[x.A == a]
subxg = testFun2(subx)
out = pd.concat([out, subxg])
return out
df.groupby(['B', 'C']).apply(lambda x: testFun(x))
The whole error output look like this:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-21-979d23aa904c> in <module>()
18 return out
19
---> 20 df.groupby(['B', 'C']).apply(lambda x: testFun(x))
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\groupby\groupby.pyc in apply(self, func, *args, **kwargs)
928
929 with _group_selection_context(self):
--> 930 return self._python_apply_general(f)
931
932 return result
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\groupby\groupby.pyc in _python_apply_general(self, f)
934 def _python_apply_general(self, f):
935 keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 936 self.axis)
937
938 return self._wrap_applied_output(
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\groupby\groupby.pyc in apply(self, f, data, axis)
2271 # group might be modified
2272 group_axes = _get_axes(group)
-> 2273 res = f(group)
2274 if not _is_indexed_like(res, group_axes):
2275 mutated = True
<ipython-input-21-979d23aa904c> in <lambda>(x)
18 return out
19
---> 20 df.groupby(['B', 'C']).apply(lambda x: testFun(x))
<ipython-input-21-979d23aa904c> in testFun(x)
9
10 def testFun(x):
---> 11 b = x['B']
12 c = x['C']
13 out = pd.DataFrame()
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
2686 return self._getitem_multilevel(key)
2687 else:
-> 2688 return self._getitem_column(key)
2689
2690 def _getitem_column(self, key):
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _getitem_column(self, key)
2693 # get column
2694 if self.columns.is_unique:
-> 2695 return self._get_item_cache(key)
2696
2697 # duplicate columns & possible reduce dimensionality
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
2487 res = cache.get(item)
2488 if res is None:
-> 2489 values = self._data.get(item)
2490 res = self._box_item_values(item, values)
2491 cache[item] = res
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\internals.pyc in get(self, item, fastpath)
4113
4114 if not isna(item):
-> 4115 loc = self.items.get_loc(item)
4116 else:
4117 indexer = np.arange(len(self.items))[isna(self.items)]
C:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\indexes\base.pyc in get_loc(self, key, method, tolerance)
3078 return self._engine.get_loc(key)
3079 except KeyError:
-> 3080 return self._engine.get_loc(self._maybe_cast_indexer(key))
3081
3082 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'B'
However, I found that if the testFun2 is changed to something simpler, like:
def testFun2(x):
return 1
then the error won't occur. This is very confusing to me - the testFun2 has nothing to do with the line b = x['B'], right? Why did I get the error in the first place? Thanks!