简体   繁体   English

将 plotly 图转换为 dataframe 以转换为 plot Z6A8064B5DF4794555500553CufflinksZDZ

[英]Converting plotly graph to dataframe in order to plot dataframe with cufflinks iplot graph

The context of the problem that I am dealing with is trying to convert the results from a time series forecast, plotted with matplotlib.plotly back into a dataframe so that I can use the cufflinks library to be able to get a more interactive chart going so that I can hover over data points to get a more detailed look at the forecast.我正在处理的问题的上下文是尝试将时间序列预测的结果转换为使用 matplotlib.plotly 绘制的时间序列预测结果返回到 dataframe 以便我可以使用更多的交互式图表库我可以通过 hover 对数据点进行更详细的预测。

so after training and creating a simulation the code goes:所以在训练和创建模拟之后,代码如下:

    date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()
for i in range(test_size):
    date_ori.append(date_ori[-1] + timedelta(days = 1))
date_ori = pd.Series(date_ori).dt.strftime(date_format = '%Y-%m-%d').tolist()
date_ori[-5:]


accepted_results = []
for r in results:
    if (np.array(r[-test_size:]) < np.min(df['Close'])).sum() == 0 and \
    (np.array(r[-test_size:]) > np.max(df['Close']) * 2).sum() == 0:
        accepted_results.append(r)
len(accepted_results)


accuracies = [calculate_accuracy(df['Close'].values, r[:-test_size]) for r in accepted_results]

plt.figure(figsize = (15, 5))
for no, r in enumerate(accepted_results):
    plt.plot(r, label = 'forecast %d'%(no + 1))
plt.plot(df['Close'], label = 'true trend', c = 'black')

plt.legend()
plt.title('average accuracy: %.4f'%(np.mean(accuracies)))

x_range_future = np.arange(len(results[0]))
plt.xticks(x_range_future[::30], date_ori[::30])

plt.show()

I have started to dissect the last plotting section to attempt to convert the data into a dataframe in order to plot with cufflinks as the format for cufflinks is like:我已经开始剖析最后一个绘图部分以尝试将数据转换为 dataframe 以便 plot 与袖扣作为袖扣的格式如下:

import cufflinks as cf
# data from FXCM Forex Capital Markets Ltd.
raw = pd.read_csv('http://hilpisch.com/fxcm_eur_usd_eod_data.csv',
                 index_col=0, parse_dates=True)
quotes = raw[['AskOpen', 'AskHigh', 'AskLow', 'AskClose']]
quotes = quotes.iloc[-60:]
quotes.tail()

    AskOpen AskHigh AskLow  AskClose
2017-12-25 22:00:00 1.18667 1.18791 1.18467 1.18587
2017-12-26 22:00:00 1.18587 1.19104 1.18552 1.18885
2017-12-27 22:00:00 1.18885 1.19592 1.18885 1.19426
2017-12-28 22:00:00 1.19426 1.20256 1.19369 1.20092
2017-12-31 22:00:00 1.20092 1.20144 1.19994 1.20147

qf = cf.QuantFig(
         quotes,
         title='EUR/USD Exchange Rate',
         legend='top',
         name='EUR/USD'
)

qf.iplot()

Where I have gotten so far is trying to dissect the plotly graph into a dataframe as so, these are the forecasted results:到目前为止,我试图将 plotly 图表分解为 dataframe ,这些是预测结果:

df = accepted_results
rd = pd.DataFrame(df)
rd.T

    0   1   2   3   4   5   6   7
0   768.699985  768.699985  768.699985  768.699985  768.699985  768.699985  768.699985  768.699985
1   775.319656  775.891012  772.283885  737.763376  773.811344  785.021571  770.438252  770.464180
2   772.387081  787.562968  764.858772  737.837558  775.712162  770.660990  768.103724  770.786379
3   786.316425  779.248516  765.839603  760.195678  783.410054  789.610540  765.924561  773.466415
4   796.039144  803.113903  790.219174  770.508252  795.110376  793.371152  774.331197  786.772606
... ... ... ... ... ... ... ... ...
277 1042.788063 977.462670  1057.189696 1262.203613 1057.900621 1042.329811 1053.378352 1171.416597
278 1026.857102 975.473725  1061.585063 1307.540754 1061.490772 1049.696547 1054.122795 1117.779434
279 1029.388746 977.097765  1069.265953 1192.250498 1064.540056 1049.169295 1045.126807 1242.474584
280 1030.373147 983.650686  1070.628785 1103.139889 1053.571269 1030.669091 1047.641127 1168.965372
281 1023.118504 984.660763  1071.661590 1068.445156 1080.461617 1035.736879 1035.599867 1231.714340

then converting the x axis from然后将x轴从

plt.xticks(x_range_future[::30], date_ori[::30])

to

df1 = pd.DataFrame((x_range_future[::30], date_ori[::30]))
df1.T
    0   1
0   0   2016-11-02
1   30  2016-12-15
2   60  2017-01-31
3   90  2017-03-15
4   120 2017-04-27
5   150 2017-06-09
6   180 2017-07-24
7   210 2017-09-05
8   240 2017-10-17
9   270 2017-11-20

lastly I have the close column and this is what I've been able to come up with for it so far最后我有关闭的专栏,这是我到目前为止所能想到的

len(df['Close'].values)
252

when i use当我使用

df['Close'].values 

I get an array, I'm having problems getting this all together, the cufflinks iplot graphs are just way better, and it would be amazing if I could somehow gain the intuition to do this, I apologize in advance if I didn't try hard enough, but I'm doing my best I can't seem to find the answer no matter how many times I've searched google so I thought I would ask here.我得到了一个数组,我在把这一切放在一起时遇到了问题,袖扣 iplot 图表要好得多,如果我能以某种方式获得这样做的直觉,那就太棒了,如果我没有尝试,我提前道歉已经够难了,但我正在尽力而为,无论我搜索谷歌多少次,我似乎都找不到答案,所以我想我会在这里问。

This is what I did, I went through and printed indipendent strings like print(date_ori) as well as simplified it with print(len(date_ori) which in turn had all of the dates for the forecast, then i made it into a dataframe with df['date'] = pd.DataFrame(date_ori), where as with the results, I had to transpose them with df.T so they would be in a long column format rather than in a long row, so first这就是我所做的,我浏览并打印了像 print(date_ori) 这样的独立字符串,并用 print(len(date_ori) 简化了它,它又包含了预测的所有日期,然后我将它变成了 dataframe df['date'] = pd.DataFrame(date_ori),与结果一样,我必须用 df.T 转置它们,以便它们采用长列格式而不是长行格式,所以首先

df = pd.DataFrame(results)
df = df.T

then然后

df['date'] = pd.DataFrame(date_ori)


I had trouble naming the column 0 which contained all of the predicted results so i just saved the file with我在命名包含所有预测结果的第 0 列时遇到了麻烦,所以我只是保存了文件

df.to_csv('yo')

then i edited the column named 0 to results and added.csv to the end, then pulled the data back into memory然后我将名为 0 的列编辑为结果并将.csv 添加到末尾,然后将数据拉回 memory

then i formatted the date然后我格式化了日期

format = '%Y-%m-%d'
df['Datetime'] = pd.to_datetime(df['date'], format=format)
df = df.set_index(pd.DatetimeIndex(df['Datetime']))

and dropped the un needed columns, and i guess i could add the close data that i started with to plot together now, but i got the results into the dataframe so now i can use these awesome charts.并删除了不需要的列,我想我现在可以将开始时的关闭数据一起添加到 plot 中,但是我将结果放入 Z6A8064B5DF4794555500553C47C55057DZ 中,所以现在我可以使用这些很棒的图表。 Can't believe i figured it out within 18 hours I was so lost lol.不敢相信我在 18 小时内就弄明白了,我迷路了,哈哈。

also i dropped the experiment to just one simulation so there was only 1 row of results to deal with so i could figure it out.我也把实验放到了一个模拟中,所以只有 1 行结果要处理,所以我可以弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM