Better date axes#68
Open
phil-brad wants to merge 5 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves matplotlib axis tick formatting by detecting when axis labels are datetime-like and switching to Matplotlib’s concise date formatting to produce more readable date axes.
Changes:
- Added datetime-axis detection for
x_labels/y_labelsand introduced date-specific tick generation/formatting helpers. - Updated tick rotation/alignment handling for the x-axis.
- Initialized grid line state during widget construction.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| self._setting_axis_limits = False | ||
|
|
||
| self.hasHiddenSeries = False | ||
| self._do_update_grid_lines(self._axes) |
Comment on lines
873
to
+877
| if hasattr(self.data, 'x_labels'): | ||
| step = self.data.x_tick_interval if hasattr(self.data, 'x_tick_interval') else None | ||
| x_ticks, x_labels, x_ticks_rotation = self._get_labels(self.data.x_labels, step, horizontal=True) | ||
| self._axes.set_xticks(x_ticks) | ||
| if all(isinstance(x, (np.datetime64, pd.Timestamp)) for x in self.data.x_labels): | ||
| x_labels = self.data.x_labels | ||
| self._update_date_xticks(x_labels) | ||
| x_ticks_rotation = 0.0 |
| self._axes.xaxis.set_tick_params(labelrotation=rotation) | ||
| matplotlib.artist.setp(self._axes.get_xticklabels(), | ||
| horizontalalignment='right' if rotation else 'center') | ||
| self._adjust_to_xticklabels_height(self._axes.get_xticklabels(), rotation) |
|
|
||
| locator = mdates.AutoDateLocator(maxticks=min(imax - imin, 25)) | ||
| offset_formats = ['', '%Y', '%Y-%b', '%Y-%b', '%Y-%m-%d', '%Y-%m-%d'] | ||
| formatter = mdates.ConciseDateFormatter(axis_obj.get_major_locator(), |
Comment on lines
+951
to
+955
| locator = mdates.AutoDateLocator(maxticks=min(imax - imin, 25)) | ||
| offset_formats = ['', '%Y', '%Y-%b', '%Y-%b', '%Y-%m-%d', '%Y-%m-%d'] | ||
| formatter = mdates.ConciseDateFormatter(axis_obj.get_major_locator(), | ||
| offset_formats=offset_formats) | ||
|
|
Comment on lines
+947
to
+955
| e0, e1 = extent | ||
| imin, imax = max(0, math.floor(e0)), min(math.ceil(e1), len(labels) - 1) | ||
| date_num_min, date_num_max = mdates.date2num(labels[0]), mdates.date2num(labels[-1]) | ||
|
|
||
| locator = mdates.AutoDateLocator(maxticks=min(imax - imin, 25)) | ||
| offset_formats = ['', '%Y', '%Y-%b', '%Y-%b', '%Y-%m-%d', '%Y-%m-%d'] | ||
| formatter = mdates.ConciseDateFormatter(axis_obj.get_major_locator(), | ||
| offset_formats=offset_formats) | ||
|
|
| self._axes.set_yticks(y_ticks) | ||
| self._axes.set_yticklabels(y_labels) | ||
| self._adjust_to_yticklabels_width(y_labels) | ||
| if all(isinstance(y, (np.datetime64, pd.Timestamp)) for y in self.data.y_labels): |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When all values of an axis are datetimes, then use the matplotlib concise date formatter for a nice date axis.