0
votes

I'm trying to plot data from 2 seperate MultiIndex, with the same data as levels in each.

Currently, this is generating two seperate plots and I'm unable to customise the legend by appending some string to individualise each line on the graph. Any help would be appreciated!

Here is the method so far:

def plot_lead_trail_res(df_ante, df_post, symbols=[]):

if len(symbols) < 1:
    print "Try again with a symbol list. (Time constraints)"
else:
    df_ante = df_ante.loc[symbols]
    df_post = df_post.loc[symbols]
    ante_leg = [str(x)+'_ex-ante' for x in df_ante.index.levels[0]]
    post_leg = [str(x)+'_ex-post' for x in df_post.index.levels[0]]
    print "ante_leg", ante_leg
    ax = df_ante.unstack(0).plot(x='SHIFT', y='MUTUAL_INFORMATION', legend=ante_leg)
    ax = df_post.unstack(0).plot(x='SHIFT', y='MUTUAL_INFORMATION', legend=post_leg)
    ax.set_xlabel('Time-shift of sentiment data (days) with financial data')
    ax.set_ylabel('Mutual Information')

Using this function call:

sentisignal.plot_lead_trail_res(data_nasdaq_top_100_preprocessed_mi_res, data_nasdaq_top_100_preprocessed_mi_res_validate, ['AAL', 'AAPL'])

I obtain the following figure:

Current plots

Ideally, both sets of lines would be on the same graph with the same axes!

Update 2 [Concatenation Solution]

I've solved the issues of plotting from multiple frames using concatenation, however the legend does not match the line colors on the graph.

There are not specific calls to legend and the label parameter in plot() has not been used.

Code:

df_ante = data_nasdaq_top_100_preprocessed_mi_res
df_post = data_nasdaq_top_100_preprocessed_mi_res_validate
symbols = ['AAL', 'AAPL']
df_ante = df_ante.loc[symbols]
df_post = df_post.loc[symbols]
df_ante.index.set_levels([[str(x)+'_ex-ante' for x in df_ante.index.levels[0]],df_ante.index.levels[1]], inplace=True)
df_post.index.set_levels([[str(x)+'_ex-post' for x in df_post.index.levels[0]],df_post.index.levels[1]], inplace=True)

df_merge = pd.concat([df_ante, df_post])
df_merge['SHIFT'] = abs(df_merge['SHIFT'])

df_merge.unstack(0).plot(x='SHIFT', y='MUTUAL_INFORMATION')

Image:

MultiIndex Plot Image

1

1 Answers

0
votes

I think, with

ax = df_ante.unstack(0).plot(x='SHIFT', y='MUTUAL_INFORMATION', legend=ante_leg)

you put the output of the plot() in ax, including the lines, which then get overwritten by the second function call. Am I right, that the lines which were plotted first are missing?

The official procedure would be rather something like

fig = plt.figure(figsize=(5, 5)) # size in inch
ax = fig.add_subplot(111) # if you want only one axes

now you have an axes object in ax, and can take this as input for the next plots.