Improving Your Data Visualizations in Python
Nick Strayer
Instructor
# Interval size setup sizes = ['99%', '95%', '90%'] Z_scores = [2.58, 1.96, 1.67] colors = ['#fee0b6','#f1a340', '#b35806']
for size, z, color in zip(sizes, Z_scores, colors): plt.hlines(y = data.y, # Calculate lower and upper boundaries xmin = data['est'] - z*data['std_err'], xmax = data['est'] + z*data['std_err'], # Color by interval size color = color,
# Make line thicker for visibility linewidth = 7,
# Label line so legend text is clear label = size)
plt.plot('est', 'y', 'ko', data = data, label = 'Point Estimate') plt.legend()
widths = [ '99%', '90%'] z_scores = [ 2.58, 1.67] colors = ['#99d8c9', '#41ae76'] for percent, Z, color in zip(widths, z_scores, colors): # Set color to distinquish bands plt.fill_between( x=data.day, y1=data['mean'] - Z*data['std_err'], y2=data['mean'] + Z*data['std_err'] color=color,
# Lower opacity so grid can show through alpha=0.5,
# Give each band id for the legend label=percent)
sizes = ['99% Confidence Interval', '95%', '90%'] # Set up different line widths for intervals widths = [ 5, 10, 15] Z_scores = [2.58, 1.96, 1.67] for size, z, width in zip(sizes, Z_scores, widths): plt.hlines( y = data.y, label = size, xmin = data['est'] - z*data['std_err'], xmax = data['est'] + z*data['std_err'], color = 'grey'
# Adjust line thickness by interval linewidth = width)
plt.plot('est', 'y', 'wo', data = data, label = 'Point Estimate') plt.legend()
Improving Your Data Visualizations in Python