Skip to content

custom style sheet: format dates with new line character #15813

Open

Description

@blaylockbk

Bug report

Bug summary

In my custom matplotlib style sheet, I want to format the dates in such a way that includes a new line character, i.e., %b %d\n%H:%M. But matplotlib will not recognize the new line character and instead prints \n as text on my figure.

More details here: https://stackoverflow.com/questions/59144140/matplotlib-cutsom-style-sheet-format-dates-with-new-line-character?noredirect=1#comment104516325_59144140

Code for reproduction

In my my_style.mplstyle file, I have this formatting instruction for the dates (note the new line character):

    date.autoformatter.hour    : %b %d\n%H:%M

When I load the style sheet and make a plot with dates on the x axis, it displays the \n as text rather than recognizing it as a new line character.

    import matplotlib.pyplot as plt
    plt.style.use('my_style')
    import pandas as pd
    from datetime import datetime
    DATES = pd.date_range(datetime(2019, 1, 1), datetime(2019, 1, 2), 6)
    plt.plot(DATES, range(len(DATES)))

Actual outcome

image

# If applicable, paste the console output here
#
#

Expected outcome

I can get the desired outcome if I use the manual method to change the date formats directly in my code, but I would rather avoid repeating these lines of code in all my plotting scripts. That is why I want to use the style sheet to do this.

    import matplotlib.dates as mdates
    myFmt = mdates.DateFormatter('%b %d\n%H:%M')
    plt.plot(DATES, range(len(DATES)))
    plt.gca().xaxis.set_major_formatter(myFmt)

image

Matplotlib version

  • Operating system: Windows
  • Matplotlib version: 3.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.4
  • Jupyter version (if applicable):
  • Other libraries:

Installed from conda

Activity

  1. timhoffm commented on Dec 2, 2019

    @timhoffm
    Member

    This is currently not possible because the parser interprets values as literal strings and thus the backslash is an actual backslash.

    In [2]: plt.rcParams['date.autoformatter.hour']                                        
    Out[2]: '%b %d\\n%H:%M'
    

    One could do val.decode('unicode_escape') but that would be an API change.

    We could however optionally allow string quotes date.autoformatter.hour : "%b %d\n%H:%M" and only text in quotes will be unescaped. That would keep backward-compatibility. It's also consistent with the way YAML handles strings. 😄

  2. jklymak commented on Dec 3, 2019

    @jklymak
    Member

    That proposal seems reasonable to me

  3. reopened this on Dec 3, 2019
  4. anntzer commented on Jan 25, 2021

    @anntzer
    Contributor

    We could however optionally allow string quotes date.autoformatter.hour : "%b %d\n%H:%M" and only text in quotes will be unescaped. That would keep backward-compatibility.

    I don't think this keeps backcompat? I'd say the breakage is unlikely to matter in practice, but both adding support for quotes and adding support for escapes break backcompat to similar extents...

    Perhaps a better solution would be to just use a different syntax to mark "new" entries, e.g.

    date.formatter.hour = "..."
    

    i.e. the equals sign (rather than colon) says that what comes after is a python literal (which we should just load using ast.literal_eval).

  5. timhoffm commented on Jan 26, 2021

    @timhoffm
    Member

    Theoretically, it's backward compatible, but only if somebody had used string-quoting before and meaning the quotes literally. The likelyhood lf that is small, and I'd be willing to risk that. Since we will only parse escapes in quotes the breakage probability of that is ​ɛ​².

    I'm not convinced of making a semantic difference with = vs. :. This is a good recipe to confuse users.

    I'd also be open towards making the config YAML, which AFAICS the current config is a subset of (PyYAML has become active again for what it's worth).
    I'm alternatively not completely opposed to a python config, but would need a little more convincing that's a benefit worth introducing a large incompatibility (or us maintaining two formats, respectively.

  6. anntzer commented on Jan 26, 2021

    @anntzer
    Contributor

    If we choose to move to yaml (ugh) or python, the easiest is likely to just make the config files matplotlibrc.yml or matplotlibrc.py respectively, which will allow supporting both formats during the transition period without any guessing/incompatibility (and even after we drop (perhaps) support for old-style matplotlibrc, people can just keep both formats around if they want backcompat with old matplotlibs).
    (I agree there's still some maintenance cost, but the python-format loader would be pretty trivial...)

  7. github-actions commented on Jun 30, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. QuLogic commented on Jun 30, 2023

    @QuLogic
    Member

    Quoted strings were added in #22589. @timhoffm intended to add support for backslash escapes, but it doesn't look like that has been implemented yet.

  9. github-actions commented on Oct 5, 2026

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  10. timhoffm commented on Oct 5, 2026

    @timhoffm
    Member

    Nowadays, a format alternative could be TOML. I’d rather adopt a standard file format than bike shedding a proprietary one.

  11. changed the title [-]matplotlib cutsom style sheet: format dates with new line character[/-] [+]custom style sheet: format dates with new line character[/+] on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions