matplotlib cutsom style sheet: format dates with new line character · Issue #15813 · matplotlib/matplotlib · GitHub
Skip to content

matplotlib cutsom style sheet: format dates with new line character #15813

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

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