Search code examples
pythonmatplotlibstandard-deviationerrorbar

Add standard deviation bars from existing std column in data?


I'm plotting a chart based on the following data (head only):

Date    noctiluca_ave   density_ave density_sd
0   2018-03-07  2.0 1027.514332 0.091766
1   2018-03-14  4.0 1027.339988 0.285309
2   2018-03-21  1.0 1027.346413 0.183336
3   2018-03-31  1.0 1027.372996 0.170423
4   2018-04-07  0.0 1027.292119 0.187385

How do I add standard deviation ('density_sd') bars to the density_ave line?

fig, ax = plt.subplots(figsize=(10, 10))
ax.plot(hydro_weekly2 ['Date'], hydro_weekly2 ['density_ave'], label='density weekly ave', color='purple')
ax2=ax.twinx()
ax2.plot(hydro_weekly2['Date'], hydro_weekly2['noctiluca_ave'], label='noctiluca abundance' , color='r')
ax.set_ylabel('Density')
ax.set_xlabel('Date')
ax2.set_ylabel('Noctiluca abundance/cells per m3')
ax.set(title="Noctiluca Abundance and Density 2018")


lines, labels = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc="upper left")

enter image description here


Solution

  • You could either replace ax.plot with ax.errorbar() or use ax.fill_between() to show a colored band.

    Here is an example with toy data and both approaches combined:

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    N = 20
    dates = pd.date_range('2018-03-07', periods=N, freq='W-WED')
    hydro_weekly2 = pd.DataFrame({'Date': dates,
                                  'noctiluca_ave': np.random.randint(0, 14000, N),
                                  'density_ave': 1027 + np.random.randn(N).cumsum() / 5,
                                  'density_sd': 0.1 + np.abs(np.random.randn(N) / 5)})
    fig, ax = plt.subplots(figsize=(10, 10))
    ax.errorbar(hydro_weekly2['Date'], hydro_weekly2['density_ave'], yerr=hydro_weekly2['density_sd'],
                label='density weekly ave', color='purple')
    ax.fill_between(hydro_weekly2['Date'], hydro_weekly2['density_ave'] - hydro_weekly2['density_sd'],
                    hydro_weekly2['density_ave'] + hydro_weekly2['density_sd'], color='purple', alpha=0.3)
    ax2 = ax.twinx()
    ax2.plot(hydro_weekly2['Date'], hydro_weekly2['noctiluca_ave'], label='noctiluca abundance', color='r')
    ax.set_ylabel('Density')
    ax.set_xlabel('Date')
    ax2.set_ylabel('Noctiluca abundance/cells per m3')
    ax.set(title="Noctiluca Abundance and Density 2018")
    plt.show()
    

    example plot