FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

DOC: Better error when float on datetime axis by jklymak · Pull Request #10084 · matplotlib/matplotlib · GitHub

Repository navigation

DOC: Better error when float on datetime axis - #10084

Merged
dstansby merged 1 commit into
matplotlib:masterfrom
jklymak:doc-better-data-error
Dec 28, 2017
Merged

dstansby merged 1 commit into
matplotlib:masterfrom
jklymak:doc-better-data-error

Conversation

jklymak commented Dec 24, 2017

Copy link
Copy Markdown
Member

PR Summary

#9211 points out that the error message after

import matplotlib.pyplot as plt
from datetime import datetime

fig, ax = plt.subplots()
ax.plot(datetime(2017, 1, 1), 1)
ax.plot(1, 1)
plt.show()

yields the mysterious error:

ValueError: ordinal must be >= 1

This PR catches earlier and returns:

ValueError: view limit minimum -36815.450000000004 < 1. This often happens if you pass a float to an axis that has datetime units

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Comment thread lib/matplotlib/dates.py Outdated

if dmin < 1:
raise ValueError('datalim minimum {} < 1. This often '
'happens if you pass a float to an '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

indent

Comment thread lib/matplotlib/dates.py Outdated
raise ValueError('cannot convert {} < 1 to a date. This '
'often happens if floats are passed to '
'a Matplotlib axis that expects datetime objects. '
''.format(ix))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

you don't need the '', starting the line with a dot binds to the string on the previous line

Comment thread lib/matplotlib/dates.py Outdated
if ix < 1:
raise ValueError('cannot convert {} < 1 to a date. This '
'often happens if floats are passed to '
'a Matplotlib axis that expects datetime objects. '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

to an axis (no need to mpl, which you also did not include below)

anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

minor formatting/wording can be improved but nothing blocking

jklymak force-pushed the doc-better-data-error branch from f02880f to 50969c4 Compare December 24, 2017 19:49
Comment thread lib/matplotlib/dates.py Outdated

if dmin < 1:
raise ValueError('datalim minimum {} < 1. This often '
'happens if you pass a float to an '

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can "float" be change to something less technical like "number" or "normal number"?

story645 Dec 25, 2017 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

value that is not a datetime?

Copy link
Copy Markdown
Member

(apart from my suggested change I am +10000 on this, since I remember in my early Matplotlib days spending ages meaning what on earth the current error message meant)

jklymak commented Dec 26, 2017

Copy link
Copy Markdown
Member Author

Yep I’ll fix it this pm. No problem making it even clearer.

jklymak force-pushed the doc-better-data-error branch from 50969c4 to 88a19c2 Compare December 26, 2017 22:55

jklymak commented Dec 26, 2017

Copy link
Copy Markdown
Member Author

Done - I couldn't get a test to work, however. 🐑 If I do:

g, ax = plt.subplots()
ax.plot(datetime(2017, 1, 1), 1)
ax.plot(1, 1)
fig.draw(fig.canvas.get_renderer())

I get the error. Trying to do this in dates_test.py I get:

  def test_bad_datetime():
        with pytest.raises(ValueError):
            fig, ax = plt.subplots()
            ax.plot(datetime.datetime(2017, 1, 1), 1)
            ax.plot(1, 1)
>           fig.draw(fig.canvas.get_renderer())
E           Failed: DID NOT RAISE <class 'ValueError'>

Comment thread lib/matplotlib/dates.py Outdated

ix = int(x)
if ix < 1:
raise ValueError('cannot convert {} < 1 to a date. This '

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think should be:
cannot convert {} to a date

{} < 1 evaluate to a bool 😉

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

OK. Changed to

                             'view limit minimum {} is less than 1 and '
                             'is an invalid Matplotlib date value. This '
                             'often happens if you pass a non-datetime '
                             'value to an axis that has datetime units'
                             .format(vmin))

jklymak force-pushed the doc-better-data-error branch from 88a19c2 to 38b137b Compare December 26, 2017 23:49
jklymak force-pushed the doc-better-data-error branch from 38b137b to a4759e8 Compare December 27, 2017 05:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL