简体   繁体   中英

How to plot multiple lines within the same graph based on multiple subsets

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

  • 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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM