DEV Community

muradalhasan
muradalhasan

Posted on

Matplotlib

Matplotlib is a low level graph plotting and open source library was created by John D. Hunter.There are two types of method to creat a matplotlib plot functional and Object oriented method.
*To plot a function we need two array of ponits and plot() function then show() function

xpoints = [0, 6]
ypoints = [0, 120]

plt.plot(xpoints, ypoints)
plt.show()
Enter fullscreen mode Exit fullscreen mode

and it will plot a graph according to x-ponits and y-pointes

  • size(ms),marker_color(mec),ful_marker_color(mec),linestyle, linewidth teg of plot function
ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o',color="hotpink", ms = 20,mec="yellow",mfc="blue",linestyle="--",linewidth=10)
plt.show()
Enter fullscreen mode Exit fullscreen mode

marker function mark the satisfied points
color function gives color of lines
ms decide the size
mec decide the marker circle color
mfc decide marker color
linestyle decides linestyle
linewidth decides linewidth of line
*lebeling the axis and plot title

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)
plt.title("This is an example title",color='red',size=20,loc='right')
plt.xlabel("Average Pulse",color='blue',size=20,family='serif')
plt.ylabel("Calorie Burnage",color='yellow',size=20,family='serif')

plt.show()
Enter fullscreen mode Exit fullscreen mode

*grid() functionn and it's attributes

plt.grid(axis='x')
plt.show()

plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
Enter fullscreen mode Exit fullscreen mode

*subplot function

plt.subplot(2, 3, 1)
plt.plot(x,y)
Enter fullscreen mode Exit fullscreen mode

2 in subplot means 2 rows and 3 column and position of that subplot is 1
*scatter diagram.we can plot multiple scatter in a single graph

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.scatter(x, y, color = 'hotpink')

#day two, the age and speed of 15 cars:
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)
plt.scatter(x, y, color = '#88c999')
plt.show()
Enter fullscreen mode Exit fullscreen mode

*sctter diagram attributes

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()

plt.show()
Enter fullscreen mode Exit fullscreen mode

*Bar diagram

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()
Enter fullscreen mode Exit fullscreen mode

*#to draw horiozontal bar

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.barh(x, y)
plt.show()
Enter fullscreen mode Exit fullscreen mode

*plt.bar() attribues

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
colors=np.array(["red","black","hotpink","yellow"])#we can add single color also customise color for every bar

plt.bar(x, y, color = colors)
plt.show()
Enter fullscreen mode Exit fullscreen mode

width = 0.1 bar takes width size like color we can make all bars same size or customise size like color

*A histogram:

x = np.random.normal(170, 10, 250)

plt.hist(x,color="hotpink")
plt.show()
Enter fullscreen mode Exit fullscreen mode

*pie chart and it's attributes

y = np.array([15, 10, 25, 50])

plt.pie(y)
plt.show()
Enter fullscreen mode Exit fullscreen mode

The label parameter must be an array with one label for each wedge

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Pull the "Apples" wedge 0.2 from the center of the pie:

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode = myexplode)
plt.show()
# Add a shadow to the pie chart by setting the shadows parameter to True:
plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)
plt.show()

Enter fullscreen mode Exit fullscreen mode

Specify a new color for each wedge:

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]

plt.pie(y, labels = mylabels, colors = mycolors)
plt.show() 
Enter fullscreen mode Exit fullscreen mode

To add a list of explanation for each wedge, use the legend() function:

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.legend()
# To add a header to the legend, add the title parameter to the legend function. 
plt.legend(title = "Four Fruits:")
plt.show() 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)