Skip to main content

datetick

Sensible date/time tick labels for Matplotlib

Motivation

Matplotlib's default date/time tick labels are often poor, and adjusting them requires using locators and formatters on an ad-hoc basis. A method or package for handling arbitrary time ranges does not exist.

In addition, the interfaces for locators and formatters complex and non-intuitive and require study and experimentation (e.g., [1], [2], [3], [4]. Tilting labels is an often-suggested solution, but this should not be needed.

datetick() contains logic for locators and formatters that apply to plots with arbitrary time ranges. One only needs to add the command datetick() after the usual plt.plot(...) command to have sensible and useable time tick labels. The primary configuration is a set of rules that account for the time range and an adjustable minimum gap between tick labels.

To prevent overlap and enforce a minimum gap, the font size is automatically reduced to a chosen minimum value. Then the number of ticks are reduced based on rules in rules.json.

Usage

import datetime as dt
import matplotlib.pyplot as plt
from datetick import datetick

dt1 = dt.datetime(2011, 1, 2)
dt2 = dt1 + dt.timedelta(days=1, hours=1, minutes=1)

plt.plot([dt1, dt2], [0.0,1.0])
datetick()
plt.show()
# or
# datetick('x') (use 'y' if y variable is datetime-like)
# or
# datetick('x', axes=plt.gca())
# or
# fig, axes = plt.subplots(2)
# plt.plot([dt1, dt2], [0.0, 1.0])
# datetick('x', axes=axes[0])

Comparison to default Matplotlib

Python-3.12/Matplotlib-3.8.4

axis=x

1.

test/visual_test/latest/20030901000000-20031201000000-6.4in_v1.svg

2.

test/visual_test/latest/19991231220000-20000102020000-6.4in_v1.svg

3.

test/visual_test/latest/19991231220000-20000102020000-6.4in_v2.svg

4.

test/visual_test/latest/19991231220000-20000102020000-12.4in_v1.svg

5.

test/visual_test/latest/19991231220000-20000102020000-3.2in_v1.svg

6.

test/visual_test/latest/19991231220000-20000102020000-1.6in_v1.svg

7.

test/visual_test/latest/20000101000000-20000103000000-6.4in_v1.svg

8.

test/visual_test/latest/20000101000000-20000103000000-6.4in_v2.svg

9.

test/visual_test/latest/20000101000000-20000103000000-6.4in_v3.svg

10.

test/visual_test/latest/20000101000000-20000103000000-6.4in_v4.svg

11.

test/visual_test/latest/20001231113000-20010103003000-6.4in_v1.svg

12.

test/visual_test/latest/20001231113000-20010103003000-6.4in_v2.svg

13.

test/visual_test/latest/20001231113000-20010103003000-6.4in_v3.svg

14.

test/visual_test/latest/20010101000000.0-20010101000000.001-6.4in_v1.svg

15. Cross second boundary

test/visual_test/latest/20010101000000.9991-20010101000001.0001-6.4in_v1.svg

16. Cross minute boundary

test/visual_test/latest/20010101000059.9991-20010101000100.0001-6.4in_v1.svg

17. Cross hour boundary

test/visual_test/latest/20010101005959.9991-20010101010000.0001-6.4in_v1.svg

18. Cross day boundary

test/visual_test/latest/20010101235959.9991-20010102000000.0001-6.4in_v1.svg

19. Cross month boundary

test/visual_test/latest/20010131235959.9991-20010201000000.0001-6.4in_v1.svg

20.

test/visual_test/latest/20010101000000.0-20010101000000.00125-6.4in_v1.svg

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datetick-0.9.14.tar.gz (32.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datetick-0.9.14-py3-none-any.whl (34.0 kB view details)

Uploaded Python 3

File details

Details for the file datetick-0.9.14.tar.gz.

File metadata

  • Download URL: datetick-0.9.14.tar.gz
  • Upload date:
  • Size: 32.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for datetick-0.9.14.tar.gz
Algorithm Hash digest
SHA256 75a571d19c7422c18b7b945943913f1c9fc4efea45613c3daba5f099137091ef
MD5 7f06609469e6c42b55fca47678a9dc85
BLAKE2b-256 4022a67bbdbd64908b89aa900edd21cecaa0260512bcdb6997fb068085112320

See more details on using hashes here.

File details

Details for the file datetick-0.9.14-py3-none-any.whl.

File metadata

  • Download URL: datetick-0.9.14-py3-none-any.whl
  • Upload date:
  • Size: 34.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for datetick-0.9.14-py3-none-any.whl
Algorithm Hash digest
SHA256 31c3f0b67dc144a17d959f8846613877f7a2f8c8eb26b0f70c35ef4a0e220893
MD5 51287d8220d29793d2409cffa823133e
BLAKE2b-256 afd8f92e073cf1a0e6a0b67ad3b1ce4f0ba128b4a1f1ebaafe576db8bf5d8d15

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.15

2 files

This release

0.9.14 This release

2 files

0.9.13

2 files

0.9.12

2 files

0.9.11

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.1

2 files

0.9.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page