Python 与时间(x)数据不对应的绘图图表

Python 与时间(x)数据不对应的绘图图表,python,pandas,charts,plotly,Python,Pandas,Charts,Plotly,我是新手,所以这段代码可能很难看! 我试图用以下代码绘制一个简单数据帧(x=时间,y=值)的内容: 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) st

我是新手,所以这段代码可能很难看! 我试图用以下代码绘制一个简单数据帧(x=时间,y=值)的内容:

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))
我得到了一张奇怪的图表,似乎回到了过去。我确实检查了我的数据,时间是正确的,图表不是。有人能告诉我发生了什么事以及如何避免吗


出现错误的图表:

发生这种情况的原因是
pd.read\u json(json\u backup)
最终按照字典顺序对索引进行排序,因此10排在2之前。将该行更改为
pd.read_json('temp.json')。sort_index()
解决了这个问题。

您能提供一个解决方案吗?目前,提供的代码中有很多未定义的变量,这使得无法重现问题。感谢您抽出时间阅读我的问题!那很有魅力!当我在阅读我的结果(而不是图表)时,我应该想到这一点,并且看到索引不是按时间顺序排列的。非常感谢你的帮助!