I have this plot: plt.plot(range(1, len(ranking) + 1), np.mean(correlationsall, axis=0))…
- User
I have this plot:
codeplt.plot(range(1, len(ranking) + 1), np.mean(correlations_all, axis=0))I want to draw the std of correlations_all as a shade, can you help?
- Assistant
Yes — you can add a shaded region representing the standard deviation using plt.fill_between. Here’s how you can modify your code: pythonCopy codeimport numpy as np import matplotlib.pyplot as plt
x = range(1, len(ranking) + 1) mean_corr = np.mean(correlations_all, axis=0) std_corr = np.std(correlations_all, axis=0)
plt.plot(x, mean_corr, label='Mean correlation') plt.fill_between( x, mean_corr - std_corr, mean_corr + std_corr, alpha=0.2, label='±1 std' )
plt.xlabel('Rank') plt.ylabel('Correlation') plt.legend() plt.show()
✅ Explanation:
np.mean(correlations_all, axis=0) computes the mean across all samples (assuming each row in correlations_all is one sample).
np.std(correlations_all, axis=0) gives the standard deviation along the same axis.
plt.fill_between() shades the area between (mean - std) and (mean + std), with alpha controlling transparency.
Would you like to make the shaded region smoother or styled (e.g., with dashed edges or different colors)?