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.
Related
With reliability package, I tried "Example 1" on Fitting a specific distribution to data as below
from reliability.Fitters import Fit_Weibull_2P
import matplotlib.pyplot as plt
data = [58,75,36,52,63,65,22,17,28,64,23,40,73,45,52,36,52,60,13,55,82,55,34,57,23,42,66,35,34,25] # made using Weibull Distribution(alpha=50,beta=3)
wb = Fit_Weibull_2P(failures=data)
plt.show()
Results with Parameters comes out, but error follows like below.
Results from Fit_Weibull_2P (95% CI):
Analysis method: Maximum Likelihood Estimation (MLE)
Optimizer: TNC
Failures / Right censored: 30/0 (0% right censored)
Parameter Point Estimate Standard Error Lower CI Upper CI
Alpha 51.858 3.55628 45.3359 59.3183
Beta 2.80086 0.41411 2.09624 3.74233
Goodness of fit Value
Log-likelihood -129.063
AICc 262.57
BIC 264.928
AD 0.759805
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 4
2 import matplotlib.pyplot as plt
3 data = [58,75,36,52,63,65,22,17,28,64,23,40,73,45,52,36,52,60,13,55,82,55,34,57,23,42,66,35,34,25] # made using Weibull Distribution(alpha=50,beta=3)
----> 4 wb = Fit_Weibull_2P(failures=data)
5 plt.show()
File N:\Programs\Python311\Lib\site-packages\reliability\Fitters.py:2333, in Fit_Weibull_2P.__init__(self, failures, right_censored, show_probability_plot, print_results, CI, quantiles, CI_type, method, optimizer, force_beta, downsample_scatterplot, **kwargs)
2331 else:
2332 rc = right_censored
-> 2333 Weibull_probability_plot(
2334 failures=failures,
2335 right_censored=rc,
2336 __fitted_dist_params=self,
2337 CI=CI,
2338 CI_type=CI_type,
2339 downsample_scatterplot=downsample_scatterplot,
2340 **kwargs,
2341 )
2342 self.probability_plot = plt.gca()
File N:\Programs\Python311\Lib\site-packages\reliability\Probability_plotting.py:373, in Weibull_probability_plot(failures, right_censored, fit_gamma, __fitted_dist_params, a, CI, CI_type, show_fitted_distribution, show_scatter_points, downsample_scatterplot, **kwargs)
368 plt.gca().set_yscale(
369 "function",
370 functions=(axes_transforms.weibull_forward, axes_transforms.weibull_inverse),
371 )
372 plt.xscale("log")
--> 373 plt.grid(b=True, which="major", color="k", alpha=0.3, linestyle="-")
374 plt.grid(b=True, which="minor", color="k", alpha=0.08, linestyle="-")
375 # adjust the figsize. This is done outside of figure creation so that layering of multiple plots is possible
File N:\Programs\Python311\Lib\site-packages\matplotlib\pyplot.py:2589, in grid(visible, which, axis, **kwargs)
2587 #_copy_docstring_and_deprecators(Axes.grid)
2588 def grid(visible=None, which='major', axis='both', **kwargs):
-> 2589 return gca().grid(visible=visible, which=which, axis=axis, **kwargs)
File N:\Programs\Python311\Lib\site-packages\matplotlib\axes\_base.py:3196, in _AxesBase.grid(self, visible, which, axis, **kwargs)
3194 _api.check_in_list(['x', 'y', 'both'], axis=axis)
3195 if axis in ['x', 'both']:
-> 3196 self.xaxis.grid(visible, which=which, **kwargs)
3197 if axis in ['y', 'both']:
3198 self.yaxis.grid(visible, which=which, **kwargs)
File N:\Programs\Python311\Lib\site-packages\matplotlib\axis.py:1655, in Axis.grid(self, visible, which, **kwargs)
1652 if which in ['major', 'both']:
1653 gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
1654 if visible is None else visible)
-> 1655 self.set_tick_params(which='major', **gridkw)
1656 self.stale = True
File N:\Programs\Python311\Lib\site-packages\matplotlib\axis.py:927, in Axis.set_tick_params(self, which, reset, **kwargs)
914 """
915 Set appearance parameters for ticks, ticklabels, and gridlines.
916
(...)
924 gridlines.
925 """
926 _api.check_in_list(['major', 'minor', 'both'], which=which)
--> 927 kwtrans = self._translate_tick_params(kwargs)
929 # the kwargs are stored in self._major/minor_tick_kw so that any
930 # future new ticks will automatically get them
931 if reset:
File N:\Programs\Python311\Lib\site-packages\matplotlib\axis.py:1071, in Axis._translate_tick_params(kw, reverse)
1069 for key in kw_:
1070 if key not in allowed_keys:
-> 1071 raise ValueError(
1072 "keyword %s is not recognized; valid keywords are %s"
1073 % (key, allowed_keys))
1074 kwtrans.update(kw_)
1075 return kwtrans
ValueError: keyword grid_b is not recognized; valid keywords are ['size', 'width', 'color', 'tickdir', 'pad', 'labelsize', 'labelcolor', 'zorder', 'gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On', 'length', 'direction', 'left', 'bottom', 'right', 'top', 'labelleft', 'labelbottom', 'labelright', 'labeltop', 'labelrotation', 'grid_agg_filter', 'grid_alpha', 'grid_animated', 'grid_antialiased', 'grid_clip_box', 'grid_clip_on', 'grid_clip_path', 'grid_color', 'grid_dash_capstyle', 'grid_dash_joinstyle', 'grid_dashes', 'grid_data', 'grid_drawstyle', 'grid_figure', 'grid_fillstyle', 'grid_gapcolor', 'grid_gid', 'grid_in_layout', 'grid_label', 'grid_linestyle', 'grid_linewidth', 'grid_marker', 'grid_markeredgecolor', 'grid_markeredgewidth', 'grid_markerfacecolor', 'grid_markerfacecoloralt', 'grid_markersize', 'grid_markevery', 'grid_mouseover', 'grid_path_effects', 'grid_picker', 'grid_pickradius', 'grid_rasterized', 'grid_sketch_params', 'grid_snap', 'grid_solid_capstyle', 'grid_solid_joinstyle', 'grid_transform', 'grid_url', 'grid_visible', 'grid_xdata', 'grid_ydata', 'grid_zorder', 'grid_aa', 'grid_c', 'grid_ds', 'grid_ls', 'grid_lw', 'grid_mec', 'grid_mew', 'grid_mfc', 'grid_mfcalt', 'grid_ms']
Windows 10 Enterprise 21H2
Python 3.11.2
reliability==0.8.7
matplotlib==3.7.0
matplotlib-inline==0.1.6
How can I prevent error? In another PC with Mac OS, error does not come out and I don't know why.
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')
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>
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);