matplotlib legend not showing

前端 未结 1 851
小蘑菇
小蘑菇 2021-01-29 10:49

I have two numpy datasets and trying to make two figures ( each figure including two datasets).

The legend does not show up on both figures. I´d appreciate if someone

相关标签:
1条回答
  • 2021-01-29 11:26

    When you say "I can not manage to make legend work" I am assuming you mean that no legend is showing up.

    Consider the example code which is a very simple example of plotting a histgoram like you are trying to do:

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = np.random.randint(0,10,(1000))
    
    fig,ax = plt.subplots()
    ax.hist(data,bins=10,edgecolor='black')
    
    ax.legend()
    
    plt.show()
    

    This will draw the figure correctly, however there should be a warning in your console saying

    C:\Python27\lib\site-packages\matplotlib\axes_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots. warnings.warn("No labelled objects found. "

    This is telling you to use label= in your ax.hist()

    So if we include that:

    ax.hist(data,bins=10,edgecolor='black',label="Entry 1")
    

    We get the following graph:

    0 讨论(0)
提交回复
热议问题