Changing Plot Design: Modifying Graph Components

So far, we've looked at the main graphs used in analyzing data, either directly or grouped, for comparison and trend visualization. But one thing that we can see is that the design of each graph is different from the others, and we don't have basic things such as a title and legends.

We've learned that a graph is composed of several components, such as a graph title, x and y labels, and so on. When using Seaborn, the graphs already have x and y labels, with the names of the columns. With Matplotlib, we don't have this. These changes are not only cosmetic.

The understanding of a graph can be greatly improved when we adjust things such as line width, color, and point size too, besides labels and titles. A graph must be able to stand on its own, so title, legends, and units are paramount. How can we apply the concepts that we described previously to make good, informative graphs on Matplotlib and Seaborn?

The possible number of ways that plots can be configured is enormous. Matplotlib is powerful when it comes to configuration, but at the expense of simplicity. It can be cumbersome to change some basic parameters in a graph using Matplotlib and this is where Seaborn and other libraries can be of help. But in some cases, this is desirable, for instance in a custom graph, so having this capacity somewhere in the stack is necessary. We will focus on how to change a few basic plot parameters in this section.

Title and Label Configuration for Axis Objects

As we said before, the object-oriented API for Matplotlib is the one that provides greater flexibility. Let's explore how to configure the title and labels on axis objects in the following exercise.

Exercise 14: Configuring a Title and Labels for Axis Objects

Perform the following steps to configure a title and labels for the axis objects. We will continue from the previous exercise and follow these steps:

  1. Set the title, the x-axis label, and the y-axis label by calling the set method:

    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    ax.set(title="Graph title", xlabel="Label of x axis (units)", ylabel="Label of y axis (units)")

    ax.plot()

    The plot is as follows:

    Figure 2.20: Configuring a title and labels

  2. The legends for the plot can be either passed externally, when using only Matplotlib, or can be set on the Pandas plot and plotted with the axes. Use the following command to plot the legends:

    fig, ax = plt.subplots()

    df.groupby('year')['horsepower'].mean().plot(ax=ax, label='horsepower')

    ax.legend()

    The plot is as follows:

    Figure 2.21: Line graph with legends

  3. The alternative method for plotting the legends is as follows:

    fig, ax = plt.subplots()

    df.groupby('year')['horsepower'].mean().plot(ax=ax)

    ax.legend(['horsepower'])

    The plot is as follows:

Figure 2.22: Line graph with legends (alternate method)

Line Styles and Color

For line graphs, the color, the weight, markers, and the style of the lines can be configured with the ls, lw, marker, and color parameters:

df.groupby('year')['horsepower'].mean().plot(ls='-.', color='r', lw=3)

Figure 2.23: Line graph with color and style

Figure Size

We can also configure the size of the figure. The figsize parameter can be passed to all plot functions as a tuple (x-axis, y-axis) with the size in inches:

df.plot(kind='scatter', x='weight', y='horsepower', figsize=(20,10))

Figure 2.24: Plot with bigger figure size

Exercise 15: Working with Matplotlib Style Sheets

Matplotlib has some style sheets that define general rules for graphs, such as background color, ticks, graph colors, and palettes. Let's say that we want to change the style so our graph has better colors for printing. To accomplish that, follow these steps:

  1. Let's first print the list of available styles using the following command:

    import matplotlib.pyplot as plt

    print(plt.style.available)

    The output is as follows:

    ['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']

  2. Now, let's create a scatter plot with the style as classic. Make sure you import the Matplotlib library first before proceeding:

    %matplotlib inline

    import numpy as np

    import matplotlib.pyplot as plt

    url = ('https://raw.githubusercontent.com/TrainingByPackt/Big-Data-Analysis-with-Python/master/Lesson02/Dataset/auto-mpg.data')

    df = pd.read_csv(url)

    column_names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'year', 'origin', 'name']

    df = pd.read_csv(url, names= column_names, delim_whitespace=True)

    df.loc[df.horsepower == '?', 'horsepower'] = np.nan

    df['horsepower'] = pd.to_numeric(df['horsepower'])

    plt.style.use(['classic'])

    df.plot(kind='scatter', x='weight', y='horsepower')

    The output is as follows:

Figure 2.25: Scatter plot with the style as classic

Note

To use a style sheet, use the following command:

plt.style.use('presentation')

One of the changes that Seaborn makes when it is imported is to add some styles to the list of available ones. Styles are also useful when creating images for different audiences, such as one for visualization in a notebook and another for printing or displaying in a presentation.