Python pandas, Plotting options for multiple lines

后端 未结 3 1193
情深已故
情深已故 2020-12-07 17:50

I want to plot multiple lines from a pandas dataframe and setting different options for each line. I would like to do something like

testdataframe=pd.DataFra         


        
3条回答
  •  既然无缘
    2020-12-07 18:11

    Considering the dataframe testdataframe

    testdataframe = pd.DataFrame(np.arange(12).reshape(4,3))
    
    print(testdataframe)
    
       0   1   2
    0  0   1   2
    1  3   4   5
    2  6   7   8
    3  9  10  11
    

    You can combine styles into a single list of strings as in styles defined below. I'll also define the linewidths in lws

    styles=['bs-', 'ro-', 'y^-']
    lws = [2, 1, 1]
    

    We can use the plot method on the testdataframe passing the list styles to the style parameter. Note that we could have also passed a dictionary (and probably other things as well).

    However, line widths are not as easily handled. I first capture the AxesSubplot object and iterate over the lines attribute setting the line width.

    ax = testdataframe.plot(style=styles)
    for i, l in enumerate(ax.lines):
        plt.setp(l, linewidth=lws[i])
    

提交回复
热议问题