1

I have a dataset similar to the following:

SNAPSHOT_DATE   DEPLOYMENT_TYPE FORECAST_YEAR   TOTAL_WIDGETS
1/1/20               1             2020             206457
1/1/20               1             2021              70571
1/1/20               1             2022              46918
1/1/20               1             2023              36492
1/1/20               1             2024                  0
1/1/20               1             2025                  0
2/1/20               1             2020             207177
2/1/20               1             2021              71947
2/1/20               1             2022              46918
2/1/20               1             2023              36492
2/1/20               1             2024                  0
2/1/20               1             2025                  0
3/1/20               1             2020             242758
3/1/20               1             2021             102739
3/1/20               1             2022              43174
3/1/20               1             2023              32956
3/1/20               1             2024                  0
3/1/20               1             2025                  0
1/1/20               2             2020             286616
1/1/20               2             2021             134276
1/1/20               2             2022              87674
1/1/20               2             2023                240
1/1/20               2             2024                  0
1/1/20               2             2025                  0
2/1/20               2             2020              308145
2/1/20               2             2021              132996
2/1/20               2             2022               87674
2/1/20               2             2023                 240
2/1/20               2             2024                   0
2/1/20               2             2025                   0
3/1/20               2             2020              218761
3/1/20               2             2021              178594
3/1/20               2             2022               87674
3/1/20               2             2023                 240
3/1/20               2             2024                   0
3/1/20               2             2025                   0

I want to be able to plot for each deployment type, Total Widgets on the y axis and the months (Jan 1 '20 - Dec 1 '20) on the x axis then include a separate line in the plot for each forecasted year 2020-2025. How can I best accomplish this? my first thought was to filter each deployment type based on Date range and forecasted year like this:

forecastchanges_widgets2020 = data.loc[((data['DEPLOYMENT_TYPE'] =='1') & (data['Date'] >= '2020-01-01') & (data['Date'] <= '2020-12-01')) & (data['FORECAST_YEAR'] =='2020')]

and plot each line, but that would mean I would need to repeat that for each year contained within each deployment type. There must be a better way to achieve the desired plot?

This question / answers does not match my requirements, because I need to separate out each deployment type into its own plot and then plot the 'total_widgets' value for each year across the month dates on the x axis

0

1 Answer 1

1
  • For this case, sns.relplot will work
    • seaborn is a high-level API for matplotlib.
  • Given your dataframe data
    • data only contains information where the 'SNAPSHOT' year is 2020, however, for the full dataset, there will be a row of plots for each year in 'Snapshot_Year'.
  • Since the x-axis will be different for each row of plots, facet_kws={'sharex': False}) is used, so xlim can scale based on the date range for the year.
import pandas as pd
import seaborn as sns

# convert SNAPSHOT_DATE to a datetime dtype
data.SNAPSHOT_DATE = pd.to_datetime(data.SNAPSHOT_DATE)

# add the snapshot year as a new column
data.insert(1, 'Snapshot_Year', data.SNAPSHOT_DATE.dt.year)

# plot the data
g = sns.relplot(data=data, col='DEPLOYMENT_TYPE', row='Snapshot_Year', x='SNAPSHOT_DATE', y='TOTAL_WIDGETS',
                hue='FORECAST_YEAR', kind='line', facet_kws={'sharex': False})
g.set_xticklabels(rotation=90)
plt.tight_layout()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.