Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Simple Line Chart | Basics: Line Charts
Visualization in Python with matplotlib
course content

Course Content

Visualization in Python with matplotlib

Visualization in Python with matplotlib

1. Basics: Line Charts
2. Bar Charts
3. Scatter Plots

bookSimple Line Chart

That's good! Let's fill our empty plot with some data.

We can pass two iterable objects (such as, lists, arrays, tuples) as parameters for the .plot() function, and they will be used for building the plot. By default, all the points will be connected with the blue line in the respective order as they are stored in the objects.

For example, in the following chapters you will work with the CO2 emission level data. The data is stored as a Series object with years being indices, and CO2 levels being values. Before we start, let's inspect the data we will work with.

123456789
# Import the libraries import matplotlib.pyplot as plt import pandas as pd # Load the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0) # Inspect the data print(data.head())
copy

Let's visualize the CO2 emission level over years for Italy.

123456789101112131415161718
# Import the libraries import matplotlib.pyplot as plt import pandas as pd # Load the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0) # Filter the data ita = data.loc['Italy'] # Create Figure and Axes objects fig, ax = plt.subplots() # Initialize the plot ax.plot(ita.index.astype(int), ita.values) # Display the plot plt.show()
copy

Here we used indices (the .index attribute) as values for the x-axis, and emission levels (the .values attribute) as values for the y-axis. Values for the x-axis were converted into integers (by applying the .astype() method since Python read them as strings initially).

Disclaimer: FREE DATA FROM WORLD BANK VIA GAPMINDER.ORG, CC-BY LICENSE.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
some-alt