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.12.tar.gz (31.9 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.12-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: datetick-0.9.12.tar.gz
  • Upload date:
  • Size: 31.9 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.12.tar.gz
Algorithm Hash digest
SHA256 cc958317778279922c10b73d75d45300556705adffd6126c2929130504caf20d
MD5 4c13a50a3169e9a4ad3d47e8508a85a9
BLAKE2b-256 c1a27fe6b4765dfd21de12ab0a3129b5b89ffad0f985d7f76009aad5823b017a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: datetick-0.9.12-py3-none-any.whl
  • Upload date:
  • Size: 33.7 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 695a229991590f62f2885f36d91a798727f2b3926be807618a7a0b03ab24099d
MD5 e9dc342b2d57798373997c0a00a558a1
BLAKE2b-256 195ffa7382b9b9fbb7c1c2372d8bbe095534732df55f6ffa6353b8b7d5a8294a

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.15

2 files

0.9.14

2 files

0.9.13

2 files

This release

0.9.12 This release

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