0

I have a list of prices that I want to plot using matplotlib, and if I just do so, everything is fine. Here is my code:

def plot_prices_and_events(prices, buy=None, sell=None):
    data = []
    max_price = 0
    min_price = sys.maxint
    values = prices.get_values()
    for price in values:
        current_price = price[1]
        data.append(current_price)
        if current_price > max_price:
            max_price = price[1]
        if current_price < min_price:
            min_price = current_price
    plt.ylabel('Bitcoin price in EUR')
    plt.xlabel('Date')
    start_date = datetime.datetime.fromtimestamp(values[0][0])
    end_date = datetime.datetime.fromtimestamp(values[-1][0])
    plt.plot(data)
    #plt.axis([start_date, end_date, min_price, max_price])
    if buy is not None:
        plt.plot(buy)
    if sell is not None:
        plt.plot(sell)
    plt.show()

With the one line commented out, everything is fine: plot with plane axis

Yet, if I try to set the scale for the x-axis, thus uncommenting the line, the axis are displayed correctly, but my plot disappears:

no more plot!

This is reproducible on two machines and by commenting and uncommenting the corresponding line. Am I missing something? I have already played around with the order of the commands, I checked the official tutorials and did some googling. The issue does not seem to be too far spread, so I hope for you people.

Thanks in advance and I hope I'm just stupid. ^^

EDIT: The issue is solved, but the solution is partly to be found in the comments. Bottom Line: The prices were not correctly ordered.

1
  • 1
    Obviously your plt.plot() plots data as a function from [0:number_of_times] to prices. You have to specify the X-range correctly also in your plt.plot() Commented Jun 7, 2017 at 9:38

1 Answer 1

2

As @Dmitri Chubarov said in a comment, by calling plt.plot(data), you are only providing vertical co-ordinates. matplotlib then has to assume that the horizontal co-ordinates are just [0:len(data)].

You later tell it to move the horizontal axis to [start_date, end_date]. However, none of your data points have a horizontal co-ordinate in this range, because the horizontal co-ordinates are [0:len(data)].

You'd have to provide horizontal co-ordinates to your data as well to make this work better. E.g. plt.plot(dates, data), where dates is a list of dates of equal length as data.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you both. This seems obvious once you know about it. I've added a list of dates for every price and added it to the plot()-call. Now the axis is labeled correctly, but my plot goes nuts: i.imgur.com/gusg9yD.png
Are you sure your data are sorted by their date?
I had not doubt they were until now, but I'll have a closer look into that before going any further. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.