Thursday, 24 March 2022

P#16 Line, Bar, Scatter, Pie charts

MATPLOTLIB...

In this blog, we will see how to plot various chart types using plt.line(), plt.bar(), plt.barh(). plt.scatter(), plt.hist(). 
If you run this code which is self explanatory, you will get this chart. 
def plotline():
import matplotlib.pyplot as plt
langs = ['B.E CSE', 'B.E. Marine', 'B.E. ECE', 'B.Sc(NatSci)', 'MBA']
students = [20, 40, 60, 80, 100]
plt.plot(langs, students) # plot
plt.xlabel('Degree')
plt.ylabel('Strength')
plt.grid()
plt.savefig('line.png')
plt.show()
plotline()
If you run this code, you will get this chart. 
def plotbar():
langs = ['B.E CSE', 'B.E. Marine', 'B.E. ECE', 'B.Sc(NatSci)', 'MBA']
students = [20, 40, 60, 80, 100]
plt.bar(langs, students, col) # BAR
plt.xlabel('Degree')
plt.ylabel('Strength')
plt.grid()
plt.savefig('bar.png')
plt.show()
plotbar()

def plotbarh():
langs = ['B.E CSE', 'B.E. Marine', 'B.E. ECE', 'B.Sc(NatSci)', 'MBA']
students = [20, 40, 60, 80, 100]
plt.barh(langs, students, color = 'hotpink') # BARh Pink
plt.xlabel('Degree')
plt.ylabel('Strength')
plt.grid()
plt.savefig('bar.png')
plt.show()
plotbarh() plots horizontal bar as shown below.
def plotbarss():
import numpy as np
import matplotlib.pyplot as plt
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
X = np.arange(4)
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.bar(X + 0.00, data[0], color='b', width=0.25)
ax.bar(X + 0.25, data[1], color='g', width=0.25)
ax.bar(X + 0.50, data[2], color='r', width=0.25)
ax.legend(labels=('cse', 'it', 'mech', 'mba'), loc='upper right')
plt.savefig('bars.png')
plt.show()
plotbarss() displays the chart as shown below with three bars.
def plotscat():
langs = ['B.E CSE', 'B.E. Marine', 'B.E. ECE', 'B.Sc(NatSci)', 'MBA']
students = [20, 40, 60, 80, 100]
plt.scatter(langs, students) # Scatter
plt.xlabel('Degree')
plt.ylabel('Strength')
plt.grid()
plt.savefig('scat.png')
plt.show()
plotscat()
plt.scatter() wil plot scatter plot as shown below()
def plotscatc():
np.random.seed(19680801) # seed the random number generator.
data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.scatter('a', 'b', c='c', s='d', data=data, marker='*')
ax.set_xlabel('entry a')
ax.set_ylabel('entry b');
plt.savefig('scatcc.png')
plt.show()
plotscatc()
This will print different size, different colors.

This will print marker different size, different colors. 
marker = “*” sets the plot symbol as *. s = different size of the marker symbol in the plot. 

def plothist():
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.savefig('hist.png')
plt.show()
plt.hist() method plots histogram as shown below;
def plotpie():
import matplotlib.pyplot as plt
import numpy as np
y = np.array([32, 40, 60, 80, 100])
plt.pie(y)
plt.savefig('pie.png')
plt.show()
plotpie()
plot.pie() method to plot y.
def plotpie1():
deg = ['B.E CSE', 'B.E. Marine', 'B.E. ECE', 'B.Sc(NatSci)', 'MBA']
students = [20, 40, 60, 80, 100]
# Creating plot
fig = plt.figure(figsize=(8, 6))
plt.pie(students, labels=deg)
# show plot
plt.savefig('pie1.png')
plt.show()
plotpie1()

This is pie plot with labels. That is provide by plt.pie(students, labels=deg)

Happy learning with AMET ODL!!!




No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...