Error on "Fitting a specific distribution to data" with reliability package - python

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.

Related

How to make use of the innvestigate package for LRP with tabular data?

I am trying to get LIME and LRP working on a simple DNN with tabular data for some general usability evaluations of the two approaches with non-techsavy users. I managed to get LIME running to get an insight into the predictions of my network. Yet, I am having trouble implementing LRP with innvestigate to get insights into those predicitons.
So far I have created a relevance-based analyzer with:
import innvestigate
#create instance to analyze later on
data = (X_train,
y_train,
X_test,
y_test)
instance_to_test = data[2][7:8]
#create LRP analyzer
LRP_analyzer = innvestigate.analyzer.relevance_based.relevance_analyzer.LRPZ(model)
#analyze the specified instance
analysis = LRP_analyzer.analyze(instance_to_test)
Yet, after that I am stuck. What I want to do at the end, is taking my specified instance of the data and create a heatmap of the contributions to the classification result (meaning: relevance scores of the features for that instance). Has anybody worked with innvestigate (and this somewhat special use-case, as it is mainly used for image data) and can help me with that? I know that it should in theory be possible as I have seen LRP used with tabular data in this paper. Besides my code above I recieve the following error message when trying to get a heatmap displayed (see bottom of the post).
Yet, that should be because the general implementation requires an image tensor with 4 axes, which I can't provide. Any ideas for a workaround to make that happen for tabular data?
TypeError Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 plt.imshow(analysis.squeeze(), cmap="seismic")
2 plt.show()
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\_api\deprecation.py:459, in make_keyword_only.<locals>.wrapper(*args, **kwargs)
453 if len(args) > name_idx:
454 warn_deprecated(
455 since, message="Passing the %(name)s %(obj_type)s "
456 "positionally is deprecated since Matplotlib %(since)s; the "
457 "parameter will become keyword-only %(removal)s.",
458 name=name, obj_type=f"parameter of {func.__name__}()")
--> 459 return func(*args, **kwargs)
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\pyplot.py:2650, in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, data, **kwargs)
2644 #_copy_docstring_and_deprecators(Axes.imshow)
2645 def imshow(
2646 X, cmap=None, norm=None, aspect=None, interpolation=None,
2647 alpha=None, vmin=None, vmax=None, origin=None, extent=None, *,
2648 interpolation_stage=None, filternorm=True, filterrad=4.0,
2649 resample=None, url=None, data=None, **kwargs):
-> 2650 __ret = gca().imshow(
2651 X, cmap=cmap, norm=norm, aspect=aspect,
2652 interpolation=interpolation, alpha=alpha, vmin=vmin,
2653 vmax=vmax, origin=origin, extent=extent,
2654 interpolation_stage=interpolation_stage,
2655 filternorm=filternorm, filterrad=filterrad, resample=resample,
2656 url=url, **({"data": data} if data is not None else {}),
2657 **kwargs)
2658 sci(__ret)
2659 return __ret
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\_api\deprecation.py:459, in make_keyword_only.<locals>.wrapper(*args, **kwargs)
453 if len(args) > name_idx:
454 warn_deprecated(
455 since, message="Passing the %(name)s %(obj_type)s "
456 "positionally is deprecated since Matplotlib %(since)s; the "
457 "parameter will become keyword-only %(removal)s.",
458 name=name, obj_type=f"parameter of {func.__name__}()")
--> 459 return func(*args, **kwargs)
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\__init__.py:1414, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
1411 #functools.wraps(func)
1412 def inner(ax, *args, data=None, **kwargs):
1413 if data is None:
-> 1414 return func(ax, *map(sanitize_sequence, args), **kwargs)
1416 bound = new_sig.bind(ax, *args, **kwargs)
1417 auto_label = (bound.arguments.get(label_namer)
1418 or bound.kwargs.get(label_namer))
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\axes\_axes.py:5487, in Axes.imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, **kwargs)
5480 self.set_aspect(aspect)
5481 im = mimage.AxesImage(self, cmap, norm, interpolation,
5482 origin, extent, filternorm=filternorm,
5483 filterrad=filterrad, resample=resample,
5484 interpolation_stage=interpolation_stage,
5485 **kwargs)
-> 5487 im.set_data(X)
5488 im.set_alpha(alpha)
5489 if im.get_clip_path() is None:
5490 # image does not already have clipping set, clip to axes patch
File D:\Programme\Anaconda\envs\LRP_innvestigate\lib\site-packages\matplotlib\image.py:715, in _ImageBase.set_data(self, A)
711 self._A = self._A[:, :, 0]
713 if not (self._A.ndim == 2
714 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
--> 715 raise TypeError("Invalid shape {} for image data"
716 .format(self._A.shape))
718 if self._A.ndim == 3:
719 # If the input data has values outside the valid range (after
720 # normalisation), we issue a warning and then clip X to the bounds
721 # - otherwise casting wraps extreme values, hiding outliers and
722 # making reliable interpretation impossible.
723 high = 255 if np.issubdtype(self._A.dtype, np.integer) else 1
TypeError: Invalid shape (10,) for image data

When adding Colorbar for a Matplotlib Tricontourf: "TypeError: You must first set_array for mappable" (MacOS has this problem, Win10 No Problem)

I have a np.array of a few columns recording many parameters and the result derived from these parameters. Now I am trying to make a contour plot of 2 parameters to see their effect on the result.
param_1 = record[:,5]
param_2 = record[:,6]
result = record[:,3]
fig, ax = plt.subplots(figsize=(8,6))
cntr = ax.tricontourf(param_1, param_2, result, levels=15, cmap=cm.jet)
print(cntr.cvalues.min())
print(cntr.cvalues.max())
norm = matplotlib.colors.Normalize(vmin=cntr.cvalues.min(),vmax=cntr.cvalues.max())
sm = plt.cm.ScalarMappable(norm=norm, cmap=cm.jet)
print(sm)
fig.colorbar(sm, ticks=cntr.levels)
Then the TypeError jumps out:
0.0675
0.2925
<matplotlib.cm.ScalarMappable object at 0x1274adbe0>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-1f1b3d95dbe5> in <module>
9 sm = plt.cm.ScalarMappable(norm=norm, cmap=cm.jet)
10 print(sm)
---> 11 fig.colorbar(sm, ticks=cntr.levels)
/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
2127 'panchor']
2128 cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 2129 cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
2130
2131 self.sca(current_ax)
/anaconda3/lib/python3.7/site-packages/matplotlib/colorbar.py in colorbar_factory(cax, mappable, **kwargs)
1564 cb = ColorbarPatch(cax, mappable, **kwargs)
1565 else:
-> 1566 cb = Colorbar(cax, mappable, **kwargs)
1567
1568 cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
/anaconda3/lib/python3.7/site-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kw)
1070 # Ensure the given mappable's norm has appropriate vmin and vmax set
1071 # even if mappable.draw has not yet been called.
-> 1072 mappable.autoscale_None()
1073
1074 self.mappable = mappable
/anaconda3/lib/python3.7/site-packages/matplotlib/cm.py in autoscale_None(self)
372 """
373 if self._A is None:
--> 374 raise TypeError('You must first set_array for mappable')
375 self.norm.autoscale_None(self._A)
376 self.changed()
TypeError: You must first set_array for mappable})
However, with the same data and code, I can make it under Windows10. It seems the problem is the OS (now is in MacOS).
Also, I found a similar question asked a few month ago:
enter link description here

Jupyter Seaborn sns.lmplot Error

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.

Memory error while plotting dataframe (matplotlib)

I'm using Pandas with Jupyter Notebook and trying to plot a small dataframe:
and when i'm inserting the following line:
df9.plot(x='Time', y='Pressure mean')
I'm getting the following error:
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-8-c789b8162a1a> in <module>()
----> 1 df9.plot(x='Time', y='Pressure mean')
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
3735 fontsize=fontsize, colormap=colormap, table=table,
3736 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3737 sort_columns=sort_columns, **kwds)
3738 __call__.__doc__ = plot_frame.__doc__
3739
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
2609 yerr=yerr, xerr=xerr,
2610 secondary_y=secondary_y, sort_columns=sort_columns,
-> 2611 **kwds)
2612
2613
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)
2436 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
2437
-> 2438 plot_obj.generate()
2439 plot_obj.draw()
2440 return plot_obj.result
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in generate(self)
1029
1030 for ax in self.axes:
-> 1031 self._post_plot_logic_common(ax, self.data)
1032 self._post_plot_logic(ax, self.data)
1033
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in _post_plot_logic_common(self, ax, data)
1157 ax.set_xticklabels(xticklabels)
1158 self._apply_axis_properties(ax.xaxis, rot=self.rot,
-> 1159 fontsize=self.fontsize)
1160 self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
1161 elif self.orientation == 'horizontal':
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in _apply_axis_properties(self, axis, rot, fontsize)
1205
1206 def _apply_axis_properties(self, axis, rot=None, fontsize=None):
-> 1207 labels = axis.get_majorticklabels() + axis.get_minorticklabels()
1208 for label in labels:
1209 if rot is not None:
C:\Anaconda3\lib\site-packages\matplotlib\axis.py in get_majorticklabels(self)
1159 def get_majorticklabels(self):
1160 'Return a list of Text instances for the major ticklabels'
-> 1161 ticks = self.get_major_ticks()
1162 labels1 = [tick.label1 for tick in ticks if tick.label1On]
1163 labels2 = [tick.label2 for tick in ticks if tick.label2On]
C:\Anaconda3\lib\site-packages\matplotlib\axis.py in get_major_ticks(self, numticks)
1288 'get the tick instances; grow as necessary'
1289 if numticks is None:
-> 1290 numticks = len(self.get_major_locator()())
1291 if len(self.majorTicks) < numticks:
1292 # update the new tick label properties from the old
C:\Anaconda3\lib\site-packages\pandas\tseries\converter.py in __call__(self)
876 vmin, vmax = vmax, vmin
877 if self.isdynamic:
--> 878 locs = self._get_default_locs(vmin, vmax)
879 else: # pragma: no cover
880 base = self.base
C:\Anaconda3\lib\site-packages\pandas\tseries\converter.py in _get_default_locs(self, vmin, vmax)
857
858 if self.plot_obj.date_axis_info is None:
--> 859 self.plot_obj.date_axis_info = self.finder(vmin, vmax, self.freq)
860
861 locator = self.plot_obj.date_axis_info
C:\Anaconda3\lib\site-packages\pandas\tseries\converter.py in _daily_finder(vmin, vmax, freq)
481 Period(ordinal=int(vmax), freq=freq))
482 span = vmax.ordinal - vmin.ordinal + 1
--> 483 dates_ = PeriodIndex(start=vmin, end=vmax, freq=freq)
484 # Initialize the output
485 info = np.zeros(span,
C:\Anaconda3\lib\site-packages\pandas\tseries\period.py in __new__(cls, data, ordinal, freq, start, end, periods, copy, name, tz, **kwargs)
186 else:
187 data, freq = cls._generate_range(start, end, periods,
--> 188 freq, kwargs)
189 else:
190 ordinal, freq = cls._from_arraylike(data, freq, tz)
C:\Anaconda3\lib\site-packages\pandas\tseries\period.py in _generate_range(cls, start, end, periods, freq, fields)
200 raise ValueError('Can either instantiate from fields '
201 'or endpoints, but not both')
--> 202 subarr, freq = _get_ordinal_range(start, end, periods, freq)
203 elif field_count > 0:
204 subarr, freq = _range_from_fields(freq=freq, **fields)
C:\Anaconda3\lib\site-packages\pandas\tseries\period.py in _get_ordinal_range(start, end, periods, freq, mult)
1026 dtype=np.int64)
1027 else:
-> 1028 data = np.arange(start.ordinal, end.ordinal + 1, mult, dtype=np.int64)
1029
1030 return data, freq
MemoryError:
What is the problem ? I can't figure it out.
Thanks !
The issue originates from using TimedeltaIndex (or timedelta) for your time column. It was reported there: https://github.com/pydata/pandas/issues/8711
No solution has been brought to us yet.
As an alternative solution, I invite you to convert your data to DateTime or DateTimeIndex. Let's say YourDate contains the starting date of your observations.
df9.index = pd.DatetimeIndex(pd.datetime.strptime(YourDate,'%d.%m.%Y %H:%M:%S')
+df9['Time'])
df9.plot(y='Pressure mean')
Note that it will plot only the hours if you have less than 24 hours.
EDIT (2016-11-07):
I can now use timedelta as index and plot correctly. This is how I proceed (assuming I have float numbers indicating hours):
converter = {'Time[h]' : lambda x: pd.to_timedelta(float(x),unit='h')}#converts float to timedelta
df = pd.read_csv(fpath, sep='\t',
skiprows=len(comments),#header
names=dt.keys(),#you need of course your own dtype
dtype=dt,#you need of course your own dtype
encoding='latin-1',#European data...
skipinitialspace=True,
converters=converter)
df = df.set_index('Time[h]')#time column to index.
As Wli mentioned, it is a bug still to be fixed. But as a workaround this worked for me. -
plt.plot(s.index,s.values)

Plotting dataframe raises error of ordinal value must be >= 1

I follow the tutorial at http://nbviewer.ipython.org/github/jvns/pandas-cookbook/blob/v0.1/cookbook/Chapter%205%20-%20Combining%20dataframes%20and%20scraping%20Canadian%20weather%20data.ipynb
I have a pandas dataframe
weather_mar2012['Temp (°C)']
Out[30]:
Date/Time
2012-03-01 00:00:00 -5.5
2012-03-01 01:00:00 -5.7
2012-03-01 02:00:00 -5.4
When trying to plot it i get an error
weather_mar2012['Temp (°C)'].plot(figsize=(15, 5))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last) <ipython-input-31-21c79ba7d5ef> in <module>()
----> 1 weather_mar2012['Temp (°C)'].plot(figsize=(15, 5))
/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds) 2486 yerr=yerr, xerr=xerr, 2487 label=label, secondary_y=secondary_y,
-> 2488 **kwds) 2489 2490
/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds) 2292 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds) 2293
-> 2294 plot_obj.generate() 2295 plot_obj.draw() 2296 return plot_obj.result
/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in generate(self)
922 self._make_legend()
923 self._post_plot_logic()
--> 924 self._adorn_subplots()
925
926 def _args_adjust(self):
/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _adorn_subplots(self) 1052 ax.set_xticklabels(xticklabels) 1053 self._apply_axis_properties(ax.xaxis, rot=self.rot,
-> 1054 fontsize=self.fontsize) 1055 elif self.orientation == 'horizontal': 1056 if self._need_to_set_index:
/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _apply_axis_properties(self, axis, rot, fontsize) 1061 1062 def _apply_axis_properties(self, axis, rot=None, fontsize=None):
-> 1063 labels = axis.get_majorticklabels() + axis.get_minorticklabels() 1064 for label in labels: 1065 if rot is not None:
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/axis.py in get_majorticklabels(self) 1155 def get_majorticklabels(self): 1156 'Return a list of Text instances for the major ticklabels'
-> 1157 ticks = self.get_major_ticks() 1158 labels1 = [tick.label1 for tick in ticks if tick.label1On] 1159 labels2 = [tick.label2 for tick in ticks if tick.label2On]
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/axis.py in get_major_ticks(self, numticks) 1284 'get the tick instances; grow as necessary' 1285 if numticks is None:
-> 1286 numticks = len(self.get_major_locator()()) 1287 if len(self.majorTicks) < numticks: 1288 # update the new tick label properties from the old
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in __call__(self)
863 def __call__(self):
864 'Return the locations of the ticks'
--> 865 self.refresh()
866 return self._locator()
867
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in refresh(self)
880 def refresh(self):
881 'Refresh internal information based on current limits.'
--> 882 dmin, dmax = self.viewlim_to_dt()
883 self._locator = self.get_locator(dmin, dmax)
884
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
624 def viewlim_to_dt(self):
625 vmin, vmax = self.axis.get_view_interval()
--> 626 return num2date(vmin, self.tz), num2date(vmax, self.tz)
627
628 def _get_unit(self):
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in num2date(x, tz)
343 tz = _get_rc_timezone()
344 if not cbook.iterable(x):
--> 345 return _from_ordinalf(x, tz)
346 else:
347 x = np.asarray(x)
/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)
223 tz = _get_rc_timezone()
224 ix = int(x)
--> 225 dt = datetime.datetime.fromordinal(ix)
226 remainder = float(x) - ix
227 hour, remainder = divmod(24 * remainder, 1)
ValueError: ordinal must be >= 1
What does it mean?
How can i fix this?
I was getting this error in ipython even with current pandas 0.20.3
Traced it down to having run a script beforehand which saved a figure with a different index, but hadn't done plt.show() as the figure had been saved and I didn't need to see it.
So as #naught101 hinted, forcing plt.close('all') before showing the next figure fixes the issue. Probably good practice at the end of scripts anyway.
This was a bug in pandas: 0.18.1 and fixed in 0.19.2, eg run conda upgrade pandas

Categories