Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

python - Plot line graph based on selected/filtered rows in dataframe

I have a dataframe of footballers and their premier league fantasy football stats, I would like to create line plots for each player (x-axis: gameDate, y-axis: points)

Below is a def I created that filters the dataframe based on the player name and attempts the lineplot.

def PlotPlayerPoints(df, player):
    df = df.copy()
    df = df.filter(like=player, axis=0)
    
    plt.figure(figsize=(20,6))
    plt.title(player)
    sns.lineplot(data=ffmlDf, x="gameDate", y="points", estimator=None)
        
for player in ffmlDf['playerName'].unique():
    PlotPlayerPoints(ffmlDf, player)
    

The lineplot comes through as the exact same for each player, so my attempt at filtering clearly did not work.

question from:https://stackoverflow.com/questions/65599767/plot-line-graph-based-on-selected-filtered-rows-in-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are looking for a hue parameter. Showing your df would help.

import pandas as pd
import seaborn as sns

df = sns.load_dataset('tips')
sns.lineplot(x='total_bill', y='size', hue='sex', data=df)

hue acts like a filter and draws the selected plot for each instance found in the hue parameter, so in your case hue would need to be the player.

enter image description here

Unless you want n lineplots for each player then, of course, you can use for loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...