Search code examples
pythonpandasdataframematplotlibplot

Plot each column as a line and group by the first row of each column


So I have a dataframe with 8-year energy values and types of the object on the first row. I need to plot all 3000 sources grouped by their types. 3000 Lines, X-axis is the 8 years, Y-axis is the energy.

It's yearly energy and the type of source

Sorry if anything is missing, it's my first question.

I've tried to just delete the types and plot without grouping by:

blazar_eneryg_with_type.plot(x ='Year', kind = 'line')

Here's the result, that I need but only in 3 groups

Here's the result, that I need but only in 3 groups


Solution

  • Ok having seen your raw data you must have done some processing before hand. Looks like you are calling df.plot(). This is what I would do:

    # read in your csv
    df = pd.read_csv('your_csv')
    
    # drop redundant column
    df.drop(columns=['Unnamed: 0'], inplace=True)
    
    #transpose and drop type column and create second df
    df1 = df.T.drop(['type1'])
    
    # create color map
    colors = {'bcu': 'red', 'bll': 'blue', 'fsrq': 'green'}
    
    # plot colors using mapping from the type column
    df1.plot(color=df['type1'].apply(lambda x: colors[x]))
    plt.show()
    

    The graph is an absolute mess and I am not sure why you would want to plot that much data on a single chart but there you go.

    EDIT : To change the legend

    ax = df1.plot(color=df['type1'].apply(lambda x: colors[x]))
    ax.legend(['bcu', 'bll', 'fsrq'])
    plt.show()