Skip to content

can not draw barplot with x datetime axis. #130

Closed

Description

@NMC-DAVE

Hi:
Proplot is vary great package and hope to update continuously. I want to draw barplot, like the code:

import pandas as pd
import proplot as plot
df = pd.DataFrame({'a': [0,1], 'date':[pd.to_datetime('2017-04-01'), pd.to_datetime('2017-04-02')]})
f, axs = plot.subplots(ncols=1, figsize=(12, 6))
axs[0].bar(df['date'], df['a'])
axs.format(title="TEST", xlabel='x axis', ylabel='y axis',
           large=20, small=10, xlocator='year', xminorlocator='month', xformatter='%Y')

but I get the fellowing error. So did i do anything wrong. Thanks very much.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-7ba278c889ae> in <module>
      2 df = pd.DataFrame({'a': [0,1], 'date':[pd.to_datetime('2017-04-01'), pd.to_datetime('2017-04-02')]})
      3 f, axs = plot.subplots(ncols=1, figsize=(12, 6))
----> 4 axs[0].bar(df['date'], df['a'])
      5 axs.format(title="TEST", xlabel='x axis', ylabel='y axis',
      6            large=20, small=10, xlocator='year', xminorlocator='month', xformatter='%Y')

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in _wrapper(self, *args, **kwargs)
   3081         @functools.wraps(func)
   3082         def _wrapper(self, *args, **kwargs):
-> 3083             return driver(self, func, *args, **kwargs)
   3084         name = func.__name__
   3085         if name not in proplot_methods:

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in bar_wrapper(self, func, x, height, width, bottom, left, vert, orientation, stacked, lw, linewidth, edgecolor, **kwargs)
   1166         linewidth=lw, edgecolor=edgecolor,
   1167         stacked=stacked, orientation=orientation,
-> 1168         **kwargs
   1169     )
   1170 

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in _wrapper(self, *args, **kwargs)
   3081         @functools.wraps(func)
   3082         def _wrapper(self, *args, **kwargs):
-> 3083             return driver(self, func, *args, **kwargs)
   3084         name = func.__name__
   3085         if name not in proplot_methods:

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in standardize_1d(self, func, *args, **kwargs)
    328     # WARNING: For some functions, e.g. boxplot and violinplot, we *require*
    329     # cycle_changer is also applied so it can strip 'x' input.
--> 330     return func(self, x, *ys, *args, **kwargs)
    331 
    332 

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in _wrapper(self, *args, **kwargs)
   3081         @functools.wraps(func)
   3082         def _wrapper(self, *args, **kwargs):
-> 3083             return driver(self, func, *args, **kwargs)
   3084         name = func.__name__
   3085         if name not in proplot_methods:

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in add_errorbars(self, func, medians, means, boxes, bars, boxdata, bardata, boxstd, barstd, boxmarker, boxmarkercolor, boxrange, barrange, boxcolor, barcolor, boxlw, barlw, capsize, boxzorder, barzorder, *args, **kwargs)
    822     else:
    823         xy = (x, iy)  # just the stats
--> 824     obj = func(self, *xy, *args, **kwargs)
    825     if not boxes and not bars:
    826         return obj

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in _wrapper(self, *args, **kwargs)
   3081         @functools.wraps(func)
   3082         def _wrapper(self, *args, **kwargs):
-> 3083             return driver(self, func, *args, **kwargs)
   3084         name = func.__name__
   3085         if name not in proplot_methods:

~/anaconda3/lib/python3.7/site-packages/proplot/wrappers.py in cycle_changer(self, func, cycle, cycle_kw, label, labels, values, legend, legend_kw, colorbar, colorbar_kw, *args, **kwargs)
   1662         if name in ('bar',):  # adjust
   1663             if not stacked:
-> 1664                 ix = x + (i - ncols / 2 + 0.5) * width / ncols
   1665             elif stacked and not is1d:
   1666                 key = 'x' if barh else 'bottom'

~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/common.py in new_method(self, other)
     62         other = item_from_zerodim(other)
     63 
---> 64         return method(self, other)
     65 
     66     return new_method

~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/__init__.py in wrapper(left, right)
    498         lvalues = extract_array(left, extract_numpy=True)
    499         rvalues = extract_array(right, extract_numpy=True)
--> 500         result = arithmetic_op(lvalues, rvalues, op, str_rep)
    501 
    502         return _construct_result(left, result, index=left.index, name=res_name)

~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op, str_rep)
    191         #  by dispatch_to_extension_op.
    192         # Timedelta is included because numexpr will fail on it, see GH#31457
--> 193         res_values = dispatch_to_extension_op(op, lvalues, rvalues)
    194 
    195     else:

~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/dispatch.py in dispatch_to_extension_op(op, left, right)
    123     # The op calls will raise TypeError if the op is not defined
    124     # on the ExtensionArray
--> 125     res_values = op(left, right)
    126     return res_values

TypeError: unsupported operand type(s) for +: 'DatetimeArray' and 'float'

Activity

  1. lukelbd commented on Mar 11, 2020

    @lukelbd
    Collaborator

    Nope, you did nothing wrong; proplot overrides the bar method to add some features, but I did not account for the possibility of datetime x-coordinates (similar to PR #121). The next version will fix this. Thanks for the reports!

  2. lukelbd commented on May 10, 2020

    @lukelbd
    Collaborator

    This is fixed in #156. It also preserves the bar-grouping capability when detecting y data with more than one column.

    Here is an example with single-column data:

    import proplot as plot
    dates = np.arange('2000-01-01', '2000-01-10', dtype=np.datetime64)
    data = np.random.rand(len(dates))
    fig, axs = plot.subplots(ncols=1, width=6, aspect=2)
    axs[0].bar(dates, data)
    axs.format(title='Test', xlabel='x axis', ylabel='y axis')
    fig.save('~/tmp/tmp.png')

    tmp

    And here is a multi-column example:

    import proplot as plot
    dates = np.arange('2000-01-01', '2000-01-10', dtype=np.datetime64)
    data = np.random.rand(len(dates), 5)
    cycle = plot.Cycle('magma', N=data.shape[1])
    fig, axs = plot.subplots(ncols=1, width=6, aspect=2)
    axs[0].bar(dates, data, cycle=cycle)
    axs.format(title='Test', xlabel='x axis', ylabel='y axis')
    fig.save('~/tmp/tmp.png')

    tmp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions