[Solved] Python pandas plotting and groupby [closed]


Because you change the question here is the updated answer:

See comments in code

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
%matplotlib inline

# read your dataframe and sort
df = pd.read_clipboard()
df.drop(columns=['length'], inplace=True)
df.rename(columns={'Text.1': 'Text length'}, inplace=True)
df.sort_values(['Text', 'Tag', 'Time'], inplace=True)
x = list(df['Time']) # set x axis by creating a list of time

fig, ax = plt.subplots() # plot mulitple lines
for xlabels, group in df.groupby(['Tag']): # group by Tag
    df['Time'] = df['Time'].astype(str) # change time to a string to create xticks
    xticks = list(df['Time']+'\n'+df['Text']+'\n'+df['Tag']) # create xticks

    group.plot(kind='line',x='Time', y='Relevance', ax=ax)
    ax.legend(['Cat1', 'Cat2'])
    ax.set_xlabel('Time')
    plt.xticks(x, xticks)

    # resize plot
    pos1 = ax.get_position()
    pos2 = [pos1.x0, pos1.y0,  pos1.width + 1, pos1.height + .5]
    ax.set_position(pos2)

output

5

solved Python pandas plotting and groupby [closed]