datactive / bigbang

Scientific analysis of collaborative communities
http://datactive.github.io/bigbang/
MIT License
149 stars 52 forks source link

smoother plotting code with seaborn #561

Open sbenthall opened 2 years ago

sbenthall commented 2 years ago

We have some archaic plotting code in the notebooks. Here's an example:

plt.figure(figsize=(12.5, 7.5))

for i, activity in enumerate(acts):

    colors = 'rgbkm'

    ta = activity.sum(1)
    rmta = ta.rolling(window).mean()
    rmtadna = rmta.dropna()

    rmtadna

    plt.plot_date(np.array(rmtadna.index),
                  np.array(rmtadna.values),
                  colors[i],
                  label=mls[i] + ' activity',
                  xdate=True)

    plt.legend()

This is a much more concise way to do it:

sum_acts = pd.DataFrame.from_records({mls[i] : a.sum(1) for i,a in enumerate(acts)})
sum_acts.index = sum_acts.index.map(date.fromordinal)
window = 100

sns.lineplot(data=sum_acts.rolling(window).mean().dropna(how='all'))

We can make the example code much cleaner and less cumbersome, expecially the earlier notebooks.