I am doing a facet grid (sns.FacetGrid) in seaborn of several Kernel Density Plots (sns.kdeplot) and running this code:
import pandas as pd
import seaborn as sns
data = sns.load_dataset('exercise')
g = sns.FacetGrid(data=data, col='diet')
g.map(sns.kdeplot,'pulse',hue='kind')
which results in this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/MislabelUnc/compare/debugsns.py in
11 g = sns.FacetGrid(data=data, col='diet')
----> 12 g.map(sns.kdeplot,'pulse',hue='kind')
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/axisgrid.py in map(self, func, *args, **kwargs)
681
682 # Draw the plot
--> 683 self._facet_plot(func, ax, plot_args, kwargs)
684
685 # Finalize the annotations and layout
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
773 plot_args = []
774 plot_kwargs["ax"] = ax
--> 775 func(*plot_args, **plot_kwargs)
776
777 # Sort out the supporting information
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
44 )
45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)
47 return inner_f
48
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/distributions.py in kdeplot(x, y, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, cbar, cbar_ax, cbar_kws, ax, weights, hue, palette, hue_order, hue_norm, multiple, common_norm, common_grid, levels, thresh, bw_method, bw_adjust, log_scale, color, fill, data, data2, **kwargs)
1694 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
1695
-> 1696 p = _DistributionPlotter(
1697 data=data,
1698 variables=_DistributionPlotter.get_semantics(locals()),
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/distributions.py in __init__(self, data, variables)
107 ):
108
--> 109 super().__init__(data=data, variables=variables)
110
111 #property
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in __init__(self, data, variables)
602 def __init__(self, data=None, variables={}):
603
--> 604 self.assign_variables(data, variables)
605
606 for var, cls in self._semantic_mappings.items():
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in assign_variables(self, data, variables)
665 else:
666 self.input_format = "long"
--> 667 plot_data, variables = self._assign_variables_longform(
668 data, **variables,
669 )
~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in _assign_variables_longform(self, data, **kwargs)
900
901 err = f"Could not interpret value `{val}` for parameter `{key}`"
--> 902 raise ValueError(err)
903
904 else:
ValueError: Could not interpret value `kind` for parameter `hue`
I have no idea what causes this, because
sns.kdeplot(data=data, x='pulse', hue='kind')
works perfectly fine.
What am I doing wrong?
The subplot is divided by the DIET column and the decomposition by the KIND column is specified by the facet grid.
import pandas as pd
import seaborn as sns
data = sns.load_dataset('exercise')
g = sns.FacetGrid(data=data, col='diet', hue='kind')
g.map(sns.kdeplot,'pulse')
The best solution is probably to use seaborn.displot which combines FacetGrid with e.g. kdeplot.
import pandas as pd
import seaborn as sns
data = sns.load_dataset('exercise')
sns.displot(data=data, col='diet', hue='kind', x='pulse', kind='kde')
Related
I'm trying to plot two individual points on a hexbin plot with colors, markers, and labels. When I try include a legend, I get a KeyError. Note I'm doing the same thing with two different sets of data, and it works fine with one set and doesn't work with the other.
I first tried using plt.plot, then with plt.scatter. I tried using different colors. I tried explicitly stating the color and marker. I can't find anything online that describes the same issue.
fig=plt.figure(figsize=(10,7))
plt.hexbin(x,y)
plt.colorbar()
plt.scatter(x1,y1,c='w',marker='*',label='Field 1')
plt.scatter(x2,y2,c='w',marker='^',label='Field 2')
plt.legend(loc='lower right')
plt.show()
For my other data, this plots just fine. But here, I get an error:
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
165 try:
--> 166 rgba = _colors_full_map.cache[c, alpha]
167 except (KeyError, TypeError): # Not in cache, or unhashable.
KeyError: ('f', None)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-33-69334a5c3996> in <module>()
21 plt.scatter(zb,np.log(massb),c='w',marker='*',label='Field 1')
22 plt.scatter(za,np.log(massa),c='w',marker='^',label='Field 2')
---> 23 plt.legend(loc='lower right')
24 #plt.title('Stellar mass vs sSFR')
25 plt.show()
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in legend(*args, **kwargs)
3821 #docstring.copy_dedent(Axes.legend)
3822 def legend(*args, **kwargs):
-> 3823 ret = gca().legend(*args, **kwargs)
3824 return ret
3825
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in legend(self, *args, **kwargs)
555 if len(extra_args):
556 raise TypeError('legend only accepts two non-keyword arguments')
--> 557 self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
558 self.legend_._remove_method = lambda h: setattr(self, 'legend_', None)
559 return self.legend_
~\Anaconda3\lib\site-packages\matplotlib\legend.py in __init__(self, parent, handles, labels, loc, numpoints, markerscale, markerfirst, scatterpoints, scatteryoffsets, prop, fontsize, borderpad, labelspacing, handlelength, handleheight, handletextpad, borderaxespad, columnspacing, ncol, mode, fancybox, shadow, title, framealpha, edgecolor, facecolor, bbox_to_anchor, bbox_transform, frameon, handler_map)
697
698 # init with null renderer
--> 699 self._init_legend_box(handles, labels, markerfirst)
700
701 # If shadow is activated use framealpha if not
~\Anaconda3\lib\site-packages\matplotlib\legend.py in _init_legend_box(self, handles, labels, markerfirst)
952 # original artist/handle.
953 handle_list.append(handler.legend_artist(self, orig_handle,
--> 954 fontsize, handlebox))
955 handles_and_labels.append((handlebox, textbox))
956
~\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in legend_artist(self, legend, orig_handle, fontsize, handlebox)
117 artists = self.create_artists(legend, orig_handle,
118 xdescent, ydescent, width, height,
--> 119 fontsize, handlebox.get_transform())
120
121 # create_artists will return a list of artists.
~\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
726 p = Rectangle(xy=(-xdescent, -ydescent),
727 width=width, height=height)
--> 728 self.update_prop(p, orig_handle, legend)
729 p.set_transform(trans)
730 return [p]
~\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in update_prop(self, legend_handle, orig_handle, legend)
74 def update_prop(self, legend_handle, orig_handle, legend):
75
---> 76 self._update_prop(legend_handle, orig_handle)
77
78 legend._set_artist_props(legend_handle)
~\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in _update_prop(self, legend_handle, orig_handle)
710 edgecolor = getattr(orig_handle, '_original_edgecolor',
711 orig_handle.get_edgecolor())
--> 712 legend_handle.set_edgecolor(first_color(edgecolor))
713 facecolor = getattr(orig_handle, '_original_facecolor',
714 orig_handle.get_facecolor())
~\Anaconda3\lib\site-packages\matplotlib\legend_handler.py in first_color(colors)
697 if colors is None:
698 return None
--> 699 colors = mcolors.to_rgba_array(colors)
700 if len(colors):
701 return colors[0]
~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
265 result = np.empty((len(c), 4), float)
266 for i, cc in enumerate(c):
--> 267 result[i] = to_rgba(cc, alpha)
268 return result
269
~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
166 rgba = _colors_full_map.cache[c, alpha]
167 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 168 rgba = _to_rgba_no_colorcycle(c, alpha)
169 try:
170 _colors_full_map.cache[c, alpha] = rgba
~\Anaconda3\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
210 except ValueError:
211 pass
--> 212 raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
213 # tuple color.
214 c = np.array(c)
ValueError: Invalid RGBA argument: 'f'
I have no idea what 'f' is or where it's coming from. I don't see why using the basic matplotlib colors isn't working.
Something to do with the way the .PolyCollection legend handler sets 'face' as the edgecolor, not expecting it to be parsed as an RGBA array.
Place your x,y data in a DataFrame and create the hexbin with pandas:
plt.hexbin(x,y)
...
plt.scatter(x1,y1,c='w',marker='*',label='Field 1')
...
plt.legend(loc='lower right')
...won't work, but:
df.plot.hexbin('x','y',ax=plt.gca())
...
plt.scatter(x1,y1,c='w',marker='*',label='Field 1')
...
plt.legend(loc='lower right')
...will do the trick.
I am following the Randy Olson approach to make beautiful time trends graphs (see here).
When I plot the following code:
tableau20 = [(31, 119, 180), (174, 199, 232)]
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)
plt.figure(figsize=(12, 14))
# Remove the plot frame lines. They are unnecessary chartjunk.
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
# Ensure that the axis ticks only show up on the bottom and left of the plot.
# Ticks on the right and top of the plot are generally unnecessary chartjunk.
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
majors = ['Number of findings (total)', 'Business Ethics Findings']
for rank, column in enumerate(majors):
plt.plot(monthly_df.argrc__End_Date__c.values, monthly_df[column.replace("\n", " ")].values,
lw=2.5, color=tableau20[rank])
y_pos = monthly_df[column.replace("\n", " ")].values[-1] - 0.5
if column == "Number of findings (total)":
y_pos += 0.5
elif column == 'Business Ethics Findings':
y_pos -= 0.5
plt.text(2018.1, y_pos, column, fontsize=12, color=tableau20[rank])
I get this error:
If I eliminate
plt.text(2016, y_pos, column, fontsize=12, color=tableau20[rank])
Instead I get the two lines plotted on the graph correctly, but with no legend. How do I show the names of my columns?
EDIT
I am adding here the traceback for further information. I hope this is helpful.
C:\Users\filippo.sebastio\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
warnings.warn(message, mplDeprecation, stacklevel=1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\Anaconda3\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)
~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
239
240 if 'png' in formats:
--> 241 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
242 if 'retina' in formats or 'png2x' in formats:
243 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
123
124 bytes_io = BytesIO()
--> 125 fig.canvas.print_figure(bytes_io, **kw)
126 data = bytes_io.getvalue()
127 if fmt == 'svg':
~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2261 orientation=orientation,
2262 bbox_inches_restore=_bbox_inches_restore,
-> 2263 **kwargs)
2264 finally:
2265 if bbox_inches and restore_bbox:
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
515
516 def print_png(self, filename_or_obj, *args, **kwargs):
--> 517 FigureCanvasAgg.draw(self)
518 renderer = self.get_renderer()
519 original_dpi = renderer.dpi
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
427 Draw the figure using the renderer
428 """
--> 429 self.renderer = self.get_renderer(cleared=True)
430 # acquire a lock on the shared font cache
431 RendererAgg.lock.acquire()
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in get_renderer(self, cleared)
452
453 if need_new_renderer:
--> 454 self.renderer = RendererAgg(w, h, self.figure.dpi)
455 self._lastKey = key
456 elif cleared:
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in __init__(self, width, height, dpi)
99 self.width = width
100 self.height = height
--> 101 self._renderer = _RendererAgg(int(width), int(height), dpi)
102 self._filter_renderers = []
103
ValueError: Image size of 312943x821 pixels is too large. It must be less than 2^16 in each direction.
<Figure size 864x1008 with 1 Axes>
I get an error when I plot with colours, and works fine without it. My line colours need to be restricted to 2 defined values.
This works in Jupyter Notebook
import random
xStart = random.sample(range(1, 10), 6)
xStart.sort()
xEnd = [x + random.randint(1, 6) for x in xStart]
yval = list(range(1, 7))
colours = ['r']*6
colours[1] = 'b'
print(xStart)
print(xEnd)
print(yval)
print(colours)
f, ax1 = plt.subplots(figsize=(6,4))
ax1.plot([xStart,xEnd], [yval,yval], '-', linewidth=1) #, color=colours)
plt.show()
This does not work.
If I uncomment the color argument, the code throws an (elaborate) error. While I can draw each line segment in a loop and colour each red or blue, I assume it will be slower than the below code. In this toy example I have 6 lines, but in reality I have 12,000 lines and it takes a few minutes, drawing one line at a time in a loop.
I think the error is related to the size of my colour argument; it is likely expecting 1 (at a time internally) whereas I am providing a list of 6.
import random
xStart = random.sample(range(1, 10), 6)
xStart.sort()
xEnd = [x + random.randint(1, 6) for x in xStart]
yval = list(range(1, 7))
colours = ['r']*6
colours[1] = 'b'
print(xStart)
print(xEnd)
print(yval)
print(colours)
f, ax1 = plt.subplots(figsize=(6,4))
ax1.plot([xStart,xEnd], [yval,yval], '-', linewidth=1, color=colours) #--> Only change from above code
plt.show()
TypeError Traceback (most recent call
last) C:\Anaconda3\lib\site-packages\matplotlib\colors.py in
to_rgba(c, alpha)
131 try:
--> 132 rgba = _colors_full_map.cache[c, alpha]
133 except (KeyError, TypeError): # Not in cache, or unhashable.
TypeError: unhashable type: 'list'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call
last) C:\Anaconda3\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)
C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in
(fig)
236
237 if 'png' in formats:
--> 238 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
239 if 'retina' in formats or 'png2x' in formats:
240 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in
print_figure(fig, fmt, bbox_inches, **kwargs)
120
121 bytes_io = BytesIO()
--> 122 fig.canvas.print_figure(bytes_io, **kw)
123 data = bytes_io.getvalue()
124 if fmt == 'svg':
C:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in
print_figure(self, filename, dpi, facecolor, edgecolor, orientation,
format, **kwargs) 2214 orientation=orientation,
2215 dryrun=True,
-> 2216 **kwargs) 2217 renderer = self.figure._cachedRenderer 2218 bbox_inches = self.figure.get_tightbbox(renderer)
C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in
print_png(self, filename_or_obj, *args, **kwargs)
505
506 def print_png(self, filename_or_obj, *args, **kwargs):
--> 507 FigureCanvasAgg.draw(self)
508 renderer = self.get_renderer()
509 original_dpi = renderer.dpi
C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in
draw(self)
428 # if toolbar:
429 # toolbar.set_cursor(cursors.WAIT)
--> 430 self.figure.draw(self.renderer)
431 finally:
432 # if toolbar:
C:\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
C:\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self,
renderer) 1297 1298
mimage._draw_list_compositing_images(
-> 1299 renderer, self, artists, self.suppressComposite) 1300 1301
renderer.close_group('figure')
C:\Anaconda3\lib\site-packages\matplotlib\image.py in
_draw_list_compositing_images(renderer, parent, artists, suppress_composite)
136 if not_composite or not has_images:
137 for a in artists:
--> 138 a.draw(renderer)
139 else:
140 # Composite any adjacent images together
C:\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
C:\Anaconda3\lib\site-packages\matplotlib\axes_base.py in draw(self,
renderer, inframe) 2435 renderer.stop_rasterizing()
2436
-> 2437 mimage._draw_list_compositing_images(renderer, self, artists) 2438 2439 renderer.close_group('axes')
C:\Anaconda3\lib\site-packages\matplotlib\image.py in
_draw_list_compositing_images(renderer, parent, artists, suppress_composite)
136 if not_composite or not has_images:
137 for a in artists:
--> 138 a.draw(renderer)
139 else:
140 # Composite any adjacent images together
C:\Anaconda3\lib\site-packages\matplotlib\artist.py in
draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
C:\Anaconda3\lib\site-packages\matplotlib\lines.py in draw(self,
renderer)
765 self._set_gc_clip(gc)
766
--> 767 ln_color_rgba = self._get_rgba_ln_color()
768 gc.set_foreground(ln_color_rgba, isRGBA=True)
769 gc.set_alpha(ln_color_rgba[3])
C:\Anaconda3\lib\site-packages\matplotlib\lines.py in
_get_rgba_ln_color(self, alt) 1267 1268 def _get_rgba_ln_color(self, alt=False):
-> 1269 return mcolors.to_rgba(self._color, self._alpha) 1270 1271 # some aliases....
C:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c,
alpha)
132 rgba = _colors_full_map.cache[c, alpha]
133 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 134 rgba = _to_rgba_no_colorcycle(c, alpha)
135 try:
136 _colors_full_map.cache[c, alpha] = rgba
C:\Anaconda3\lib\site-packages\matplotlib\colors.py in
_to_rgba_no_colorcycle(c, alpha)
183 # float)andnp.array(...).astype(float)` all convert "0.5" to 0.5.
184 # Test dimensionality to reject single floats.
--> 185 raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
186 # Return a tuple to prevent the cached value from being modified.
187 c = tuple(c.astype(float))
ValueError: Invalid RGBA argument: ['r', 'b', 'r', 'r', 'r', 'r']
OK thanks to Bazingaa and this thread, How to get different colored lines for different plots in a single figure?
...the final code is as follows.
Since I am drawing multiple lines with one ax.plot() command, the colour argument will not take. IMHO it should since it logically makes sense and should be an enhancement matplotlib does. Nonetheless, here is the solution as Bazingaa pointed out to me.
For those interested, as expected this code does run a LOT faster compared with drawing 12K lines in a loop (in order to draw & color them one at a time with individual ax.plot() commands).
import random
xStart = random.sample(range(1, 10), 6)
xStart.sort()
xEnd = [x + random.randint(1, 6) for x in xStart]
yval = list(range(1, 7))
colours = ['r']*6
colours[1] = 'b'
f, ax1 = plt.subplots(figsize=(6,4))
ax1.plot([xStart,xEnd], [yval,yval], '-', linewidth=1) #, color=colours) #Leaving the color argument commented
#Add new code to colour after the fact
for idx,line in enumerate(ax1.lines):
line.set_color(colours[idx])
plt.show()
I am having trouble while trying to plot a lmplot in Jupyter.
Below you can see the first rows of my data and rest of the data goes same as well.
Year_of_Release Platform_General Platform counts Global_Sales(M#) GS_Amount/Game
1994.0 Sony_Playstation PS 1 1.27 1.270000
1996.0 Sony_Playstation PS 5 17.48 3.496000
1997.0 Sony_Playstation PS 12 30.89 2.574167
1998.0 Sony_Playstation PS 22 43.08 1.958182
1999.0 Sony_Playstation PS 25 49.02 1.960800
Below is the code that should draw the plot but
# Make a custom palette with platform colors
pal = dict(Sony_Playstation="#6495ED",Microsoft_Xbox="#F08080",Nintendo="Green")
# Show the survival proability as a function of platforms
g = sns.lmplot(x="Year_of_Release", y="GS_Amount/Game", col="Platform_General", hue="Platform_General",
data=vgs_df_pf_grouped,palette=pal, y_jitter=.02, logistic=True)
# Use more informative axis labels than are provided by default
g.set_axis_labels("Year of Release", "Sales per Game Released (M #)")
When I run this code below long error massage appears and because of I am new to data analytics I can't understand what is wrong with data or code.
I appreciate some help with this one. Thank you.
ValueError Traceback (most recent call last)
<ipython-input-68-c78b6ba34d96> in <module>()
5 # Show the survival proability as a function of age and sex
6 g = sns.lmplot(x="Year_of_Release", y="GS_Amount/Game",col="Platform_General", hue="Platform_General",
----> 7 data=vgs_df_pf_grouped,palette=pal, y_jitter=.02, logistic=True)
8
9 # Use more informative axis labels than are provided by default
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in lmplot(x, y, data, hue, col, row, palette, col_wrap, size, aspect, markers, sharex, sharey, hue_order, col_order, row_order, legend, legend_out, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, x_jitter, y_jitter, scatter_kws, line_kws)
588 scatter_kws=scatter_kws, line_kws=line_kws,
589 )
--> 590 facets.map_dataframe(regplot, x, y, **regplot_kws)
591
592 # Add a legend
~/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py in map_dataframe(self, func, *args, **kwargs)
795
796 # Draw the plot
--> 797 self._facet_plot(func, ax, args, kwargs)
798
799 # Finalize the annotations and layout
~/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
813
814 # Draw the plot
--> 815 func(*plot_args, **plot_kwargs)
816
817 # Sort out the supporting information
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
788 scatter_kws["marker"] = marker
789 line_kws = {} if line_kws is None else copy.copy(line_kws)
--> 790 plotter.plot(ax, scatter_kws, line_kws)
791 return ax
792
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in plot(self, ax, scatter_kws, line_kws)
340 self.scatterplot(ax, scatter_kws)
341 if self.fit_reg:
--> 342 self.lineplot(ax, line_kws)
343
344 # Label the axes
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in lineplot(self, ax, kws)
385
386 # Fit the regression model
--> 387 grid, yhat, err_bands = self.fit_regression(ax)
388
389 # Get set default aesthetics
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in fit_regression(self, ax, x_range, grid)
198 from statsmodels.genmod.families import Binomial
199 yhat, yhat_boots = self.fit_statsmodels(grid, GLM,
--> 200 family=Binomial())
201 elif self.lowess:
202 ci = None
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in fit_statsmodels(self, grid, model, **kwargs)
258 return yhat
259
--> 260 yhat = reg_func(X, y)
261 if self.ci is None:
262 return yhat, None
~/anaconda3/lib/python3.6/site-packages/seaborn/regression.py in reg_func(_x, _y)
252 def reg_func(_x, _y):
253 try:
--> 254 yhat = model(_y, _x, **kwargs).fit().predict(grid)
255 except glm.PerfectSeparationError:
256 yhat = np.empty(len(grid))
~/anaconda3/lib/python3.6/site-packages/statsmodels/genmod/generalized_linear_model.py in fit(self, start_params, maxiter, method, tol, scale, cov_type, cov_kwds, use_t, full_output, disp, max_start_irls, **kwargs)
901 return self._fit_irls(start_params=start_params, maxiter=maxiter,
902 tol=tol, scale=scale, cov_type=cov_type,
--> 903 cov_kwds=cov_kwds, use_t=use_t, **kwargs)
904 else:
905 return self._fit_gradient(start_params=start_params,
~/anaconda3/lib/python3.6/site-packages/statsmodels/genmod/generalized_linear_model.py in _fit_irls(self, start_params, maxiter, tol, scale, cov_type, cov_kwds, use_t, **kwargs)
977 dev = self.family.deviance(self.endog, mu, self.freq_weights)
978 if np.isnan(dev):
--> 979 raise ValueError("The first guess on the deviance function "
980 "returned a nan. This could be a boundary "
981 " problem and should be reported.")
ValueError: The first guess on the deviance function returned a nan. This could be a boundary problem and should be reported.
In Seaborn, you can use FacetGrid to set up data-aware grids on which to plot. You can then use the map or map_dataframe methods to plot to those grids.
I am having trouble correctly specifying a user-defined plot function that works with map or map_dataframe. In this example I use the errorbar function in which I want to pass the error values as a 2xN array-like. In my example (taken from #mwaskom's answer here) the errors are symmetrical -- but imagine I have a situation where they are not.
In [255]:
from scipy import stats
tips_all = sns.load_dataset("tips")
tips_grouped = tips_all.groupby(["smoker", "size"])
tips = tips_grouped.mean()
tips["error_min"] = tips_grouped.total_bill.apply(stats.sem) * 1.96
tips["error_max"] = tips_grouped.total_bill.apply(stats.sem) * 1.96
tips.reset_index(inplace=True)
tips
Out[255]:
smoker size total_bill tip error_min error_max
0 No 1 8.660000 1.415000 2.763600 2.763600
1 No 2 15.342333 2.489000 0.919042 0.919042
2 No 3 21.009615 3.069231 2.680447 2.680447
3 No 4 27.769231 4.195769 3.303131 3.303131
4 No 5 30.576667 5.046667 11.620808 11.620808
5 No 6 34.830000 5.225000 9.194360 9.194360
6 Yes 1 5.825000 1.460000 5.399800 5.399800
7 Yes 2 17.955758 2.709545 1.805528 1.805528
8 Yes 3 28.191667 4.095000 6.898186 6.898186
9 Yes 4 30.609091 3.992727 5.150063 5.150063
10 Yes 5 29.305000 2.500000 2.263800 2.263800
Define my error bar function, that takes data and indexes the error columns to produce the 2xN array:
In [256]:
def my_errorbar(*args, **kwargs):
data = kwargs['data']
errors = np.vstack([data['error_min'],
data['error_max']])
print(errors)
plt.errorbar(data[args[0]],
data[args[1]],
yerr=errors,
**kwargs);
Call using map_dataframe (because my function gets the data as a kwarg):
In [257]:
g = sns.FacetGrid(tips, col="smoker", size=5)
g.map_dataframe(my_errorbar, "size", "total_bill", marker="o")
[[ 2.7636 0.9190424 2.68044722 3.30313068 11.62080751
9.19436049]
[ 2.7636 0.9190424 2.68044722 3.30313068 11.62080751
9.19436049]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-257-dc8b35ec70ec> in <module>()
1 g = sns.FacetGrid(tips, col="smoker", size=5)
----> 2 g.map_dataframe(my_errorbar, "size", "total_bill", marker="o")
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/seaborn/axisgrid.py in map_dataframe(self, func, *args, **kwargs)
509
510 # Draw the plot
--> 511 self._facet_plot(func, ax, args, kwargs)
512
513 # Finalize the annotations and layout
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
527
528 # Draw the plot
--> 529 func(*plot_args, **plot_kwargs)
530
531 # Sort out the supporting information
<ipython-input-256-62202c841233> in my_errorbar(*args, **kwargs)
9 data[args[1]],
10 yerr=errors,
---> 11 **kwargs);
12
13
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/pyplot.py in errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, hold, **kwargs)
2764 barsabove=barsabove, lolims=lolims, uplims=uplims,
2765 xlolims=xlolims, xuplims=xuplims,
-> 2766 errorevery=errorevery, capthick=capthick, **kwargs)
2767 draw_if_interactive()
2768 finally:
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_axes.py in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
2859
2860 if not barsabove and plot_line:
-> 2861 l0, = self.plot(x, y, fmt, **kwargs)
2862
2863 if ecolor is None:
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
1371 lines = []
1372
-> 1373 for line in self._get_lines(*args, **kwargs):
1374 self.add_line(line)
1375 lines.append(line)
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs)
302 return
303 if len(remaining) <= 3:
--> 304 for seg in self._plot_args(remaining, kwargs):
305 yield seg
306 return
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
290 ncx, ncy = x.shape[1], y.shape[1]
291 for j in xrange(max(ncx, ncy)):
--> 292 seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
293 ret.append(seg)
294 return ret
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_base.py in _makeline(self, x, y, kw, kwargs)
242 **kw
243 )
--> 244 self.set_lineprops(seg, **kwargs)
245 return seg
246
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/axes/_base.py in set_lineprops(self, line, **kwargs)
184 raise TypeError('There is no line property "%s"' % key)
185 func = getattr(line, funcName)
--> 186 func(val)
187
188 def set_patchprops(self, fill_poly, **kwargs):
/Users/x/miniconda3/envs/default/lib/python3.4/site-packages/matplotlib/lines.py in set_data(self, *args)
557 """
558 if len(args) == 1:
--> 559 x, y = args[0]
560 else:
561 x, y = args
ValueError: too many values to unpack (expected 2)
I don't understand the reason for the failure here. Note that the plot function gets something, because a plot of the first grid of errorbars is produced. I assume I'm not passing the **kwargs dictionary on correctly.
In general, I would find it really helpful if the tutorial for Seaborn contained one or two examples of user-defined plot functions passed to map or map_dataframe.
This is #mwaskom's answer, and works a treat (see comments):
Just change the my_errorbar function so that it pops the data out of the keyword dict:
def my_errorbar(*args, **kwargs):
data = kwargs.pop('data')
errors = np.vstack([data['error_min'],
data['error_max']])
print(errors)
plt.errorbar(data[args[0]],
data[args[1]],
yerr=errors,
**kwargs);