0% found this document useful (0 votes)
77 views7 pages

Tufte-Style Plots in Python

The document discusses implementing Edward Tufte's principles for data visualization in Python using Matplotlib. It summarizes Tufte's recommendations from his book "The Visual Display of Quantitative Information". It then shows how to create minimal line plots in the style of Tufte's designs using Matplotlib's object-oriented interface. This includes modifying font settings and adding data labels and lines to the plot. In the next post, the author plans to demonstrate additional Tufte-inspired plots like minimal boxplots and dot-dash plots.

Uploaded by

mr blah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views7 pages

Tufte-Style Plots in Python

The document discusses implementing Edward Tufte's principles for data visualization in Python using Matplotlib. It summarizes Tufte's recommendations from his book "The Visual Display of Quantitative Information". It then shows how to create minimal line plots in the style of Tufte's designs using Matplotlib's object-oriented interface. This includes modifying font settings and adding data labels and lines to the plot. In the next post, the author plans to demonstrate additional Tufte-inspired plots like minimal boxplots and dot-dash plots.

Uploaded by

mr blah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tufte in Python – Part One

anishazaveri1 May 29, 2020

Edward Tufte recommends several principles for


representing data in his book “The Visual Display of
Quantitative Information“. Here are some that I find
especially useful:

1. The representation of numbers, as physically


measured on the surface of the graphic itself, should
be directly proportional to the numerical quantities
represented (p. 77)
2. Write out explanations of the data on the graphic
itself. Label important events in the data (p.77)
3. Show data variation, not design variation (p. 77)
4. Maximize the data-ink ratio. Erase non-data ink and
redundant data-ink (p.105)

Inspired by Lukasz Piwek‘s implementation of Tufte’s


design principles in R, I decided to attempt the same in
Python. In this post, I will show you how to create Tufte-
style line plots, such as this:
Python has an array of visualization packages (here‘s a
good overview). My go-to package till now has been
plotnine, which provides a ggplot2 interface in Python. It’s
elegant and has allowed me to escape learning Python’s
infamously annoying package, matplotlib. However, for all
the flak it gets, matplotlib is the most customizable and
powerful visualization package Python offers. It also
serves as the base for most other Python visualization
tools (including plotnine). So, here’s a guide to
implementing Tufte’s principles in Python using matplotlib.

A very short introduction to Matplotlib


A confusing aspect of matplotlib is the existence of two
APIs. This is possibly one of it’s biggest flaws. It makes
troubleshooting bugs very difficult since answers on
StackOverflow frequently jump between the two APIs.

1. MATLAB-style API: Matplotlib was originally written


to mimic MATLAB, and the pyplot (plt) interface
provides a collection of MATLAB-like commands
2. Object oriented API: This API is more flexible, and the
one to use if you want better control and
customization.

I recommend using the object-oriented interface. This


ensures that you have standard syntax irrespective of
whether your plot is simple or complex

Here are the highlights of my matplotlib reading. It took


me about 30 min to get familiar with the basic syntax.

1. Lifecycle of a plot: A good matplotlib primer using the


object-oriented interface. Pay special attention to the
definition of Fig and Axes under ‘A note on the
Object-Oriented API vs. Pyplot‘. I recommend
keeping this figure open on the side as reference.
After reading this you should feel comfortable with
~80% of the visualizations in this post
2. Get familiar with Artists: Everything in your plot is
basically an Artist. Recognizing this is useful when
you want to customize the default instances of your
objects.
3. Get familiar with GridSpec: This will be useful for the
scatter-histogram plots, which we will create in Part 2
of this post.

Minimal lineplot
The plot we will replicate is found in The Visual Display of
Quantitative Information, p.68.

First, let’s modify the default matplotlib font. The font in


Tufte’s plot is an oldstyle serif font, one where the
numerals don’t line up at the top and the bottom. After
some Googling I settled on ‘Sabon Roman OsF‘ and
downloaded and installed the .ttf version of the font.

To install the font in matplotlib, I deleted the fontList file


from matplotlib’s font cache (find this by running
print(matplotlib.get_cachedir())). Next, modify the
rcParams to use our new font as the default serif font (you
may have to restart your kernel to get this to work)

Next, using the original plot as reference, I created some


data:

Now let’s initialize and modify the figure and axes:

At this point, here’s how our plot looks:

Time to add some data!

Here’s the final plot:


Now that we know our way around Tufte-style line plots,
we can replicate a more complicated example from The
Visual Display of Quantitative Information, p.75. The dark
line-segment between 1955-1956 indicates stricter
enforcement by Connecticut policemen against cars
exceeding the speed limit. Data from other states is
provided for comparison.

Final figure:
In the next post we will look at some other Tufte-style
plots, including a minimal boxplot and a dot-dash plot.

Common questions

Powered by AI

According to Edward Tufte's principles, it is important to ensure that representations of numerical quantities in visualizations are directly proportional to the represented data to maintain accuracy and integrity in data interpretation. Misrepresentation can lead to misinterpretation, as disproportionate visual elements can distort the viewer's understanding of the actual data quantities .

Challenges in familiarizing oneself with matplotlib include navigating its two different APIs and troubleshooting bugs due to this confusion. These can be overcome by choosing the object-oriented API for consistent and flexible plotting, investing time in understanding its lifecycle concepts, and utilizing community references such as StackOverflow and documentation efficiently .

The key components of the lifecycle of a plot in the object-oriented API of matplotlib include figures and axes. These elements provide the structure and context for placing data elements, ensuring that plots are organized and efficiently managed, which facilitates the creation of complex, customizable visualizations .

Using custom fonts in matplotlib visualizations can improve aesthetics and legibility, contributing to the overall design adherence to specific styles and themes, such as those recommended by Tufte. Effective integration involves modifying rcParams to use the new font as the default, ensuring it aligns with the visual style goals, and managing font caches to see changes take effect .

Applying Tufte's principle of showing data variation over design variation in line plots involves focusing on accurately visualizing changes and trends in the data rather than altering design elements which may distract the viewer. This approach emphasizes clarity in displaying numerical facts, ensuring the plot communicates the true nature of the dataset effectively .

The MATLAB-style API in matplotlib offers a collection of MATLAB-like commands which can be intuitive for those familiar with MATLAB, while the object-oriented API is more flexible and allows for greater control and customization. The document recommends the object-oriented API because it ensures standard syntax regardless of complexity, which is beneficial for consistent and manageable code .

Erasing non-data ink and redundant data-ink improves data visualization by increasing the data-ink ratio, which helps to focus the viewer's attention on the essential components of the data. It strips away unnecessary elements that could distract or confuse the audience, thereby enhancing the clarity and effectiveness of the visual representation .

Understanding the concept of 'Artists' in matplotlib assists in the customization of visual plots because every element in a plot is essentially an Artist. Recognizing this allows for targeted and precise customization of each default object to tailor visual elements exactly as desired, contributing to more accurate and effective data representation .

Modifying default elements like fonts in matplotlib is important to achieve adherence to specific visual styles, such as Tufte's, because it ensures that the visualization aligns with the intended aesthetic and functional objectives. This customization portrays the data more clearly and adheres to visual design principles that enhance understanding .

Tufte's principles suggest writing out explanations of the data directly on the graphic and labeling important events. This approach provides context and aids understanding by clarifying what specific elements of the data represent to the audience, thus enhancing the overall comprehension of the visualized information .

You might also like