Giter Club home page Giter Club logo

Comments (8)

spencerkclark avatar spencerkclark commented on June 15, 2024 1

The issue in @MartinDix's initial comment is a symptom of the issue identified in #79 (comment): the ndays attribute -- now the resolution attribute -- cannot be computed without knowing the range of the axis, which is not known at the time the locator is constructed. In the case of setting the ticks it seems that matplotlib creates a new locator and so any axis range information is lost.

This is OK. I propose working around this in nc-time-axis in the following way:

  1. Set a default date format in NetCDFTimeDateFormatter if the locator does not have a resolution attribute. I would suggest "%Y-%m-%d" to be consistent with matplotlib. Note that when a user explicitly sets the ticks for a datetime axis, matplotlib also uses a default format.
  2. Create another formatter class that will let users set their own date formats to override this default format if they desire. Matplotlib does this with their DateFormatter class (see for example here). I think this would be a useful feature regardless.

To make the analogy with matplotlib clear, I might suggest renaming NetCDFTimeDateFormatter to AutoCFTimeFormatter -- possibly after a deprecation cycle, though I think it is unlikely anyone is using it outside nc-time-axis -- and naming the new formatter proposed in (2) CFTimeFormatter.

Does anyone in @SciTools/nc-time-axis-devs or this issue have any thoughts on this proposal?

from nc-time-axis.

adamcpovey avatar adamcpovey commented on June 15, 2024

I have a similar problem. A minimal example is that,

import matplotlib.pyplot as plt
import nc_time_axis
import numpy
import cftime

x = [nc_time_axis.CalendarDateTime(cftime.datetime(y, 1, 1), "360_day")
     for y in range(2000, 2010)]
y = numpy.arange(10)
plt.scatter(x, y)
plt.show()

only works if numpy.array is wrapped around the definition of x.

I believe the solution is to put the following here,

        if isinstance(value, np.ndarray) or isinstance(value, list):

from nc-time-axis.

bradyrx avatar bradyrx commented on June 15, 2024

I think I'm having a similar problem here. There is an incompatibility between nc-time-axis and native matplotlib datetime locators. See my issue at proplot-dev/proplot#126.

With a numpy datetime axis:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import xarray as xr

times = np.arange('1990', '2000', dtype='datetime64[M]')
data = xr.DataArray(
    np.random.rand(len(times)),
    dims=['time'],
    coords=[times]
)

f, ax = plt.subplots()

ax.plot(data.time, data)
ax.xaxis.set_major_locator(mdates.YearLocator(2))

Screen Shot 2020-03-16 at 11 17 40 AM

With a cftime axis:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import xarray as xr

times = xr.cftime_range('1990', '2000', freq='M')
data = xr.DataArray(
    np.random.rand(len(times)),
    dims=['time'],
    coords=[times]
)

f, ax = plt.subplots()

ax.plot(data.time, data)
ax.xaxis.set_major_locator(mdates.YearLocator(1))

Screen Shot 2020-03-16 at 11 18 05 AM

from nc-time-axis.

bradyrx avatar bradyrx commented on June 15, 2024

@lukelbd, do you think this would be a relatively quick fix? This looks like a somewhat dead repo given how old this issue is. I am very unfamiliar with these locators. Would it just require a few lines of code in __init__? Any thoughts?

from nc-time-axis.

lukelbd avatar lukelbd commented on June 15, 2024

This might be a pretty big PR unfortunately, but shouldn't be very conceptually complicated. You basically need to copy + modify YearLocator, MonthLocator, etc. in matplotlib/dates.py, and that section of code is ~300 lines long. You'd have to go through each line and convert the datetime64 syntax to nc-time-axis syntax -- e.g. wherever they get integer year from a datetime64 array, change that to how you'd get the integer year from a cftime array, etc.

If you have time go for it, but otherwise it might make more sense to convert your nc-time-axis times to datetime64 if possible, even if the calendars don't technically match (my understanding is the main difference between these two formats is that nc-time-axis can work with different calendar types?). Also if you don't get a response to a PR submitted to this repo, I can add your locators directly to proplot just like I add some fixes for currently active cartopy/matplotlib issues.

from nc-time-axis.

lukelbd avatar lukelbd commented on June 15, 2024

Oh, now I see the specific issue at the top is different and possibly easier to fix.

The user was trying to manually set the tick locations by sending a cftime array to set_xticks, which results in matplotlib applying a FixedLocator as the axis locator. However, nc-time-axis evidently expects the axis locator to be its own special locator, and raises an error otherwise (at least this is what I suspect is happening; you should try to print(self.locator) in that example). I don't think there is an issue when using FixedLocator with datetime64 coordinates, so this appears to be an unfortunate limitation of nc-time-axis. Might be simple to fix or might be complicated, depends on how this was designed. But you'll need to play round.

Since this repo has had no commits for >1 year I'd be open to just adding the entire repo's codebase to proplot. Definitely fits within proplot's scope of easier plotting/xarray integration. But that's a long term goal... maybe this summer.

from nc-time-axis.

spencerkclark avatar spencerkclark commented on June 15, 2024

See #84 for a proof of concept.

from nc-time-axis.

spencerkclark avatar spencerkclark commented on June 15, 2024

The proposal in #41 (comment) has been implemented in #84. See the PR description for an example of using the new CFTimeFormatter.

from nc-time-axis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.