Search code examples
pythonplotdataframeaxis-labelscategorical-data

vizualize categorical and numerical values on the same plot


I have the dataframe, the story behind it: score of group student for every month, and the student that contributed the most in the recieving of such a score. What I would like is to plot the score as curve (or bar) and also to visualize the names o(probably as dot for line plot, or just name for every bar)

  Month     Score                _Max
     1         6                 Mike         
     2         5                 Alice
     3         9                 Eleonora
     4         11                 Helen

I'm using this method, which is messy, I know, and gives not readable x-ticks labels but I have no other idea how to deal with both categorical and numerical values in time series on the same plot

    df['index_col'] = df.index
    ax=df.plot(kind='bar', color=['red','blue'], figsize=(30,10))
    ax.set_xlabel("Month", fontsize=21)
    ax.set_ylabel("Score", fontsize=21) 
    labels = df._Max[:]
    ax.set_xticklabels(labels, rotation =90)

Solution

  • Maybe this will work for you:

    plt.bar(df['Month'], df['Score'], align='center')
    
    for i in df.index:
        plt.text(df.loc[i, 'Month'], 12, df.loc[i, '_Max'],
                 horizontalalignment='center')
    
    
    plt.xticks(df['Month'])
    plt.xlabel('Month')
    
    plt.ylim([0, 15])
    plt.ylabel('Score')
    

    enter image description here

    Update

    Since you got several rows, I suggested that you could use color to categorize the names. I only knwo how to do it with Seaborn.

    import seaborn as sns
    
    sns.barplot('Month', 'Score', '_Max', df)
    

    enter image description here