This seems pretty simple, but for some reason, I can't get it to work. This is in pandas 1.2.3 and matplotlib 3.4.0 running on an anaconda's python 3.8.8 on Windows 10 64-bit, inside a Jupyter notebook locally.
I've created a replication below. First, a working version; note the commented out label line:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df_test = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df_test.plot(kind="scatter", x="A", y="B",
alpha=0.4,
figsize=(10,7),
#label="la la",
s=df_test["C"]*100,
c="D",
cmap=plt.get_cmap("jet"),
colorbar=True,
)
plt.legend()
Running the above gives a complaint No handles with labels found to put in legend
(as expected) and some deprecation warnings, but shows a chart. The dots are nicely colored, and you might even see the empty legend box.
Now, uncomment the label line:
df_test.plot(kind="scatter", x="A", y="B",
alpha=0.4,
figsize=(10,7),
label="la la",
s=df_test["C"]*100,
c="D",
cmap=plt.get_cmap("jet"),
colorbar=True,
)
plt.legend()
and when I run this, I get a TypeError: object of type 'NoneType' has no len()
error like the below:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\anaconda\envs\tf-gpu\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
D:\anaconda\envs\tf-gpu\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
246
247 if 'png' in formats:
--> 248 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
249 if 'retina' in formats or 'png2x' in formats:
250 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
D:\anaconda\envs\tf-gpu\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
130 FigureCanvasBase(fig)
131
--> 132 fig.canvas.print_figure(bytes_io, **kw)
133 data = bytes_io.getvalue()
134 if fmt == 'svg':
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2228 else suppress())
2229 with ctx:
-> 2230 self.figure.draw(renderer)
2231
2232 if bbox_inches:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
72 @wraps(draw)
73 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 74 result = draw(artist, renderer, *args, **kwargs)
75 if renderer._rasterizing:
76 renderer.stop_rasterizing()
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
2732
2733 self.patch.draw(renderer)
-> 2734 mimage._draw_list_compositing_images(
2735 renderer, self, artists, self.suppressComposite)
2736
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\_api\deprecation.py in wrapper(*inner_args, **inner_kwargs)
429 else deprecation_addendum,
430 **kwargs)
--> 431 return func(*inner_args, **inner_kwargs)
432
433 return wrapper
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
2923 renderer.stop_rasterizing()
2924
-> 2925 mimage._draw_list_compositing_images(renderer, self, artists)
2926
2927 renderer.close_group('axes')
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\legend.py in draw(self, renderer)
612
613 self.legendPatch.draw(renderer)
--> 614 self._legend_box.draw(renderer)
615
616 renderer.close_group('legend')
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\offsetbox.py in draw(self, renderer)
366 for c, (ox, oy) in zip(self.get_visible_children(), offsets):
367 c.set_offset((px + ox, py + oy))
--> 368 c.draw(renderer)
369
370 bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\offsetbox.py in draw(self, renderer)
366 for c, (ox, oy) in zip(self.get_visible_children(), offsets):
367 c.set_offset((px + ox, py + oy))
--> 368 c.draw(renderer)
369
370 bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\offsetbox.py in draw(self, renderer)
366 for c, (ox, oy) in zip(self.get_visible_children(), offsets):
367 c.set_offset((px + ox, py + oy))
--> 368 c.draw(renderer)
369
370 bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\offsetbox.py in draw(self, renderer)
366 for c, (ox, oy) in zip(self.get_visible_children(), offsets):
367 c.set_offset((px + ox, py + oy))
--> 368 c.draw(renderer)
369
370 bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\offsetbox.py in draw(self, renderer)
692 if self._clip_children and not (c.clipbox or c._clippath):
693 c.set_clip_path(tpath)
--> 694 c.draw(renderer)
695
696 bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\collections.py in draw(self, renderer)
1007 def draw(self, renderer):
1008 self.set_sizes(self._sizes, self.figure.dpi)
-> 1009 super().draw(renderer)
1010
1011
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
D:\anaconda\envs\tf-gpu\lib\site-packages\matplotlib\collections.py in draw(self, renderer)
378 do_single_path_optimization = False
379 if (len(paths) == 1 and len(trans) <= 1 and
--> 380 len(facecolors) == 1 and len(edgecolors) == 1 and
381 len(self._linewidths) == 1 and
382 all(ls[1] is None for ls in self._linestyles) and
TypeError: object of type 'NoneType' has no len()
It seems to be an interaction with the c=
for the coloring and the label=
. If I comment out the color stuff (c=, cmap, colorbar), the plot renders with a legend box, and we saw that it also works with colors included and label commented out. I also tried restarting the kernel, and had same results. I have not tried making new environments with other versions yet, as I wanted to see if this was just me, or something others could replicate.
(BTW, On Colab, python 3.7.10, pandas 1.1.5, matplotlib 3.2.2, the code does appear to work (with a minor figsize complaint), so could be something about the more recent versions of these packages, or my setup...)
So, please be kind, but could anyone point out what silly thing I'm missing here? Any suggestions? Anyone able to replicate?
Possibly a bug in the older version, as it runs fine on recent versions of matplotlib. Recommend to just updating the matplotlib and pandas. Don't linger over it too much.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.