Course Content
Visualization in Python with matplotlib
Visualization in Python with matplotlib
The Simplest Scatter Plot
Welcome to the third section of the course! Let's consider one more plot type widely used in analytics - scatter plots. This type of plot is quite easy to understand - it's just a collection of points with specific coordinates. Often this type of plot is used in experiments or discovering if there is relation between factors.
To build a scatter plot we need to do the same as for a simple line plot. But this time we use .scatter()
applied to Axes
object (instead of .plot()
in the previous section). The first parameter of this function will be the x-axis, and the second - y-axis. Throughout this section, you will use the 'gapminder' dataset (as of 2017), which contains different economical, demographical, social indicators. Before we start, let's explore the data you will work with.
# Import the libraries import pandas as pd import matplotlib.pyplot as plt # Reading the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0) # Describing the data print(data.columns) print(data.info())
For instance, let's visualize on scatter plot the gdp per capita and share of population with access to the Internet.
# Import the libraries import pandas as pd import matplotlib.pyplot as plt # Reading the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0) # Create Figure and Axes objects fig, ax = plt.subplots() # Initialize a scatter plot ax.scatter(data['gdp per capita'], data['internet users']) # Display the plot plt.show()
For instance, if you run the code above, you will see that there is a relation between economical wellness and internet availability for people.
Disclaimer: FREE DATA FROM WORLD BANK VIA GAPMINDER.ORG, CC-BY LICENSE.
Thanks for your feedback!