Part 8: Visualization

In this section, we use the dataset cargame.csv to demonstrate how to create basic graphical displays in Python. Below is the scenario for the data:

A toy company has four types of vehicles for sale: car, truck, racer, and taxi. To judge the quality of the different types of vehicles, a team records the following characteristics: the user’s gender (Gender), the type of a car in the trial, the distance vehicle is pulled back (Pull Distance), the distance the car goes after pulling it back (Distance) and the time for each trial in seconds (Time).

First, we use the module Pandas to open and read the data.

We then prepare the data for Visualization.

Bar Charts

Note: The y-axis can also be changed to represent the relative frequency. Can you figure out how to do this?

In the following example, we study the distribution of average distance that each type of vehicles covered and create a horizontal bar chart by using the Python function plt.barh().

In the following example, we study the stacked bar chart for the total distance grouped by two variables: Name of Car and Distance. Here we use the plot() function in the module Pandas. In the legend method, we use two parameters: loc and ncol.

  • loc indicates the location of the legend, it can be an integer (0 to 10) or a string or a pair of floats.
  • ncol is an integer that shows the number of columns that the legend has.

Tutorials for learning how to create Python bar charts can be found at matplotlib, PythonSpot, pyplot, Plotly, pandas, and seaborn (You need to download the library first, but there are lots of good features. Highly recommended for professional data visualization!).

Line Plots

Scatter Plots

Histogram

Congratulations! You've completed the Python Coding Tutorials! If you need more hands-on help, check out our Coding Office Hours.