3

I've created a script to create multiple plots in one object. The results I am looking for are two plots one over the other such that each plot has different y axis scale but x axis is fixed - dates. However, only one of the plots (the top) is properly created, the bottom plot is visible but empty i.e the geom_line is not visible. Furthermore, the y-axis of the second plot does not match the range of values - min to max. I also tried using facet_grid (scales="free") but no change in the y-axis. The y-axis for the second graph has a range of 0 to 0.05.

I've limited the date range to the past few weeks. This is the code I am using:

    df = df.set_index('date')
    weekly = df.resample('w-mon',label='left',closed='left').sum()
    data = weekly[-4:].reset_index()
    data= pd.melt(data, id_vars=['date'])
    pplot = ggplot(aes(x="date", y="value", color="variable", group="variable"), data)
    #geom_line()
    scale_x_date(labels = date_format('%d.%m'), 
                         limits=(data.date.min() - dt.timedelta(2),
                                 data.date.max() + dt.timedelta(2)))
    #facet_grid("variable", scales="free_y")
    theme_bw()

The dataframe sample (df), its a daily dataset containing values for each variable x and a, in this case 'date' is the index:

date            x      a
2016-08-01      100    20
2016-08-02       50    0
2016-08-03       24    18
2016-08-04       0     10

The dataframe sample (to_plot) - weekly overview:

         date       variable  value
0  2016-08-01       x        200
1  2016-08-08       x        211
2  2016-08-15       x        104
3  2016-08-22       x        332
4  2016-08-01       a         8
5  2016-08-08       a         15
6  2016-08-15        a        22
7  2016-08-22        a        6

Sorry for not adding the df dataframe before.

OAK
  • 2,994
  • 9
  • 36
  • 49
  • Could someone please specify what more I can share with you to have some assistance? I can't quite figure out still why the second graph doesn't have the correct y-axis range and is not being plotted. It works on my local/home environment just fine though. – OAK Sep 22 '16 at 18:48
  • You could post all your (relevant...) code for us to execute it at first :) For instance here, I have no idea what `df` really is, in your code. – Daneel Sep 26 '16 at 10:06
  • @Daneel update the post. thanks. – OAK Sep 26 '16 at 11:51

1 Answers1

2

Your calls to the plot directives geom_line(), scale_x_date(), etc. are standing on their own in your script; you do not connect them to your plot object. Thus, they do not have any effect on your plot.

In order to apply a plot directive to an existing plot object, use the graphics language and "add" them to your plot object by connecting them with a + operator.

The result (as intended):

ggplot in python with facet_grid

The full script:

from __future__ import print_function

import sys
import pandas as pd
import datetime as dt
from ggplot import *

if __name__ == '__main__':
    df = pd.DataFrame({
        'date': ['2016-08-01', '2016-08-08', '2016-08-15', '2016-08-22'],
        'x': [100, 50, 24, 0],
        'a': [20, 0, 18, 10]
    })

    df['date'] = pd.to_datetime(df['date'])

    data = pd.melt(df, id_vars=['date'])
    plt = ggplot(data, aes(x='date', y='value', color='variable', group='variable')) +\
        scale_x_date(
            labels=date_format('%y-%m-%d'), 
            limits=(data.date.min() - dt.timedelta(2), data.date.max() + dt.timedelta(2))
        ) +\
        geom_line() +\
        facet_grid('variable', scales='free_y')

    plt.show()
jbndlr
  • 4,965
  • 2
  • 21
  • 31