In this assignment you must choose one of the options presented below and submit a visual as well as your source code for peer grading. The details of how you solve the assignment are up to you, although your assignment must use matplotlib so that your peers can evaluate your work. The options differ in challenge level, but there are no grades associated with the challenge level you chose. However, your peers will be asked to ensure you at least met a minimum quality for a given technique in order to pass. Implement the technique fully (or exceed it!) and you should be able to earn full grades for the assignment.
Ferreira, N., Fisher, D., & Konig, A. C. (2014, April). Sample-oriented task-driven visualizations: allowing users to make better, more confident decisions. In Proceedings of the SIGCHI Conference on Human Factors in Computing Systems (pp. 571-580). ACM. (video)
In this paper the authors describe the challenges users face when trying to make judgements about probabilistic data generated through samples. As an example, they look at a bar chart of four years of data (replicated below in Figure 1). Each year has a y-axis value, which is derived from a sample of a larger dataset. For instance, the first value might be the number votes in a given district or riding for 1992, with the average being around 33,000. On top of this is plotted the 95% confidence interval for the mean (see the boxplot lectures for more information, and the yerr parameter of barcharts).
A challenge that users face is that, for a given y-axis value (e.g. 42,000), it is difficult to know which x-axis values are most likely to be representative, because the confidence levels overlap and their distributions are different (the lengths of the confidence interval bars are unequal). One of the solutions the authors propose for this problem (Figure 2c) is to allow users to indicate the y-axis value of interest (e.g. 42,000) and then draw a horizontal line and color bars based on this value. So bars might be colored red if they are definitely above this value (given the confidence interval), blue if they are definitely below this value, or white if they contain this value.
Easiest option: Implement the bar coloring as described above - a color scale with only three colors, (e.g. blue, white, and red). Assume the user provides the y axis value of interest as a parameter or variable.
Harder option: Implement the bar coloring as described in the paper, where the color of the bar is actually based on the amount of data covered (e.g. a gradient ranging from dark blue for the distribution being certainly below this y-axis, to white if the value is certainly contained, to dark red if the value is certainly not contained as the distribution is above the axis).
Even Harder option: Add interactivity to the above, which allows the user to click on the y axis to set the value of interest. The bar colors should change with respect to what value the user has selected.
Hardest option: Allow the user to interactively set a range of y values they are interested in, and recolor based on this (e.g. a y-axis band, see the paper for more details).
Note: The data given for this assignment is not the same as the data used in the article and as a result the visualizations may look a little different.
# Use the following data for this assignment: import pandas as pd import numpy as np np.random.seed(12345) df = pd.DataFrame([np.random.normal(32000,200000,3650), np.random.normal(43000,100000,3650), np.random.normal(43500,140000,3650), np.random.normal(48000,70000,3650)], index=[1992,1993,1994,1995])
# %%file week3_solution.py %matplotlib notebook import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl def assignment_data(): """Return distribution dataframe""" np.random.seed(12345) df = pd.DataFrame([np.random.normal(32000, 200000, 3650), np.random.normal(43000, 100000, 3650), np.random.normal(43500, 140000, 3650), np.random.normal(48000, 70000, 3650)], index=[1992, 1993, 1994, 1995]) df = df.transpose() df.columns = [1992, 1993, 1994, 1995] return df def normalize_y(y_value, mean): """ Normalize the chosen y value Notes ------ The last 'if' block makes sure we don't go lower than midpoint in our colorations. Removing it means we could potentially return to 100% dark color within the bar. """ if y_value > mean: return 0 if y_value/mean < 0.5: return 0.5 return y_value/mean greys = ['grey']*4 darks = ['#25383C', '#2B547E', '#3B9C9C', '#800517'] brights = ['#00FFFF', '#FFFF00', '#FFD801', '#F433FF'] violets = ['#A74AC7', '#6C2DC7', '#B93B8F', '#7D1B7E'] color_scheme = [greys, darks, brights, violets] cm_scheme = [plt.cm.bone, plt.cm.cool, plt.cm.Accent, plt.cm.jet, plt.cm.inferno, plt.cm.YlOrRd, plt.cm.BuPu, plt.cm.viridis] # %%file week3_solution.py def interactive_bar(ax, ax2, distribution_data, y_value, color_map): """Change bar colors according to clicked y value Parameters ---------- ax : Axes The axes with which to plot the data ax2 : Axes Axes on which to put colormap distribution_data : DataFrame Dataframe of distribution y_value : int Value of y color_map : Matplotlib colormap Notes ------- 100% dark purple shade indicate that the chosen y value is above the bar. The other shades show how close we are to the mean WITHIN the bar means : Series Mean of each column in the dataframe std_dev : Series Standard deviation of each column in the dataframe min_value : float Minimum value in the population """ means = distribution_data.mean() conf_95 = distribution_data.sem()*1.96 max_value = distribution_data.values.max() min_value = distribution_data.values.min() bins = len(distribution_data.columns) normalize = mpl.colors.Normalize(vmin=0, vmax=1) colors = [color_map(x) for x in np.linspace(0, 1, bins)] for key, col_name in enumerate(distribution_data.columns): bar_mean = means[col_name] bar_err = conf_95[col_name] normed_y = normalize_y(y_value, bar_mean) bar_color = color_map(normed_y) ax.bar(key, bar_mean, align='center', width=1.0, yerr=bar_err, alpha=1, color=bar_color, edgecolor='k', capsize=10, label='%s: p = %2.2f'%(col_name, bar_mean)) ax.set_ylabel('Mean values (95% confidence intervals on top)') ax.legend(loc='best') ax.axhline(y=y_value, color='#3D3C3A', zorder=20) ax.set_xticks((range(len(distribution_data.columns)))) ax.set_xticklabels(distribution_data.columns, position=(-700, 0)) ax.set_title('Bar colors at y = %.0f'%(y_value)) ax2.set_yticks() color_bar = mpl.colorbar.ColorbarBase(ax2, cmap=color_map, norm=normalize, orientation='horizontal') color_bar.set_ticks(np.linspace(0, 1, bins+1)) color_bar.set_ticklabels(['0%', '25%', '50%', '75%', '100%']) plt.show() def onclick(event): """Change bar colors""" ax.cla() ax2.cla() interactive_bar(ax, ax2, distribution_data, event.ydata, cm_scheme) def color_dance(event): """Switch colormaps""" ax.cla() ax2.cla() key = int(event.ydata)%4 print("KK", key) interactive_bar(ax, ax2, distribution_data, event.ydata, cm_scheme[key]) def activate(): """Switch colors with a single colormap""" distribution_data = assignment_data() cm_viridis = plt.cm.viridis fig = plt.figure(figsize=(8, 6), facecolor='lightblue') ax = fig.add_axes([0.15, 0.15, 0.6, 0.80], frame_on=True) ax2 = fig.add_axes([0.4, 0.05, 0.4, 0.025], frame_on=True) interactive_bar(ax, ax2, distribution_data, 40000, cm_scheme) plt.gcf().canvas.mpl_connect('button_press_event', color_dance) if __name__ == "__main__": activate()