![]() The basic idea is that you provide the input data/formula and pgfplots does the rest. The pgfplots package, which is based on TikZ, is a powerful visualization tool and ideal for creating scientific/technical graphics. 2 Basic example (also externalizing the figures).1.2 Compilation time (brief background).# Code from above ('fivethirtyeight') df.plot('weight', 'mpg', kind='scatter', xlabel='Mileage (mpg)', ylabel='Weight (lbs.)', figsize=(8,4)) plt.axhline(29, color='green', linewidth=1) plt.axhline(16, color='red', linewidth=1) plt.text(2400, 30, "Green Line Indicates Optimal Efficiency Requirements", color='green', fontsize=14) plt.text(1700, 14, "Red Line Indicates Minimum Efficiency Requirements", color='red', fontsize=11) # Adding an annotation with an arrow 'prop' plt.text(2200, 36, 'Toyota Corolla (31mpg 2,200lbs)', color='green', fontsize=14) plt.annotate('Heaviest/Least expensive above optimal', xy=(2200, 31), xytext=(2200, 33.75), color='green', arrowprops=$ For Fleet Purchases',fontsize=13) plt. The last code example and the last visual also add titles with a few basic font formatting suggestions. Below we’ll add an annotation to the Toyota Corolla with plt.text(), to add text, and plt.annotate() which along with the arrowprops=() option adds a helpful arrow. For example, above we annotated the reference lines. Sometimes annotations help readers understand another additional contexts. Let's fix that omission with an annotation.Īnnotations point and explain specific data points. We’re not sure which dots represent which vehicle. But, it isn’t quite clear exactly which vehicle we should buy. Using the data and code specified herein. # Standard Import(s) import pandas as pd import matplotlib.pyplot as plt # Load Data df = pd.read_stata(' ') # Generate scatter with updated dimensions, labels, and theme ('fivethirtyeight') df.plot('weight', 'mpg', kind='scatter', xlabel='Mileage (mpg)', ylabel='Weight (lbs.)', figsize=(8,4)) ![]() After deciding which theme to apply, here we’ll proceed with the popular theme modeled after FiveThiryEight’s style, do so with ('fivethirtyeight'). To explore the option of changing to a pre-defined theme consider exploring the output from. ![]() This next iteration will also add more meaningful and explicit axis titles with xlabel and ylabel. The handy figsize=() option updates the figure size. A wider aspect ratio is typically better for most presentation formats (wide slide decks, and the half-or-so page you can see on a pdf report, etc). The first thing we can do is update the scale. The full self-contained story is the goal. We have several (many) adjustments before we can call this plot a full and self-contained story. Many readers may recognize how just a few lines of code using pandas alone, which takes an assist from matplotlib under the hood, to produce a simple scatter plot (easy peasy): # Standard Import(s) import pandas as pd import matplotlib.pyplot as plt # Load Data df = pd.read_stata(' ') # Creating a plot df.plot('weight','mpg',kind='scatter') Our task is to find the most cost-effective vehicle above the optimal efficiency standard that also weighs the most. Suppose, the folks in fleet management have established 16 miles per gallon as the minimum fleet vehicle efficiency requirement and 29 miles per gallon as the optional efficiency standard. Each observation is a single vehicle and we know a range of important points about our data including make, price, efficiency (mpg), and other factors. Using the handy 1978 automobile data from Stata ( that I’ve previously written about) we will also pretend we’re in the 1970s. Your company has asked you to help decide what vehicle it should purchase for its fleet. Yet another version shows this process in Stata. A companion article will shows the same in Seaborn. This article will show how to take your visualization from a simple scatter plot to a full story. When done well this might mean your audience will respond (in their mind’s voice): When preparing a scatter plot you can make the single plot tell a complete story. And when prepared correctly, they’ll be easily understood by many data (and so-called non-data) folk. They illustrate relationships between two (or more) variables. Scatter plots are an important tool for many analysts and data professionals. Telling a complete story by enhancing your scatterplot Overview
0 Comments
Leave a Reply. |