Tamis
Tamis

Reputation: 35

Plotly chart not corresponding to time (x) data

I'm a novice so this code is probably ugly ! I'm trying to chart the content of a simple dataframe (x=time, y=value) with the following code :

import json
import requests
import pandas as pd
import datetime

import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
py.init_notebook_mode(connected=True)

starttime = time.time()
now = datetime.datetime.now()
json_url = 'https://poloniex.com/public?command=returnLoanOrders&currency=MAID'

json_backup = 'temp.json'
data_store_filename = 'data_store.dat'
df_store = pd.DataFrame(columns=["Time", "Average_Rate"])

df_store = pd.read_json(json_backup)

while True:
    #download the data we want
    j = requests.get(url=json_url)
    content = json.loads(j.content)
    #create a dataframe with that data
    df = pd.DataFrame.from_dict(content['offers'])
    df = df[['rate', 'amount', 'rangeMin', 'rangeMax']]
    #Create a dateframe with the data we intend to store
    df_rate = pd.DataFrame(columns=["Time", "Average_Rate"])
    #converts data type to float so that we can do some math on it
    df[['rate']] = df[['rate']].astype(float)
    df[['amount']] = df[['amount']].astype(float)
    #multiply the rate column by 100 to get the correct rate"
    df['rate'] *= 100
    length = len(df.index)

    average_rate = df['rate'].sum() / length

    df_store = df_store.append({
         "Time": datetime.datetime.now(),
         "Average_Rate": average_rate
          }, ignore_index=True)

    df_store.to_json(json_backup)

    backup = pd.read_json(json_backup)

    trace1 = go.Scatter(
    x = backup.Time,
    y = backup.Average_Rate,
    mode = 'lines',
    name = 'lines'
    )
    py.iplot([trace1])

    time.sleep(30.0 - ((time.time() - starttime) % 30.0))

I'm getting this weird chart that seem to go back in time. I did check my data and time is correct, the chart isn't. Can anyone explain me what is happening and how to avoid it ?

Chart with error : Link

Upvotes: 0

Views: 52

Answers (1)

fuglede
fuglede

Reputation: 18201

This happens because pd.read_json(json_backup) ends up sorting the index lexicographically, so that 10 comes before 2. Changing that line to pd.read_json('temp.json').sort_index() takes care of the issue.

Upvotes: 1

Related Questions