Data Visualization using Matplotlib Python
Hello all,
This is the fifth article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles
- Python for Data Science Series - Part 1
- Python for Data Science Series - Part 2
- Using Numpy in Python
- Using Pandas in Python
To install matplotlib in your python environment, please use the below command
pip install matplotlib
Please refer the videos below for detailed explanation on matplotlib
After you have installed Matplotlib, please refer the following notebook to understand on how to use its functionalities.
#import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
t
np.sin(2 * np.pi * t)
s = np.sin(2 * np.pi * t)
s
list(range(0,10,2))
plt.plot(t, s)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
#fig.savefig("test.png")
#plt.show()
main_output_folder_path = r"E:\openknowledgeshare.blogspot.com\Python\Outputs"
import os
image_file_path = os.path.join(main_output_folder_path,'Simple_Plot.png')
fig.savefig(image_file_path)
Line Plot¶
a = [1,2,3,4,5]
b = [2,5,8,20,10]
plt.plot(a,b)
plt.grid()
Scatter Plot¶
a = [1,2,3,4,5]
b = [2,5,8,20,10]
plt.scatter(a,b)
Multi Line Plot¶
a = [1,2,3,4,5]
b = [2,5,8,20,10]
c = [10,20,30,40,50]
plt.plot(a,b,label='line1')
plt.plot(a,c,label='line2')
plt.plot(a,b,label='line1')
plt.plot(a,c,label='line2')
plt.legend(loc = 'best')
Subplots¶
a = [1,2,3,4,5]
b = [2,5,8,20,10]
c = [10,20,30,40,50]
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
axs[0].plot(a,b,label='Line1')
fig.add_axes(axs[0])
axs[1].plot(a,c,label='Line2')
fig.add_axes(axs[1])
fig.colorbar(line, ax=axs[0])
plt.legend()
fig.show()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
ax[0][0].plot(a, b) # The method ax is now one array and is referred by indexes
ax[0][0].set_title('Title')
ax[1][0].plot(a, c)
ax[1][0].set_title('Title')
ax[0][1].scatter(a, b) # The method ax is now one array and is referred by indexes
ax[0][1].set_title('Title')
ax[1][1].scatter(a, c)
ax[1][2].set_title('Title')
#plt.tight_layout() # It will separate the graphs to avoid overlays
plt.show()
Histogram¶
a = np.arange(0,100,1)
b = np.random.choice(a,1000)
plt.hist(b,bins=[10,20,50,70,100])
plt.hist(b,bins=20)
HeatMap¶
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
# Load the example flights dataset and convert to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
import seaborn as sns; sns.set(style="ticks", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="day", row="smoker")
g = g.map(plt.hist, "total_bill")
tips['day'].unique()
Comments
Post a Comment