Search code examples
pythondataframematplotlibplotscatter

Plotting from data but only when specific critera or values are met in python (matplotlib)


so I don't have much of an idea on how to do this at all, I know how to plot for example a scatter plot like below from a dataframe, but is there a way that I can plot for when a column meets a certain parameter, for example only on specific dates, or would I have to create new dataframes for each selection of data I want to plot and plot from there?

df_plot.plot(x = 'Date', y = 'Good (1) : Bad (0)', kind = 'scatter', title = ('Good Vs. Bad Data')).set_xlabel("{}{}{}{}".format(' Date ',df_plot['Date'][0],' untill ',df_plot['Date'][len(df_plot)-1]))
plt.xticks([])

TIA!


Solution

  • Yes, AFAIK the easiest way is to pass DataFrame.plot a subset of your dataframe. If your dataframe is using the date column as the index and it is a datetime column, you can select a subset like this:

    start_date = '1980-12-30'
    end_date = '2020-02-07'
    df.loc[start_date:end_date]
    

    or if it is not your index:

    start_date = '1980-12-30'
    end_date = '2020-02-07'
    df['Date_column'][start_date:end_date]
    

    If your column contains strings of dates, you can convert the column like this:

    df['Date_column'] = pd.to_datetime(df['Date_column'])