One legend entry when plotting several curves using one `plot` call

后端 未结 2 758
南方客
南方客 2021-01-21 13:54

I am creating a grid by plotting several curves using one plot call as:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()         


        
2条回答
  •  再見小時候
    2021-01-21 14:24

    Here is one possible solution: You may use the fact that underscores do not produce legend entries. So setting all but the first label to "_" suppresses those to appear in the legend.

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots()
    
    x = np.array([[0,1], [0,1], [0,1]])
    y = np.array([[0,0], [1,1], [2,2]])
    
    ax.plot([0,1],[0,2], label='foo', color='b')
    
    lines = ax.plot(x.T, y.T, label='bar', color='k')
    plt.setp(lines[1:], label="_")
    ax.legend()
    
    plt.show()
    

提交回复
热议问题