Matplotlib –

Matplotlib is probably the single most used python package for 2D Graphs. It provides vary quick way to visualize data from python and publication-quality figures in many formats. 

import matplotlib.pyplot as plt
plt.plot(x, y)
plt.plot([1,2,3],[5,7,4])
plt.show()



Matplotlib Graph

Adding Labels and Title

import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,7,4]
plt.plot(x, y,)
plt.xlabel("X Co-ordinates")
plt.ylabel("Y C0-ordinates")
plt.title('Interesting graph\nCheck it out')
plt.show()
 Adding legends 
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,7,4]
x2 = [1, 2, 3]
y2 = [10, 14, 12]
plt.plot(x, y, label='stock_price')
plt.plot(x, y, label='First Line')
plt.plot(x2, y2, label='Second Line')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting graph\nCheck it out')
plt.legend()
plt.show()

Changing Colors and Line widths 
import matplotlib.pyplot as plt
x1 = [1,2,3]
y1 = [5,7,4]
x2 = [1, 2, 3]
y2 = [10, 14, 12]
toset pig size
plt.figure(figsize=(15,6))
plt.plot(x1, y1, color="blue", linewidth=2.5,
linestyle="--", label="first line")
plt.plot(x2, y2, color="red", linewidth=2.5,
linestyle="-", label="second line")
plt.xlabel("plot number")
plt.ylabel("variables")
plt.legend()
plt.show()
Bar Charts
With bar charts, each column represents a group defined by a categorical variable;


x1 = [2,4,6,8,10]
y1 = [8,6,2,5,6]
x2 = [1,3,5,7,9]
y2 = [5,2,7,8,2]
plt.bar(x1,y1, label='Bar1', color='r')
plt.bar(x2, y2, label='Bar2', color='c')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting graph\nCheck it out')
plt.legend()
plt.show()

Histogram
histograms, each column represents a group defined by a continuous, quantitative variable.


population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,
110,120,121,122,130,111,115,112,
80,75,65,54,44,43,42,48]
print(len(population_ages))
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
ids = [x for x in range(len(population_ages))]
plt.bar(ids, population_ages)
plt.hist(population_ages, bins, histtype='bar', rwidth=0.2)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
Scatter Plot 


import numpy as np
import matplotlib.pyplot as plt
n = 1024
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
print(x)
print(y)
milage = [10, 20, 25, 30]
price = [2000, 2300, 2500, 3000]
no_of_accident = [1, 2, 3, 4]
price_1 = [2000, 1800, 1500, 1000]


#aplha blending value-0 and 1
plt.scatter(milage, price, s=100, c='r', alpha=0.4)
plt.scatter(no_of_accident, price_1, s=100, c='b', alpha=0.4)
#plt.xlim(-1.5, 1, 1.5), plt.xticks([])
#plt.ylim(-1.5, 1, 1.5), plt.yticks([])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.show()

[-0.84507974 -0.04957022 0.74391362 … 0.01690187 0.48294461 -0.33967639] [ 0.64663892 0.11162714 -0.44525569 … -0.53222313 -0.09144687 0.00128311]


Pie Charts 

slices = [7, 2, 2, 13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']

plt.pie(slices, labels=activities,
colors= cols,
startangle = 90,
shadow = True,
explode = (0, 0.1, 0, 0),
autopct = '%1.1f%%')

plt.title('Interesting Graph\nCheck it out')
plt.show()

Leave a Reply