简体   繁体   中英

Pandas, matplotlib and plotly - how to fix series legend?

I'm trying to create an interactive plotly graph from pandas dataframes.

However, I can't get the legends displayed correctly.

Here is a working example:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py

# sign into the plotly api
py.sign_in("***********", "***********")

# create some random dataframes
dates = pd.date_range('1/1/2000', periods=8)
df1 = pd.DataFrame(np.random.randn(8, 1), index=dates, columns=['A'])
df2 = pd.DataFrame(np.random.randn(8, 1), index=dates, columns=['B'])
df1.index.name = 'date'
df2.index.name = 'date'

Now I attempt to plot the dataframes using plotly.

fig, ax = plt.subplots(1,1)

df1.plot(y='A', ax=ax)
df2.plot(y='B', ax=ax)

py.iplot_mpl(fig, filename='random')

Notice there is no legend

密谋-没有传说

Edit:

Based on suggestions below I have added an update dict. Although this does display the legend, it messes up the plot itself:

fig, ax = plt.subplots(1,1)

df1.plot(y='A', ax=ax)
df2.plot(y='B', ax=ax)

update = dict(
    layout=dict(
        annotations=[dict(text=' ')],  # rm erroneous 'A', 'B', ... annotations
        showlegend=True                # show legend 
    )
)
py.iplot_mpl(fig, update=update, filename='random')

在此处输入图片说明

Edit 2:

Removing the annotations entry from the layout dict results in the plot being displayed correctly, but the legend is not the y column name, but rather the x column name, the index name of the dataframe

fig, ax = plt.subplots(1,1)

df1.plot(y='A', ax=ax)
df2.plot(y='B', ax=ax)

update = dict(
    layout=dict(
        showlegend=True                # show legend 
    )
)
py.iplot_mpl(fig, update=update, filename='random')

This results in the following plot:

在此处输入图片说明

Edit 3:

I have found a way to override the legend text but it seems a bit klunky. Given that I've specified the dataframe column I want to plot:

df1.plot(y='A', ax=ax)

I would have expected that y='A' would result in 'A' being used as the legend label.

It seems this is not the case, and while it is possible to override using the index label, as seen below, it just feels wrong.

Is there a better way to achieve this result?

update = dict(
    layout=dict(
        showlegend=True,
    ),
    data=[
        dict(name='A'),
        dict(name='B'),
    ]
)
py.iplot_mpl(fig, update=update, filename='random')

在此处输入图片说明

Legends don't convert well from matplotlib to plotly.

Fortunately, adding a plotly legend to a matplotlib plot is straight forward:

update = dict(
    layout=dict(
        showlegend=True  # show legend 
    )
)
py.iplot_mpl(fig, update=update)

See the full working ipython notebook here .

For more information, refer to the plotly user guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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