Style Plots using Matplotlib

Import Modules

import matplotlib.pyplot as plt
% matplotlib inline

Fitbit Activity Data

Below is my Fitbit data of daily steps taken and daily calories burned over a 15-day period.

calories_burned = [3092, 2754, 2852, 2527, 3199, 2640, 2874, 2649,
                   2525, 2858, 2530, 2535, 2487, 2534, 2668]
steps = [15578, 8769, 14133, 8757, 18045, 9087, 14367, 11326, 6776,
                   14359, 10428, 9296, 8177, 8705, 9426]

Our Initial Plot with Default Styles

plt.scatter(steps, calories_burned);

png

There's a lot we can improve to make this plot more appealing.

Add a Title

Call the title method and pass in a string value for our title name.

plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken");

png

Add X and Y Axis Labels

Call the xlabel method and pass in a string value for the label name on the x-axis.

Call the ylabel method and pass in a string value for the label name on the y-axis.

plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken")
plt.xlabel("Steps Taken Per Day")
plt.ylabel("Calories Burned Per Day");

png

Adjust the Figure Size

Call the figure method and for the figsize argument, pass in a tuple of (width, height).

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken")
plt.xlabel("Steps Taken Per Day")
plt.ylabel("Calories Burned Per Day");

png

Adjust the Font Sizes

For the methods title, xlabel, ylabel, include a numeric value for fontsize argument to change the font size.

Call the tick_params method and for the labelsize argument, pass in a numeric value to change the font size of the tick values.

Generally, tick values have smaller fonts than axes labels.

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken", fontsize=18)
plt.xlabel("Steps Taken Per Day", fontsize=14)
plt.ylabel("Calories Burned Per Day", fontsize=14)
plt.tick_params(labelsize=12);

png

Adjust the Padding Between the Title and Plot

Include the argument y in the title method and pass in a numeric value.

1.0 is the default value; so 1.03 adds a litle bit of additional padding.

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken", fontsize=18, y=1.03)
plt.xlabel("Steps Taken Per Day", fontsize=14)
plt.ylabel("Calories Burned Per Day", fontsize=14)
plt.tick_params(labelsize=12);

png

Adjust Padding Between Tick Values and the Plot

In the tick_params method, for the argument pad, pass in a numeric value, being the pixel size betwen the tick values and the plot.

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken", fontsize=18, y=1.03)
plt.xlabel("Steps Taken Per Day", fontsize=14)
plt.ylabel("Calories Burned Per Day", fontsize=14)
plt.tick_params(labelsize=12, pad=6);

png

Add Padding Between Axes Labels and Tick Values

In the xlabel and ylabel methods, include the argument labelpad and include a numeric value to add more padding.

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned)
plt.title("Calories Burned Based on Steps Taken", fontsize=18, y=1.03)
plt.xlabel("Steps Taken Per Day", fontsize=14, labelpad=15)
plt.ylabel("Calories Burned Per Day", fontsize=14, labelpad=15)
plt.tick_params(labelsize=12, pad=6);

png

Adjust the Color of our Plot

In the scatter method, pass the value green to the c argument that specifies the color of our scatter points.

plt.figure(figsize=(8, 8))
plt.scatter(steps, calories_burned, c='green')
plt.title("Calories Burned Based on Steps Taken", fontsize=18, y=1.03)
plt.xlabel("Steps Taken Per Day", fontsize=14, labelpad=15)
plt.ylabel("Calories Burned Per Day", fontsize=14, labelpad=15)
plt.tick_params(labelsize=12, pad=6);

png