Data visualization [from programming to drawing]: 3. Histogram and thermodynamic diagram

Time:2020-2-15

Reference source: vitu.ai

In the previous article, you have learned how to draw line graphs. Next, we will learn about histogram and heat graph

Set up your notebook

Let’s just set it up at the beginning

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Print ("setup complete")

Select data set

In this article we will use data from the U.S. Department of transportation on flight delays

Click here to download the dataset

Open it in Excel as follows:

Data visualization [from programming to drawing]: 3. Histogram and thermodynamic diagram

We can see that each cell represents the average delay time (minutes) of each airline in each month in 2015, and the negative number represents the early arrival of flights, for example, American Airlines (AA) delays an average of 7 minutes in each month

Let’s upload the CSV file to VITU’s dataset space

Data visualization [from programming to drawing]: 3. Histogram and thermodynamic diagram

Next, we use panda to load this file:

# Path of the file to read
flight_filepath = "flight_delays.csv"

# Read the file into a variable flight_data
flight_data = pd.read_csv(flight_filepath, index_col="Month")

You will notice that our read ﹣ CSV code is a little different from the previous chapter. This time, the data month is no longer dates, so we don’t need to add parse ﹣ dates = true

It’s time to check the data

Since the data is relatively small this time, we can take a look at the whole dataset

# Print the data
flight_data

Histogram

For example, we want to draw a histogram to show the average delay time of spirit Airlines (NK) by month

# Set the width and height of the figure
plt.figure(figsize=(10,6))

# Add title
plt.title("Average Arrival Delay for Spirit Airlines Flights, by Month")

# Bar chart showing average arrival delay for Spirit Airlines flights by month
sns.barplot(x=flight_data.index, y=flight_data['NK'])

# Add label for vertical axis
plt.ylabel("Arrival delay (in minutes)")

Data visualization [from programming to drawing]: 3. Histogram and thermodynamic diagram

This creates a histogram

sns.barplot(x=flight_data.index, y=flight_data[‘NK’])

There are so many points in it

  • Sns.bartlot – this tells notebook that we want to draw a histogram
  • X = flight data. Index – this indicates that the X coordinate uses the index of the flight data column, which is the month
  • Y = flight_data [‘nk ‘] – this is the Y coordinate using the delay data of NK

Thermodynamic chart

There is another diagram to introduce in this article

We will create a thermal diagram in the following code to see the flight_data. Each cell is colored according to the actual value

# Set the width and height of the figure
plt.figure(figsize=(14,7))

# Add title
plt.title("Average Arrival Delay for Each Airline, by Month")

# Heatmap showing average arrival delay for each airline by month
sns.heatmap(data=flight_data, annot=True)

# Add label for horizontal axis
plt.xlabel("Airline")

Data visualization [from programming to drawing]: 3. Histogram and thermodynamic diagram

In the picture, you can see which airline has the most serious delay, NK!

Original address: data visualization [from programming to drawing]: 3. Histogram and thermal diagram

Recommended Today

Monkey patch monkey patch programming method and its application in Ruby

What ismonkey patch (Monkey Patch)? In a dynamic language, functions are added and changed without modifying the source code. Purpose of using monkey patch:1. Additional function2. Function change3. Fix program errors4. Add a hook to execute some other processing while executing a method, such as printing logs, realizing AOP, etc,5. Cache, when the amount of […]