-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better time axis formatting for plots
- Loading branch information
1 parent
1e500ff
commit cfe9888
Showing
3 changed files
with
49 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf8 -*- | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
""" | ||
Plot utils. | ||
:copyright: | ||
2021-2024 Claudio Satriano <[email protected]> | ||
:license: | ||
GNU General Public License v3.0 or later | ||
(https://www.gnu.org/licenses/gpl-3.0-standalone.html) | ||
""" | ||
import matplotlib.dates as mdates | ||
|
||
|
||
def format_time_axis(ax, which='both'): | ||
""" | ||
Format the time axis of a Matplotlib plot. | ||
""" | ||
if which == 'both': | ||
axes = [ax.xaxis, ax.yaxis] | ||
elif which == 'xaxis': | ||
axes = [ax.xaxis] | ||
elif which == 'yaxis': | ||
axes = [ax.yaxis] | ||
else: | ||
raise ValueError(f'Invalid value for "which": {which}') | ||
for axis in axes: | ||
dmin, dmax = axis.get_data_interval() | ||
timespan = dmax-dmin | ||
if timespan > 365: | ||
_major_locator = mdates.YearLocator() # every year | ||
_major_fmt = mdates.DateFormatter('%Y') | ||
_minor_locator = mdates.MonthLocator() # every month | ||
else: | ||
_major_locator = mdates.AutoDateLocator(minticks=3, maxticks=7) | ||
_major_fmt = mdates.ConciseDateFormatter(_major_locator) | ||
_minor_locator = mdates.DayLocator() # every day | ||
axis.set_major_locator(_major_locator) | ||
axis.set_major_formatter(_major_fmt) | ||
axis.set_minor_locator(_minor_locator) | ||
axis.grid(True, which='major', linestyle='--', color='0.5') | ||
axis.grid(True, which='minor', linestyle=':', color='0.8') |