facebook-prophet

Pyplot limit x axis based on number of elements


I'm plotting a facebook-prophet forecast and I only want to plot the last month data. This 'last month' changes from month to month so I can't use plt.xlim() to limit it by a given range.

Is there any way to limit the x-axis by a given number of elements, like plot last 1000 x-axis values no matter what those values are?


Solution

  • I'd say write a function that represents your understanding of the appropriate x limits. For example:

    def get_x_limits(data):
    
        lower_x = min(data)
        # or
        lower_x = min(data)*0.9 # 10% below lowest value (if values are positive)
        # or
        lower_x = min(data) - 1 # a little below lowest value
    
        # similarly
        upper_x = max(data)
        # or
        upper_x = max(data)*1.1 # 10% above highest value (if values are positive)
        # or
        upper_x = max(data) + 1 # a little above highest value
    
        # maybe smth like this
        if upper_x - lower_x < 1000:
            upper_x = lower_x + 1000
    
        # and finally
        return lower_x, upper_x
    

    You can then use these values to set your limits:

    lower_x, upper_x = get_x_limits(data)
    plt.xlim(lower_x,upper_x)
    

    I have to admit that I ignored the question for the number of elements since I thought its mostly relevant what's in your data, not how much data you have. However, you can still work len(data) into the get_x_limits function the way it fits your need.