Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale_x_datetime **kwargs for breaks and labels #25

Closed
flyingvince opened this issue Jun 27, 2017 · 6 comments
Closed

scale_x_datetime **kwargs for breaks and labels #25

flyingvince opened this issue Jun 27, 2017 · 6 comments
Labels

Comments

@flyingvince
Copy link

Hi,

Is it possible to define the **kwargs for scale_x_datetime in the same way as the Python ggplot library, for example using breaks='1 week' and labels='%W' ? The ggplot library is using date_breaks and date_format helpers to achieve this goal, is there an equivalent in plotnine?

The code below throws an error: PlotnineError: 'Breaks and labels have unequal lengths'

Using plotnine.__version__ = '0.2.1'

import random
import pandas as pd
import plotnine

n = 100
df = pd.DataFrame({'date': pd.date_range(start='2017-01-01', periods=n), 
                   'value': [random.randrange(0, 100) for x in range(n)]})

ggplot(df, aes('date', 'value')) + \
    geom_line() + \
    scale_x_date(breaks='1 week', labels='%W') + \
    scale_y_continuous()

This is what I am referring to, from ggplot ggplot scales docs

ggplot(meat, aes('date','beef')) + \
    geom_line() + \
    scale_x_date(breaks=date_breaks('10 years'),
                 labels=date_format('%B %-d, %Y'))

Thanks for your work, great library coverage compared to the original ggplot2 in R.

@has2k1
Copy link
Owner

has2k1 commented Jun 27, 2017

Checkout the documentation of the scaling library mizani, the functions are defined in there.

@flyingvince
Copy link
Author

Thank you for your help. In the example below I would like to extend the x labels up to 'Aug-2017' and have minor breaks shown up to that point as well.

How to achieve this? Do I need to use xlim() ? Is there a way to force mizani.breaks.date_breaks to extend beyond the min & max datetime objects available from limits object?

import random
import pandas as pd
from plotnine import ggplot, aes, geom_line
from plotnine.scales import scale_x_date, scale_y_continuous
from mizani.breaks import date_breaks, minor_breaks
from mizani.formatters import date_format


n = 30
df_test = pd.DataFrame({'date': pd.date_range(start='2017-01-01', periods=n, freq='1W', tz='UTC'), 
                        'value': [random.randrange(0, 100) for x in range(n)]})

limits = df_test['date'].min(), df_test['date'].max()
breaks = date_breaks('1 month')
breaks_date = [x for x in breaks(limits)]
labels_date = date_format('%b-%Y')([x for x in breaks_date])

minor_breaks_x = minor_breaks(n=5)(breaks_date, limits)

ggplot(df_test, aes('date', 'value')) + \
    geom_line() + \
    scale_x_date(breaks=breaks_date, labels=labels_date, minor_breaks=minor_breaks_x) + \
    scale_y_continuous()

image

@has2k1
Copy link
Owner

has2k1 commented Jun 27, 2017

You do not need to create the explicit breaks and labels.

This should work

limits = df_test['date'].min(), df_test['date'].max() # change as you need

ggplot(df_test, aes('date', 'value')) + \
    geom_line() + \
    scale_x_date(breaks=date_breaks('1 month'),
                 labels=date_format('%b-%Y'),
                 minor_breaks=minor_breaks(n=5),
                 limits=limits) +\
    scale_y_continuous()

You cannot use xlim() or lims().

@has2k1
Copy link
Owner

has2k1 commented Jun 27, 2017

Also, if all the intervals between the breaks are not equal, the minor breaks will not be able to extend beyond the limits. months have varying number of days, so that creates unequal intervals.

@flyingvince
Copy link
Author

Noted, thank you for the quick feedback.

@hmanuel1
Copy link

hmanuel1 commented Jun 4, 2019

Hi,
Very nice work with this package! I have been using extensively with great results.

Minor comment: it will be nice to add second resolution to the date_breaks() function in the mizani.breaks module.

I was able to add this line:
from matplotlib.dates import SecondLocator
in mizani.breaks breaks.py

and add the "SecondLocator" to same module:

LOCATORS = {
'second': SecondLocator,
'minute': MinuteLocator,
'hour': HourLocator,
'day': DayLocator,
'week': WeekdayLocator,
'month': MonthLocator,
'year': lambda interval: YearLocator(base=interval)
}

Code is working find, but don't know if there is any other dependencies!

Thanks you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants