Comments (8)
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:
- Set a default date format in
NetCDFTimeDateFormatter
if the locator does not have aresolution
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. - 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.
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.
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))
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))
from nc-time-axis.
@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.
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.
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.
See #84 for a proof of concept.
from nc-time-axis.
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)
- Add citation information? HOT 14
- Implement a cftime-compatible version of matplotlib's `ConciseDateFormatter`
- Add an example of a(n horribly) wrong plot when NOT using nc-time-axis HOT 5
- Request for contributor ORCIDs HOT 9
- how to best set lim? // allow string in `convert`? HOT 3
- small change to tests needed for upcoming cftime 1.3.0 release HOT 2
- Cannot import nc_time_axis (v1.2.0, python v3.8.5, conda environment) HOT 1
- New minor release? HOT 5
- New cftime 1.5.0 breaks nc-time-axis HOT 1
- Documentation HOT 2
- Reboot nc-time-axis
- investigate use of matplotlib.use("agg") HOT 5
- nc-time-axis==1.3.1 impossible to install with scitools-iris==3.0.2 HOT 14
- `nc-time-axis` dates don't work with `plt.fill_between(...)` HOT 5
- Enable plotting of now calendar-aware `cftime.datetime` objects HOT 1
- Deprecate `CalendarDateTime`? HOT 1
- Update requirements
- Switch internal time units to microseconds
- `NetCDFDateTimeLocator.min_n_ticks` not used
- matplotlib deprecates munits.ConversionInterface.is_numlike
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nc-time-axis.