Legend only shows one label when plotting with pandas

前端 未结 3 1388
青春惊慌失措
青春惊慌失措 2020-11-30 03:54

I have two Pandas DataFrames that I\'m hoping to plot in single figure. I\'m using IPython notebook.

I would like the legend to show the label for both of the DataFr

3条回答
  •  爱一瞬间的悲伤
    2020-11-30 04:48

    When multiple series are plotted then the legend is not displayed by default.
    The easy way to display custom legends is just to use the axis from the last plotted series / dataframes (my code from IPython Notebook):

    %matplotlib inline  # Embed the plot
    import matplotlib.pyplot as plt
    
    ...
    rates[rates.MovieID <= 25].groupby('MovieID').Rating.count().plot()  # blue
    (rates[rates.MovieID <= 25].groupby('MovieID').Rating.median() * 1000).plot()  # green
    (rates[rates.MovieID <= 25][rates.RateDelta <= 10].groupby('MovieID').Rating.count() * 2000).plot()  # red
    ax = (rates[rates.MovieID <= 25][rates.RateDelta <= 10].groupby('MovieID').Rating.median() * 1000).plot()  # cyan
    
    ax.legend(['Popularity', 'RateMedian', 'FirstPpl', 'FirstRM'])
    

    The plot with custom legends

提交回复
热议问题