Bruno Gonçalves www.bgoncalves.com Data Visualization using matplotlib 
 www.bgoncalves.com https://bmtgoncalves.github.io/DataVisualization/
Bruno Gonçalves www.bgoncalves.com Data Visualization using matplotlib 
 www.bgoncalves.com https://bmtgoncalves.github.io/DataVisualization/
Bruno Gonçalves www.bgoncalves.com Data Visualization using matplotlib 
 www.bgoncalves.com https://bmtgoncalves.github.io/DataVisualization/
www.bgoncalves.com@bgoncalves The views and opinions expressed in this article are those of the authors and do not necessarily reflect the official policy or position of my employer. The examples provided with this tutorial were chosen for their didactic value and are not mean to be representative of my day to day work. Disclaimer
Human Perception
www.bgoncalves.com@bgoncalves Human Perception http://abstrusegoose.com/421
www.bgoncalves.com@bgoncalves Human Senses
www.bgoncalves.com@bgoncalves • Some cognitive tasks are significantly easier than others. In order, we are good a distinguishing: • Position, length • Direction, Angle, Area • Volume, Curvature, Shade Perception
www.bgoncalves.com@bgoncalves • Some cognitive tasks are significantly easier than others. In order, we are good a distinguishing: • Position, length • Direction, Angle, Area • Volume, Curvature, Shade • Color Saturation. Perception
www.bgoncalves.com@bgoncalves Perception • Context also matters! • An object seen in the context of larger objects will appear smaller, while in the content of smaller objects it will appear larger. • Some cognitive tasks are significantly easier than others. In order, we are good a distinguishing: • Position, length • Direction, Angle, Area • Volume, Curvature, Shade • Color Saturation.
www.bgoncalves.com@bgoncalves Perception • Context also matters! • An object seen in the context of larger objects will appear smaller, while in the content of smaller objects it will appear larger. • And we “fill in the gaps” • Some cognitive tasks are significantly easier than others. In order, we are good a distinguishing: • Position, length • Direction, Angle, Area • Volume, Curvature, Shade • Color Saturation.
www.bgoncalves.com@bgoncalves Perception Biases
www.bgoncalves.com@bgoncalves Human Vision https://en.wikipedia.org/wiki/Photoreceptor_cell light
www.bgoncalves.com@bgoncalves Human Vision https://en.wikipedia.org/wiki/Photoreceptor_cell
www.bgoncalves.com@bgoncalves Human Vision https://en.wikipedia.org/wiki/Photoreceptor_cell
www.bgoncalves.com@bgoncalves Human Vision https://en.wikipedia.org/wiki/Photoreceptor_cell
www.bgoncalves.com@bgoncalves Colors galore!
www.bgoncalves.com@bgoncalves “Who in the rainbow can draw the line where the violet tint ends and the orange tint begins? Distinctly we see the difference of the colors, but where exactly does the one first blendingly enter into the other? So with sanity and insanity.”
 (H. Melville) Color Perception
www.bgoncalves.com@bgoncalves Color Wheel
www.bgoncalves.com@bgoncalves Color Schemes Warm Colors Cold Colors
www.bgoncalves.com@bgoncalves Color Schemes
www.bgoncalves.com@bgoncalves Color Schemes
www.bgoncalves.com@bgoncalves Color Systems Light Ink
www.bgoncalves.com@bgoncalves Colors and Culture http://www1.icsi.berkeley.edu/wcs/
www.bgoncalves.com@bgoncalves Color Blindness
www.bgoncalves.com@bgoncalves Color Blindness https://en.wikipedia.org/wiki/Color_blindness https://github.com/MaPePeR/jsColorblindSimulator/blob/master/colorblind.js
www.bgoncalves.com@bgoncalves Viridis Color Scheme
www.bgoncalves.com@bgoncalves Color Scheme Choosers http://tools.medialab.sciences-po.fr/iwanthue/
www.bgoncalves.com@bgoncalves Color Scheme Choosers http://web.colorotate.org/
www.bgoncalves.com@bgoncalves Color Scheme Choosers http://www.colourco.de/
www.bgoncalves.com@bgoncalves Color Scheme Choosers http://www.colourlovers.com/palettes
www.bgoncalves.com@bgoncalves Color Theory
Visualization
www.bgoncalves.com@bgoncalves Fundamental Principles of Analytical Design
www.bgoncalves.com@bgoncalves Fundamental Principles of Analytical Design 1. Show comparisons, contrasts and differences 2. Show causality, mechanism, explanation and systematic structure 3. Show multivariate data: more than one or two variables 4. Completely integrate words, numbers, images and diagrams 5. Documentation 6.Content matters most of all “Information Visualization is a form of knowledge compression”
 D. McCandless
www.bgoncalves.com@bgoncalves Rules can be broken… …sometimes
www.bgoncalves.com@bgoncalves Fundamental tools • Points • Lines • Areas • Shapes • Colors • Text • Each for these can be used to encode a given variable to produce all the types of plots we are familiar with: • Scatter plot - Just points (line)
www.bgoncalves.com@bgoncalves Fundamental tools • Points • Lines • Areas • Shapes • Colors • Text • Each for these can be used to encode a given variable to produce all the types of plots we are familiar with: • Scatter plot - Just points (line) • Bar chart - Areas
www.bgoncalves.com@bgoncalves Fundamental tools • Points • Lines • Areas • Shapes • Colors • Text • Each for these can be used to encode a given variable to produce all the types of plots we are familiar with: • Scatter plot - Just points (line) • Bar chart - Areas • Bubble chart - Scatter plot + size + color (time)
www.bgoncalves.com@bgoncalves Fundamental tools • Points • Lines • Areas • Shapes • Colors • Text • Each for these can be used to encode a given variable to produce all the types of plots we are familiar with: • Scatter plot - Just points (line) • Bar chart - Areas • Bubble chart - Scatter plot + size + color (time) • Pie chart - Areas + colors
www.bgoncalves.com@bgoncalves Fundamental tools • Points • Lines • Areas • Shapes • Colors • Text • Each for these can be used to encode a given variable to produce all the types of plots we are familiar with: • Scatter plot - Just points (line) • Bar chart - Areas • Bubble chart - Scatter plot + size + color (time) • Pie chart - Areas + colors • etc…
Matplotlib
www.bgoncalves.com@bgoncalves Basic Plotting X Axis YAxis Line Markers Axis Label Axis Axes Matplotlib uses an object oriented structure following the same notation
www.bgoncalves.com@bgoncalves Basic Plotting • Matplotlib uses an object oriented structure following an intuitive notation • Each Axes object contains one or more Axis objects. • A Figure is a set of one or more Axes. • Each Axes is associated with exactly one Figure and each set of Markers is associated with exactly one Axes. • In other words, Markers/Lines represent a dataset that is plotted against one or more Axis. An Axes object is (effectively) a subplot of a Figure. X Y
www.bgoncalves.com@bgoncalves Basic Plotting - Programmatically! X Y
www.bgoncalves.com@bgoncalves Basic Plotting - Programmatically! https://matplotlib.org/2.0.0/ • While the Figure object controls the way in which the figure is displayed. • .gca() - Get the current Axes, creating one if necessary • .show() - Show the final figure • .savefig(“filename.ext”, dpi=300) - Save the figure to “filename.ext” where “.ext” defines the format the saved image () X Y
www.bgoncalves.com@bgoncalves Basic Plotting - Programmatically! • The first step is to import the pyplot module from matplotlib and instanciating a Figure object:
 • The convention is to import pyplot as plt • To create subplots (Axes) you use .subplots(nrows, ncols, sharex=False, sharey=False) instead of .figure(). set sharex and/or sharey to True to keep the same scale in both cases. • .subplots - returns a (fig, ax_lst) tuple where ax_lst is a list of Axes and fig is the Figure. • Axes have several methods of interest: • .plot(x, y) - Make a scatter or line plot from a list of x, y coordinates. • .imshow(mat) - Plot a matrix as if it were an image. Element 0,0 is plotted in the top right corner. • .bar(x, y) - Make a bar plot where x is a list of the lower left coordinates of each bar and y is the respective height. • .pie(values, labels=labels) - Produce a pie plot out of a list of values list and labeled with labels • .savefig(filename) - Write the current figure as an static image import matplotlib.pyplot as plt fig = plt.figure() X Y
www.bgoncalves.com@bgoncalves Matplotlib - decorations • The respective functions are named in an intuitive wa Every Axes object has as methods: • .set_xlabel(label) • .set_ylabel(label) • .set_title(title) • And axis limits can be set using: • .set_xlim(xmin, xmax) • .set_ylim(ymin, ymax) • Tick marks and labels are set using: • .set_xticks(ticks)/.set_yticks(ticks) • .set_xticklabels(labels)/.set_yticklabels(labels) https://matplotlib.org/2.0.0/
www.bgoncalves.com@bgoncalves Matplotlib - Images • .imshow(fig) - Display an image on a set of axes. • fig can be any matrix of numbers. • Further plotting can occur by simply using the functions described above https://matplotlib.org/2.0.0/
https://bmtgoncalves.github.io/DataVisualization/ https://github.com/bmtgoncalves/DataVisualization

Data Visualization using matplotlib