In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
In [2]:
np.random.seed(0)
sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.show()
In [3]:
df = pd.DataFrame(np.random.randn(50).reshape(10,5))
df
Out[3]:
In [4]:
#corr() is used to find the pairwise correlation of all columns in the dataframe.
corr = df.corr()
corr
Out[4]:
In [5]:
sns.heatmap(corr)
plt.show()
In [6]:
#set the lower and upper bound of the heatmap color bar. Here, we passed 0 value as lower bound and 1 as upper bound
ax1 = sns.heatmap(corr, cbar=0, linewidths=2, vmax=1, vmin=0, square=True, cmap='Blues')
plt.show()
In [7]:
sns.set()
flights = sns.load_dataset("flights")
flights
Out[7]:
DataFrame.pivot(index=None, columns=None, values=None)
Parameters
index: Column to use to make new frame’s index. If None, uses existing index.
columns: Column to use to make new frame’s columns.
values: Column(s) to use for populating new frame’s values.
If not specified, all remaining columns will be used and the result will have hierarchically indexed columns.
Returns
reshaped DataFrame
In [8]:
#Reshape data (produce a “pivot” table) based on column values.
flights = flights.pivot("month", "year", "passengers")
flights
Out[8]:
In [9]:
flights.columns
Out[9]:
In [10]:
ax = sns.heatmap(flights)
plt.title("Heatmap Flight Data")
plt.show()
In [11]:
ax = sns.heatmap(flights, cmap="coolwarm")
plt.title("Heatmap Flight Data")
plt.show()
In [12]:
plt.figure(figsize=(16,9))
ax = sns.heatmap(flights, cmap="coolwarm", annot = True)
plt.title("Heatmap Flight Data")
plt.show()