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

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

Related

I have a problem when I try run a matplotlib command in Python

I have a dataset in python, that is operating is ok, but when I try visualize in matplotlib continuously get a mistake.
I type a viz.plot() command and this is:
This message is.:
TypeError Traceback (most recent call last)
<ipython-input-18-7608c70ebd6e> in <module>
----> 1 viz.plot()
/usr/lib/python3/dist-packages/pandas/plotting/_core.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)
2939 fontsize=fontsize, colormap=colormap, table=table,
2940 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2941 sort_columns=sort_columns, **kwds)
2942 __call__.__doc__ = plot_frame.__doc__
2943
/usr/lib/python3/dist-packages/pandas/plotting/_core.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)
1975 yerr=yerr, xerr=xerr,
1976 secondary_y=secondary_y, sort_columns=sort_columns,
-> 1977 **kwds)
1978
1979
/usr/lib/python3/dist-packages/pandas/plotting/_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
1802 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
1803
-> 1804 plot_obj.generate()
1805 plot_obj.draw()
1806 return plot_obj.result
/usr/lib/python3/dist-packages/pandas/plotting/_core.py in generate(self)
256 def generate(self):
257 self._args_adjust()
--> 258 self._compute_plot_data()
259 self._setup_subplots()
260 self._make_plot()
/usr/lib/python3/dist-packages/pandas/plotting/_core.py in _compute_plot_data(self)
361 "datetime",
362 "datetimetz",
--> 363 "timedelta"])
364
365 try:
/usr/lib/python3/dist-packages/pandas/core/frame.py in select_dtypes(self, include, exclude)
3075 # the "union" of the logic of case 1 and case 2:
3076 # we get the included and excluded, and return their logical and
-> 3077 include_these = Series(not bool(include), index=self.columns)
3078 exclude_these = Series(not bool(exclude), index=self.columns)
3079
/usr/lib/python3/dist-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
273 else:
274 data = _sanitize_array(data, index, dtype, copy,
--> 275 raise_cast_failure=True)
276
277 data = SingleBlockManager(data, index, fastpath=True)
/usr/lib/python3/dist-packages/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
4147
4148 subarr = construct_1d_arraylike_from_scalar(
-> 4149 value, len(index), dtype)
4150
4151 else:
/usr/lib/python3/dist-packages/pandas/core/dtypes/cast.py in construct_1d_arraylike_from_scalar(value, length, dtype)
1199 if is_integer_dtype(dtype) and isna(value):
1200 dtype = np.float64
-> 1201 subarr = np.empty(length, dtype=dtype)
1202 subarr.fill(value)
1203
TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
Does anyone know what the solution might be??
Thank you very much in advance for your response.:
Gabor-Gabor
I suppose "viz" is your dataframe.
To make use of plot() you need to give 2 parameters: plt.plot(x,y) or plt.plot(x,y,viz)

shade area between 2 line plots ValueError: ordinal must be >= 1

I have the following code which correctly renders this:
plt.xlabel('Date')
plt.ylabel('Temp')
plt.title('Min and Max temperature 2005-2014')
# Plotting on the first y-axis
minimum=new_df['min']
maximum=new_df['max']
plt.plot(new_df['Date'], new_df['min'], color='orange', label='Min')
plt.plot(new_df['Date'], new_df['max'], color='olive', label='Max')
Now I need to colour the area between the 2 lines:
I tried this:
plt.fill_between(minimum, maximum, color='#539ecd')
but then I get this error:
ValueError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
/opt/conda/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
225
226 if 'png' in formats:
--> 227 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
228 if 'retina' in formats or 'png2x' in formats:
229 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
/opt/conda/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
117
118 bytes_io = BytesIO()
--> 119 fig.canvas.print_figure(bytes_io, **kw)
120 data = bytes_io.getvalue()
121 if fmt == 'svg':
/opt/conda/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2190 orientation=orientation,
2191 dryrun=True,
-> 2192 **kwargs)
2193 renderer = self.figure._cachedRenderer
2194 bbox_inches = self.figure.get_tightbbox(renderer)
/opt/conda/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
543
544 def print_png(self, filename_or_obj, *args, **kwargs):
--> 545 FigureCanvasAgg.draw(self)
546 renderer = self.get_renderer()
547 original_dpi = renderer.dpi
/opt/conda/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self)
462
463 try:
--> 464 self.figure.draw(self.renderer)
465 finally:
466 RendererAgg.lock.release()
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
61 def draw_wrapper(artist, renderer, *args, **kwargs):
62 before(artist, renderer)
---> 63 draw(artist, renderer, *args, **kwargs)
64 after(artist, renderer)
65
/opt/conda/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer)
1141
1142 mimage._draw_list_compositing_images(
-> 1143 renderer, self, dsu, self.suppressComposite)
1144
1145 renderer.close_group('figure')
/opt/conda/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
137 if not_composite or not has_images:
138 for zorder, a in dsu:
--> 139 a.draw(renderer)
140 else:
141 # Composite any adjacent images together
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
61 def draw_wrapper(artist, renderer, *args, **kwargs):
62 before(artist, renderer)
---> 63 draw(artist, renderer, *args, **kwargs)
64 after(artist, renderer)
65
/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2407 renderer.stop_rasterizing()
2408
-> 2409 mimage._draw_list_compositing_images(renderer, self, dsu)
2410
2411 renderer.close_group('axes')
/opt/conda/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
137 if not_composite or not has_images:
138 for zorder, a in dsu:
--> 139 a.draw(renderer)
140 else:
141 # Composite any adjacent images together
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
61 def draw_wrapper(artist, renderer, *args, **kwargs):
62 before(artist, renderer)
---> 63 draw(artist, renderer, *args, **kwargs)
64 after(artist, renderer)
65
/opt/conda/lib/python3.6/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)
1134 renderer.open_group(__name__)
1135
-> 1136 ticks_to_draw = self._update_ticks(renderer)
1137 ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
1138 renderer)
/opt/conda/lib/python3.6/site-packages/matplotlib/axis.py in _update_ticks(self, renderer)
967
968 interval = self.get_view_interval()
--> 969 tick_tups = [t for t in self.iter_ticks()]
970 if self._smart_bounds:
971 # handle inverted limits
/opt/conda/lib/python3.6/site-packages/matplotlib/axis.py in <listcomp>(.0)
967
968 interval = self.get_view_interval()
--> 969 tick_tups = [t for t in self.iter_ticks()]
970 if self._smart_bounds:
971 # handle inverted limits
/opt/conda/lib/python3.6/site-packages/matplotlib/axis.py in iter_ticks(self)
910 Iterate through all of the major and minor ticks.
911 """
--> 912 majorLocs = self.major.locator()
913 majorTicks = self.get_major_ticks(len(majorLocs))
914 self.major.formatter.set_locs(majorLocs)
/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in __call__(self)
981 def __call__(self):
982 'Return the locations of the ticks'
--> 983 self.refresh()
984 return self._locator()
985
/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in refresh(self)
1001 def refresh(self):
1002 'Refresh internal information based on current limits.'
-> 1003 dmin, dmax = self.viewlim_to_dt()
1004 self._locator = self.get_locator(dmin, dmax)
1005
/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
758 vmin, vmax = vmax, vmin
759
--> 760 return num2date(vmin, self.tz), num2date(vmax, self.tz)
761
762 def _get_unit(self):
/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in num2date(x, tz)
399 tz = _get_rc_timezone()
400 if not cbook.iterable(x):
--> 401 return _from_ordinalf(x, tz)
402 else:
403 x = np.asarray(x)
/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)
252
253 ix = int(x)
--> 254 dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
255
256 remainder = float(x) - ix
ValueError: ordinal must be >= 1
<matplotlib.figure.Figure at 0x7fb311ea1cf8>
Edit:
dataframe looks like this:
Date min max min2015 max2015
0 2014-01-01 -160 156 -133 11
1 2014-01-02 -267 139 -122 39
2 2014-01-03 -267 133 -67 39
3 2014-01-04 -261 106 -88 44
4 2014-01-05 -150 128 -155 28
and I convert the Date to datetime type like this:
new_df['Date'] = pd.to_datetime(new_df['Date'], infer_datetime_format=True)
Edit:
When I run:
plt.fill_between(new_df['Date'], minimum, maximum)
I get this error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-0f6fcbb48fdc> in <module>()
59
60
---> 61 leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')
<ipython-input-23-0f6fcbb48fdc> in leaflet_plot_stations(binsize, hashid)
44 minimum = new_df['min']
45 maximum = new_df['max']
---> 46 plt.fill_between(new_df['Date'], minimum, maximum)
47 #plt.scatter(new_df['Date'], new_df['min2015'], 'o')
48 #plt.scatter(new_df['Date'], new_df['max2015'], 'o')
/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py in fill_between(x, y1, y2, where, interpolate, step, hold, data, **kwargs)
2999 ret = ax.fill_between(x, y1, y2=y2, where=where,
3000 interpolate=interpolate, step=step, data=data,
-> 3001 **kwargs)
3002 finally:
3003 ax._hold = washold
/opt/conda/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1890 warnings.warn(msg % (label_namer, func.__name__),
1891 RuntimeWarning, stacklevel=2)
-> 1892 return func(ax, *args, **kwargs)
1893 pre_doc = inner.__doc__
1894 if pre_doc is None:
/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
4770
4771 # Convert the arrays so we can work with them
-> 4772 x = ma.masked_invalid(self.convert_xunits(x))
4773 y1 = ma.masked_invalid(self.convert_yunits(y1))
4774 y2 = ma.masked_invalid(self.convert_yunits(y2))
/opt/conda/lib/python3.6/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
2343 cls = type(a)
2344 else:
-> 2345 condition = ~(np.isfinite(a))
2346 cls = MaskedArray
2347 result = a.view(cls)
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Looks like the fill is being tried horizontally over the time axis, and minimum and maximum contain values that aren't dates. I've looked up the documentation:
matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)
That was the first error.
Then with numpy newer than 1.17.0, you could just do:
size = 500
minimum = np.random.normal(0, 100, size)
minimum.sort()
minimum = np.random.randint(250, 300, size) - np.abs(minimum)
df = pd.DataFrame(minimum,
pd.date_range("2005-01-01", periods=size, freq="d"),
columns=['min'],
)
df['max'] = df['min'] + np.random.randint(200, 250, size)
fig = df.plot()
fig.fill_between(df.index, df['min'], df['max'], color='#539ecd')
But from that second traceback, we can see that fill_between is trying ~(np.isfinite(a)) on all the axes. Which isn't supported on older numpy.datetime64, the type of your x-axis.
So we will have to use a numeric x-axis and then change the labels.
df = pd.DataFrame(minimum,
columns=['min'],
)
df['max'] = df['min'] + np.random.randint(200, 250, size)
fig = df.plot()
fig.fill_between(df.index, df['min'], df['max'], color='#539ecd')
# We take the original datetime axis
date_axis = pd.date_range("2005-01-01", periods=size, freq="d")
# and map a function from (axis, tick) -> wanted string
def label(axis, tick):
tick = int(tick)
if tick == len(axis):
tick -= 1
if 0 <= tick < len(axis):
return f"{axis[tick].year}-{axis[tick].month}"
else:
return ' '
fig.set_xticks(fig.get_xticks()) #silence a warning
fig.set_xticklabels(
[label(date_axis, tick) for tick in fig.get_xticks()]
)
fill_between takes the x coordinates as first argument. The following should work:
plt.fill_between(new_df["Date"], minimum, maximum, color="lemonchiffon")
Note that using matplotlib 3.4 I could not reproduce the error. Whether the values in Date were converted to dates or were left as strings, fill_between(minimum, maximum) does not throw an error but does produce unexpected plots.
Edit
Using numpy 1.11.3 and matplotlib 2.0.2, I've been able to reproduce the TypeError raises by np.isfinite (see this post for more information). Indeed this function supports datetime64 only from version 1.17. I highly suggest that you update the versions of matplotlib and numpy. However, using the earlier versions described in this paragraph, the error was bypassed by explicitly casting the dates:
plt.fill_between(np.array(new_df["Date"]), minimum, maximum, color='lemonchiffon')

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)

Why KeyError while plottin a pandas data frame with matplotlib? [duplicate]

This question already has answers here:
KeyError when plotting a sliced pandas dataframe with datetimes
(3 answers)
Closed 7 years ago.
I have this data frame:
date_obj col1 col2 col3 col4
40038 2012-11-19 1.000 0.831856 0.986209 0.843919
40039 2012-11-20 2.015 0.521764 1.177320 0.938245
40040 2012-11-21 1.160 1.645345 1.964620 4.536440
40041 2012-11-22 3.171 2.444018 2.931550 3.737840
40042 2012-11-23 4.563 3.208111 3.587250 2.434040
40043 2012-11-24 5.379 3.863732 3.824540 1.634780
40044 2012-11-26 1.125 20.756739 4.162820 23.552100
40045 2012-11-27 3.340 5.369354 4.535090 1.129290
40046 2012-11-28 5.463 12.185730 8.102790 1.224300
40047 2012-11-29 6.596 14.328685 9.271000 24.655600
40048 2012-11-30 31.544 13.513497 12.103400 21.273500
40049 2012-12-01 24.921 26.144050 16.256200 13.883100
40050 2012-12-03 5.488 2.581351 7.220790 3.349450
40051 2012-12-04 6.977 5.893819 5.548870 2.948770
40052 2012-12-05 7.115 6.533022 5.863820 2.517030
40053 2012-12-06 5.842 8.754232 7.518660 1.447940
40054 2012-12-07 6.346 12.018631 10.263100 11.837400
40055 2012-12-08 17.666 4.548846 10.610400 11.110800
40056 2012-12-10 4.300 2.823566 1.475000 1.989210
40057 2012-12-11 2.415 2.436319 2.677440 2.908270
40058 2012-12-12 2.319 2.121092 3.455550 3.890480
40059 2012-12-13 1.000 1.633918 3.858540 4.316940
40060 2012-12-14 2.238 1.688475 5.065990 5.267850
40061 2012-12-15 1.798 2.621267 7.175370 6.957340
I try to plot it in the following way:
plt.figure(figsize=(17, 10))
plt.setp(plt.xticks()[1], rotation=45)
plt.plot_date(df_cut['date_obj'],df_cut['col1'], color='black', linestyle='-', markersize=3, linewidth=2)
plt.plot_date(df_cut['date_obj'],df_cut['col2'], color='red', linestyle='-', markersize=3)
plt.plot_date(df_cut['date_obj'],df_cut['col3'], color='green', linestyle='-', markersize=3)
plt.plot_date(df_cut['date_obj'],df_cut['col4'], color='blue', linestyle='-', markersize=3)
As a result I get an error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-544-1b8650d1e7e7> in <module>()
/ipython/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot_date(x, y, fmt, tz, xdate, ydate, hold, **kwargs)
2850 try:
2851 ret = ax.plot_date(x, y, fmt=fmt, tz=tz, xdate=xdate, ydate=ydate,
-> 2852 **kwargs)
2853 draw_if_interactive()
2854 finally:
ipython/local/lib/python2.7/site-packages/matplotlib/axes.pyc in plot_date(self, x, y, fmt, tz, xdate, ydate, **kwargs)
4061 if not self._hold: self.cla()
4062
-> 4063 ret = self.plot(x, y, fmt, **kwargs)
4064
4065 if xdate:
ipython/local/lib/python2.7/site-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
3994 lines = []
3995
-> 3996 for line in self._get_lines(*args, **kwargs):
3997 self.add_line(line)
3998 lines.append(line)
ipython/local/lib/python2.7/site-packages/matplotlib/axes.pyc in _grab_next_args(self, *args, **kwargs)
328 return
329 if len(remaining) <= 3:
--> 330 for seg in self._plot_args(remaining, kwargs):
331 yield seg
332 return
ipython/local/lib/python2.7/site-packages/matplotlib/axes.pyc in _plot_args(self, tup, kwargs)
306 x = np.arange(y.shape[0], dtype=float)
307
--> 308 x, y = self._xy_from_xy(x, y)
309
310 if self.command == 'plot':
python/local/lib/python2.7/site-packages/matplotlib/axes.pyc in _xy_from_xy(self, x, y)
222 def _xy_from_xy(self, x, y):
223 if self.axes.xaxis is not None and self.axes.yaxis is not None:
--> 224 bx = self.axes.xaxis.update_units(x)
225 by = self.axes.yaxis.update_units(y)
226
ipython/local/lib/python2.7/site-packages/matplotlib/axis.pyc in update_units(self, data)
1299 neednew = self.converter != converter
1300 self.converter = converter
-> 1301 default = self.converter.default_units(data, self)
1302 #print 'update units: default=%s, units=%s'%(default, self.units)
1303 if default is not None and self.units is None:
ipython/local/lib/python2.7/site-packages/matplotlib/dates.pyc in default_units(x, axis)
1156 'Return the tzinfo instance of *x* or of its first element, or None'
1157 try:
-> 1158 x = x[0]
1159 except (TypeError, IndexError):
1160 pass
ipython/local/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
611 def __getitem__(self, key):
612 try:
--> 613 return self.index.get_value(self, key)
614 except InvalidIndexError:
615 pass
ipython/local/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
761 """
762 try:
--> 763 return self._engine.get_value(series, key)
764 except KeyError, e1:
765 if len(self) > 0 and self.inferred_type == 'integer':
What is strange, this code works for some data frames and for some it doesn't. The data frames are not different by their structure. The only difference between them is only in values that they contain.
Could anybody please help me to resolve this problem?
Dataframe store dates as numpy.datetime64 objects, not python datetime objects.
Furthermore matplotlib.plot_date uses its own numeric representation of dates.
You could draw your data this way:
plt.plot_date(matplotlib.dates.date2num(pandas.to_datetime(df_cut['date_obj'].values)),df_cut['col1'].values, color='black', linestyle='-', markersize=3, linewidth=2)
Or you could define column 'date_obj' as the index of your data:
df0 = pd.DataFrame.from_records(YourDataSource, columns=['date_obj','col1','col2','col3','col4'],index='date_obj')
And then simply use pandas' plot() attribute:
df0['col1'].plot()

Bar chart in pandas on time series data

I am trying to do bar chart in pandas on time series data.
Documentation says it is not possible: http://pandas.pydata.org/pandas-docs/stable/visualization.html#bar-plots
Is there some workaround ?
This is my code
# there must be ORDER BY, other wise rows will not be ordered
df = sql.read_frame("SELECT * FROM hzmo_report ORDER BY datum;", cnx, index_col='datum')
df.index = pd.to_datetime(df.index) # converting to DatetimeIndex
df['korisnika'].plot(ax=axs1[0], title='SOMETHING', marker='o')
df['korisnika'].diff().plot(ax=axs1[1], title='SOMETHING', marker='o') # i would like this to be bar plot
If I do
df['korisnika'].diff().plot(kind='bar', ax=axs1[1], title='SOMETHING', marker='o')
I have just added kind='bar'
I get:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-109-d41eb2b2e3a7> in <module>()
36 fig1.suptitle('Umirovljenici', fontsize=16)
37 df['korisnika'].plot(ax=axs1[0], title='Broj korisnika mirovine', marker='o')
---> 38 ( df['korisnika'].diff() ).plot(ax=axs1[1], kind='bar', title='Apsolutna razlika naspram prethodnog mjeseca', marker='o')
39 #df['korisnika'].diff().hist()
40
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\tools\plotting.pyc in plot_series(series, label, kind, use_index, rot, xticks, yticks, xlim, ylim, ax, style, grid, legend, logy, secondary_y, **kwds)
1504 secondary_y=secondary_y, **kwds)
1505
-> 1506 plot_obj.generate()
1507 plot_obj.draw()
1508
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\tools\plotting.pyc in generate(self)
731 self._compute_plot_data()
732 self._setup_subplots()
--> 733 self._make_plot()
734 self._post_plot_logic()
735 self._adorn_subplots()
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\tools\plotting.pyc in _make_plot(self)
1291 else:
1292 rect = bar_f(ax, self.ax_pos + i * 0.75 / K, y, 0.75 / K,
-> 1293 start=pos_prior, label=label, **kwds)
1294 rects.append(rect)
1295 labels.append(label)
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\tools\plotting.pyc in f(ax, x, y, w, start, **kwds)
1251 if self.kind == 'bar':
1252 def f(ax, x, y, w, start=None, **kwds):
-> 1253 return ax.bar(x, y, w, bottom=start, **kwds)
1254 elif self.kind == 'barh':
1255 def f(ax, x, y, w, start=None, **kwds):
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\matplotlib\axes.pyc in bar(self, left, height, width, bottom, **kwargs)
4779 label='_nolegend_'
4780 )
-> 4781 r.update(kwargs)
4782 r.get_path()._interpolation_steps = 100
4783 #print r.get_label(), label, 'label' in kwargs
C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\matplotlib\artist.pyc in update(self, props)
657 func = getattr(self, 'set_'+k, None)
658 if func is None or not callable(func):
--> 659 raise AttributeError('Unknown property %s'%k)
660 func(v)
661 changed = True
AttributeError: Unknown property marker
You can plot a bar-plot of a time-series. Not that useful IMHO though.
ts = Series(randn(20),date_range('20130101',periods=20))
ts.plot()
A time-series line-plot
A Bar Plot

Categories