Python pandas, Plotting options for multiple lines

后端 未结 3 1209
情深已故
情深已故 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:33

    You're so close!

    You can specify the colors in the styles list:

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    testdataframe = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C'])
    styles = ['bs-','ro-','y^-']
    linewidths = [2, 1, 4]
    fig, ax = plt.subplots()
    for col, style, lw in zip(testdataframe.columns, styles, linewidths):
        testdataframe[col].plot(style=style, lw=lw, ax=ax)
    

    Also note that the plot method can take a matplotlib.axes object, so you can make multiple calls like this (if you want to):

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    testdataframe1 = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C'])
    testdataframe2 = pd.DataFrame(np.random.normal(size=(4,3)), columns=['D', 'E', 'F'])
    styles1 = ['bs-','ro-','y^-']
    styles2 = ['rs-','go-','b^-']
    fig, ax = plt.subplots()
    testdataframe1.plot(style=styles1, ax=ax)
    testdataframe2.plot(style=styles2, ax=ax)
    

    Not really practical in this case, but the concept might come in handy later.

提交回复
热议问题