Skip to content

Using ginput on a plot_date plot doesn't return datetime #7462

Open

Description

@dstansby

When using ginput() on a plot made using plot_date(), the return type of the x value is float, but it should be datetime.

Minimal example:

import matplotlib.pyplot as plt
from datetime import datetime, timedelta

times = []
data = []
for i in range(0, 10):
    times.append(datetime(2016, 1, 1, 0, 0, 0) + timedelta(seconds=i))
    data.append(i)

plt.plot_date(times, data)
x = plt.ginput()
print(x[0][0])
print(type(x[0][0]))

The last statement prints "<class 'numpy.float64'>"

  • Present on 2.0.0b4+2828.g86b32ca (installed from source using pip on Linux)

Activity

  1. changed the title [-]Using ginput on a plot_date plot doeson't return datetime[/-] [+]Using ginput on a plot_date plot doesn't return datetime[/+] on Nov 15, 2016
  2. modified the milestones: , on Nov 20, 2016
  3. tacaswell commented on Nov 20, 2016

    @tacaswell
    Member

    Not sure if we actually know enough to be able to do this in general right now.

    datetime is handled through the unit framework which (roughly) works by:

    • registering a function that converts units -> float
    • registering a formatter that converts float -> string representation of value

    I think you are asking to also provide an inverse from float -> the input data type with units.

    It should be possible to add this in a back compatible way, but will need a bit of thought.

    For this particular case, iirc the float is seconds from 1900 days from 0001-01-01 UTC, plus 1.

  4. efiring commented on Nov 20, 2016

    @efiring
    Member

    It's days from a ridiculous but common sort of origin, long, long, ago. See http://matplotlib.org/api/dates_api.html for the explanation.

  5. dstansby commented on Feb 28, 2017

    @dstansby
    MemberAuthor

    Would it not be possible to use num2date to convert the output of ginput directly to a datetime? http://matplotlib.org/api/dates_api.html#matplotlib.dates.num2date

  6. mitko1994 commented on Mar 16, 2017

    @mitko1994

    @tacaswell, can you please provide more information on the unit framework? Where in the code is it? I traced the function calls to BlockingMouseInput but I don't understand exactly how and where the mouse click coordinates are retrieved. Perhaps some information on that might also help me.

  7. mitko1994 commented on Apr 3, 2017

    @mitko1994

    After thorough investigation of this bug as a team for our project at UofT, we came to the conclusion there isn't a really good solution to this problem.

    We had the following fix in mind:

    1. introduce a new parameter to ginput called converter which we will use to pass the desired converter function to ginput.
    2. the converter function passed must take a float
    3. The output of ginput() will now be the output of the converter function or a float if no converter is passed in.

    Advantages of the fix:
    1.The float that ginput() generates, can be converted to any type as long as the user provides a converter function.
    2. You can technically pass in any function in place of the converter and use it to deduce information about the data

    Disadvantages of the fix:

    1. The output type of ginput() is inconsistent. This is a sign of bad software design although python permits it.
    2. No real change to what's available now. The only thing is that the conversion step is done implicitly. This however, introduces the inconsistency in the output type of ginput().

    Conclusion
    The current design of ginput() is the most appropriate one. Conversions should be done explicitly to preserve the consistency of the output type of ginput(). Perhaps there may be another way to work this out by introducing polymorphism for all the possible types that could be used in a plot and change ginput to have an output of the parent type, however that will require quite a bit of restructuring and the benefit may not be worth the effort.

    @tacaswell, do you still consider this a feature that's worth implementing? If not, can we close this issue? Thanks

  8. tacaswell commented on Apr 3, 2017

    @tacaswell
    Member

    I think the best approach would be to add an example that uses ginput + num2date to get out the desired datatime with some comments about how if you are using unitful data + ginput the user is responsible for taking care of the inverse transformation.

  9. mitko1994 commented on Apr 10, 2017

    @mitko1994

    Thanks @tacaswell. Under what subsection of examples should we add this example? Also, are we responsible for adding the HTML pages and links for the new example we will create?

  10. 7 remaining items

  11. added this to the milestone on Mar 24, 2018
  12. modified the milestones: v3.0, needs sorting on Jul 26, 2018
  13. github-actions commented on Apr 5, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  14. jklymak commented on Apr 5, 2023

    @jklymak
    Member

    I think most converters have inverses? If so we could just have an option to ginout to change the output type. The inverse isn't unique, but could at least be in the units/type the user wants. Note that the cursor status in the lower right already does this.

  15. added
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Apr 5, 2023
  16. oscargus commented on Apr 5, 2023

    @oscargus
    Member

    Marking as good first issue. Basically the following should be done:

    1. Add a parameter to ginput that makes a conversion to the input format (through the converter inverse) with documentation
    2. Figure out how to get that value, looking at e.g. the status message formatting:
      def _mouse_event_to_message(event):
      if event.inaxes and event.inaxes.get_navigate():
      try:
      s = event.inaxes.format_coord(event.xdata, event.ydata)
      except (ValueError, OverflowError):
      pass
      else:
      s = s.rstrip()
      artists = [a for a in event.inaxes._mouseover_set
      if a.contains(event)[0] and a.get_visible()]
      if artists:
      a = cbook._topmost_artist(artists)
      if a is not event.inaxes.patch:
      data = a.get_cursor_data(event)
      if data is not None:
      data_str = a.format_cursor_data(data).rstrip()
      if data_str:
      s = s + '\n' + data_str
      return s
      return ""
    3. Add a test

    Note that one will have to dig a bit deeper, as we are not really interested in the string formatting of the cursor position here.

  17. turnipseason commented on May 29, 2023

    @turnipseason
    Contributor

    Hi! I wanted to work on this. I have a question -- since ginput returns (x, y), should we pass two separate converters in case the axes are using different units (for example plotting datetime against floats, etc)?

  18. efiring commented on May 29, 2023

    @efiring
    Member

    Looking back through the comments, the recommendation from @tacaswell was that this option should not be added to ginput. Instead, an example should be added to the documentation showing how to convert the output of ginput back to the desired units. That was 5 years ago. To be clear: is the reversal of this guidance coming because of changes in mpl in the interim, or is it just a different opinion?

  19. removed
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions