Search code examples
pythonmatplotliblegendmismatch

Legend Color and Plot Line Color Mismatch


Hello Overstack Community,

I am a beginner in Python and I am having trouble getting the color of my plot aligned with those of the plot.

Please find below the code I have written:-

merged_genres_mmd_plot.pivot_table(index='release_year', columns='genres', aggfunc='count').plot(figsize=(16,8))
plt.title('Popularity of Genre over the Years', fontsize=18)
plt.legend(['Action','Comedy','Drama','Horror','Western'])
plt.xlabel('Release Year',labelpad = 25)
plt.show()

This is the plot I am getting.

Can someone help me figure out a way I can align the colors of the legend with that of the plot?

Thanking you in Advance!


Solution

  • There are several answers to your question, the best one I think is:

    with the data you have you can select what is the x and y axis, using pl.plot() you can plot all the of the curves you have and define it's label, as such:

    plt.plot(x, y, label = 'Action')
    plt.plot(x, y, label = 'Comedy')
    .
    .
    .
    plt.legend()
    plt.show()