Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:59:49+00:00 2026-06-02T09:59:49+00:00

So the situation is like this. Suppose we have: class Test(datetime): def a(self): return

  • 0

So the situation is like this. Suppose we have:

class Test(datetime):
    def a(self):
        return '{0:02d}{1:02d}{2:02d}{3:02d}'.format(self.year % 100, self.month, self.day, self.hour)
x = [Test(2010, 05, 02, 06), Test(2010, 05, 02, 12)]
y = [0, 1]

Now trying to use:

import matplotlib.pyplot as plt
plt.plot(x, y)

gives a blank graph and the following error:

/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
   2456         ax.hold(hold)
   2457     try:
-> 2458         ret = ax.plot(*args, **kwargs)
   2459         draw_if_interactive()
   2460     finally:
/usr/local/lib/python2.7/dist-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
   3847 
   3848         for line in self._get_lines(*args, **kwargs):
-> 3849             self.add_line(line)
   3850             lines.append(line)
   3851 
/usr/local/lib/python2.7/dist-packages/matplotlib/axes.pyc in add_line(self, line)
   1441             line.set_clip_path(self.patch)
   1442 
-> 1443         self._update_line_limits(line)
   1444         if not line.get_label():
   1445             line.set_label('_line%d'%len(self.lines))
/usr/local/lib/python2.7/dist-packages/matplotlib/axes.pyc in _update_line_limits(self, line)
   1449 
   1450     def _update_line_limits(self, line):
-> 1451         p = line.get_path()
   1452         if p.vertices.size > 0:
   1453             self.dataLim.update_from_path(p, self.ignore_existing_data_limits,
/usr/local/lib/python2.7/dist-packages/matplotlib/lines.pyc in get_path(self)
    642         """
    643         if self._invalidy or self._invalidx:
--> 644             self.recache()
    645         return self._path
    646 
/usr/local/lib/python2.7/dist-packages/matplotlib/lines.pyc in recache(self, always)
    390                 x = ma.asarray(xconv, np.float_)
    391             else:
--> 392                 x = np.asarray(xconv, np.float_)
    393             x = x.ravel()
    394         else:
/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
    233 
    234     """
--> 235     return array(a, dtype, copy=False, order=order)
    236 
    237 def asanyarray(a, dtype=None, order=None):
TypeError: float() argument must be a string or a number

Do any of you know what’s going on? And yes, I know there is a date2num that I could use on x, but I’d like to understand the reason for the error here if it’s possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:59:51+00:00Added an answer on June 2, 2026 at 9:59 am

    Matplotlib uses the __class__ attribute of an object to determine how to handle it. Your Test class isn’t the same as the datetime class:

    >>> from datetime import datetime
    >>> class Test(datetime):
    ...   def a(self):
    ...     return '{0:02d}{1:02d}{2:02d}{3:02d}'.format(self.year % 100, self.month, self.day, self.hour)
    ...
    >>>
    >>> t = Test(2010, 05, 02, 06)
    >>> d = datetime(2010, 05, 02, 06)
    >>>
    >>> getattr(t, '__class__')
    <class '__main__.Test'>
    >>> getattr(d, '__class__')
    <type 'datetime.datetime'>
    

    So matplotlib doesn’t recognise your Test class as a datetime.

    Here’s an ugly hack that works around the problem by registering the Test class so that its handled the same way a datetime is. Check out the matplotlib.units documentation for more information. This code is not guaranteed to work with future versions of matplotlib:

    >>> import matplotlib.pyplot as plt
    >>> import matplotlib.units, matplotlib.dates
    >>> import datetime
    >>>
    >>> class Test(datetime.datetime):
    ...   def a(self):
    ...     return '{0:02d}{1:02d}{2:02d}{3:02d}'.format(self.year % 100, self.month, self.day, self.hour)
    ...
    >>> matplotlib.units.registry[Test] = matplotlib.dates.DateConverter()
    >>>
    >>> x = [Test(2010, 05, 02, 06), Test(2010, 05, 02, 12)]
    >>> y = [0, 1]
    >>> plt.plot(x, y)
    [<matplotlib.lines.Line2D object at 0x02F6E730>]
    

    (I tested this using python 2.7.2 with matplotlib 1.1.0 on Windows)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have situation like this: class IData { virtual void get() = 0; virtual
Suppose I have a class like this class A { private int _x; //must
I have situation like this: user submits form with action='/pay' in '/pay' I have
So I have a situation like this: A needs to call B service and
I have a situation like this: +------------------------------------------------------------------------------+ | | | +---------------------------------------------------+ | | |
I have a situation like this: start(); <Some code> end(); I want start() function
I haven't been able to find anything for a situation like this. I have
The situation is like this: I have an Observable Collection that has a bunch
I just found an interesting situation. Suppose you have some SwingWorker (I've made this
Suppose I have a class like class Empty{ Empty(int a){ cout << a; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.