Skip to content

Secondary axis called with [0, 1] might produce exceptions in case these are invalid data #14270

Closed
@maxnoe

Description

@maxnoe

Bug report

Transform functions for the new feature secondary_xaxis are always called with 0 and 1 at
instantiation, which might be invalid input for transformations.

Code for reproduction

E.g. trying to get a secondary axis showing mjd for a date axis

import matplotlib.pyplot as plt
from matplotlib.dates import date2num, num2date
from datetime import datetime, timedelta
import numpy as np


MJDREF = datetime(1858, 11, 17, 0)


def trans(mpl_date):
    print(mpl_date)
    return (num2date(mpl_date) - MJDREF).total_seconds() / 86400


def inv(mjd):
    return date2num(MJDREF + timedelta(days=mjd))


t = np.array(['2018-01-01', '2018-03-03', '2018-04-10'], dtype='datetime64[s]')


fig, ax = plt.subplots()
ax.plot(t, [1, 2, 1])

t_ax = ax.secondary_xaxis('top', functions=(trans, inv))

Results in:

ValueError: Cannot convert 0 to a date.  This often happens if non-datetime values are passed to an axis that expects datetime objects.

Matplotlib version

  • Operating system: linux, but should be indepenent
  • Matplotlib version: 3.1
  • Matplotlib backend: all
  • Python version: 3.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions