9.2: Encoding Data That Change Over Time

Principles of Data Science · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    9.2: Encoding Data That Change Over Time
    Learning Objectives

    By the end of this section, you should be able to:

    • 9.2.1 Create and interpret labeled graphs to visually identify trends in data over time.
    • 9.2.2 Use Python to generate various data visualizations for time series data.

    Researchers are frequently interested in analyzing and interpreting data over time. Recall from Time and Series Forecasting that any set of data that consists of numerical measurements of the same variable collected and organized according to regular time intervals may be regarded as time series data. A time series graph allows data scientists to visually identify trends in data over time. For example, a college administrator is very interested in tracking enrollment data from semester to semester. A marketing manager is interested in tracking quarterly revenue for a business. An investor might be interested in tracking stock prices on a monthly basis.

    In many cases, a time series graph can reveal seasonal patterns to cycles, which is important information to help guide many business decisions, such as planning marketing campaigns or setting optimal inventory levels. Time series graphs also provide important data for forecasting methods, which attempts to predict future trends based on past data.

    Refer to Examples of Time Series Data for the basics of visualizing time series data in Excel and Python. The Trend Curve and Trend-Cycle Component explores visualization of a trend curve. Here we’ll explore line charts, trend curves, and the use of Python for time series data visualization in more detail.

    Line Charts and Trend Curves

    A time series graph is used in many fields such as finance, health care, economics, etc. to visualize data measurements over time. To construct a time series graph, the horizontal axis is used to plot the time increments, and the vertical axis is used to plot the values of the variable that we are measuring, such as revenue, enrollment, inventory level, etc. By doing this, we make each point on the graph correspond to a point in time and a measured quantity. The points on the graph are typically connected by straight lines in the order in which they occur. Once the chart is created, a researcher can identify trends or patterns in the data to help guide decision-making.

    Example 9.5

    The following data plots the enrollment at a community college over an eight-year time period. Create a times series graph of the data and comment on any trends (Table 9.3).

    Year Enrollment
    1 8641
    2 9105
    3 9361
    4 9895
    5 9230
    6 8734
    7 8104
    8 7743
    Table 9.3 Community College Enrollment
    Answer

    To create a time series graph for this dataset, start off by creating the horizontal axis by plotting the time increments, from years 1 to 8. For the vertical axis, used a numeric scale extending from 4,000 to 12,000 since this covers the span of the numeric enrollment data.

    Then for each row in the table, plot the corresponding

    Using Python for Times Series Data Visualization

    Data visualization is a very important part of data science, and Python has many built-in graphing capabilities through a package called matplotlib. To import this package into a Python program, use the command:

    import matplotlib.pyplot as plt

    Matplotlib contains functions such as plot that can be used to generate time series graphs.

    After defining the data to be graphed, the Python function plt.plot(x, y) can be used to generate the time series chart.

    Example 9.6

    Use the plot function in Python to create a time series graph for the dataset shown in Example 9.5.

    Answer

    In the following Python code, note that the plt.plot with marker specifies that points should be plotted at the various points. In addition, plt.ylim specifies the range for the y-axis.

    Python Code

        # import the matplotlib library
        import matplotlib.pyplot as plt
        import matplotlib.ticker as ticker
        
        # Define the x-data
        x = [1, 2, 3, 4, 5, 6, 7, 8]
        
        # Define the y-data
        y = [8641, 9105, 9361, 9895, 9230, 8734, 8104, 7743]
        
        # Use the plot function to generate a time series graph
        plt.plot(x, y, marker='o', linestyle='-')
        
        # Set the scale for the y-axis
        plt.ylim(6000, 11000)  # This sets the y-axis range from 6000 to 11000
        
        # Define a function to format the ticks with commas as thousands separators
        def format_ticks(value, tick_number):
            return f'{value:,.0f}'
        
        # Apply the custom formatter to the y-axis
        plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(format_ticks))
        
        # Add labels to the x and y axes by using xlabel and ylabel functions
        plt.xlabel("Year")
        plt.ylabel("Enrollment")
        
        # Add a title using the title function
        plt.title("Enrollment at a Community College")
        
        # Show the plot
        plt.show()
        

    The resulting output will look like this:

    A line graph labeled enrollment at a community college. The X axis representing years ranges from 1 to 8 and the Y axis for enrollment ranges from 6,000 to 12,000. The line created by the 8 data points indicates that the enrollment started at about 8,600, gradually increased over years 1 to 4 to a peak of 9,800, then began to decrease from years 4 to 8 to a low around 7,700.
    Table 9.4 Summary of Visualization Methods Based on Goals and Data

    This page titled 9.2: Encoding Data That Change Over Time was last modified on Tue, 22 Apr 2025 14:36:22 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.