Summary of Matplotlib - Explained with examples
MATPLOTLIB tries to make easy things easy and hard things possible.There are thousands of libraries in Python, and Matplotlib is one of the most powerful tools for data visualization in Python.
Then, What is Data visualization ?
It is the presentation of data in pictorail or ghraphical format.
Matplotlib is a cross-platform library for making 2D plots from data in arrays. You can generate plots, graphs, histograms, power spectra, bar charts, errorcharts, scatterplots, etc., with just a few lines of code.
So, we will see overall Matplotlib summary with examples.
To install this package, in cmd enter: pip install matplotlib¶
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import style
Importing pyplot(to draw) and style(for styling).
You can also import like: import matplotlib.pyplot as plt.
%matplotlib inline : Displays output inline in Browser only.
style.use("ggplot")
ggplot is a Python implementation of the grammar of graphics.
x = [1,2,3]
y = [3,4,1]
(x,y) is our dataset , in which x is x-axis and y is y-axis
plt.title("Random data")
plt.plot(x,y, color="g", marker='o', label="data1")
plt.legend()
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
plot : means to plot a Line graph.
"g" : The "g" means green color of line. If not mentioned it gives color by default.
marker='o' : It gives the mark to intersection points.
label : means it gives a label to that particular line to identify.
legend : we have to call that explicitly then only we can use labels.
title: To give a title.
xlabel : gives lable to x-axis.
ylabel : gives lable to y-axis.
In plot we are passing (x,y) with a label "data1".
comparision between more than two dataset¶
x = [1,2,3] #our new data
y = [3,4,1]
x1 = [2,4,6]
y1 = [2,5,3]
(x,y) and (x1,y1) are our two dataset
plt.title("comparision of dataset")
plt.plot(x,y,"g", linewidth=3.0, label="data1")
plt.plot(x1,y1 , label="data2", ls = ':')
plt.legend()
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.axis([0, 6, 0, 6])
plt.show()
plot: means to plot a Line graph.
"g": The "g" means green color of line. If not mentioned it gives color by default.
linewidth: changes the line width of a graph
label : means it gives an label to that particular line to identify.
ls = ':' : Linestyle changes the style of the plotted line
legend : we have to call that explicitly then only we can use labels
title: To give a title
xlabel: gives lable to x-axis
ylabel: gives lable to y-axis
axis: gives the axis like starting and ending point:[xmin, xmax, ymin, ymax]
In first plot we are passing (x,y) with a label "data1"
and (x1,y1) with "data2".
Types of graph plotting in Matplotlib¶
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import style
style.use("ggplot")
x = [1,2,4,5]
y = [2,5,3,1]
Line graph¶
A line graph is a type of chart used to show information that changes over time.
We plot line graphs using several points connected by straight lines.
plt.plot(x,y,label="line-graph")
plt.legend()
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
bar graph¶
Bar graphs are the pictorial representation of data (generally grouped),
in the form of vertical or horizontal rectangular bars,
where the length of bars are proportional to the measure of data.
plt.bar(x,y,color="blue", label="bar-graph")
plt.legend()
plt.title("Bar graph")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
horizontal bar graph¶
It is also a bar graph just plotted horizontally.
plt.barh(x,y,color="blue", label="horizontal-Bar-graph")
plt.legend()
plt.title("Bar graph")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
scatter plot¶
A scatter plot is a chart type that is normally used to observe and
visually display the relationship between variables
plt.scatter(x,y,color="purple", label="Scatter-plot")
plt.legend()
plt.title("scatter plot")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
Histograms¶
It is similar to a Bar Chart, but a histogram groups numbers into ranges .
The height of each bar shows how many fall into each range.
bins means ranges. rwidht means to give spaces between the data.
new_data = [20,30,26,55,60,80,95,10]
plt.hist(new_data,color="pink", label="histogram",
bins=[0,30,60,100], rwidth=0.95)
plt.legend()
plt.title("Histogram")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
Pie chart¶
A Pie Chart can only display one series of data.
Pie charts show the size of items (called wedge) in one data series,
proportional to the sum of the items.
Pie chart, where the slices will be ordered and plotted counter-clockwise.
we have taken the data in variable x , labels name in mylabels , colors in mycolors variables.
explode : means to raise that particular portion or a slice. In this we have "explode" the 3rd slice (i.e. 'oranges').
autopct : enables to display the percent value
shadow : gives shadow to chart.
x = [1,2,4,5]
mylabels = ["Apples", "Bananas", "oranges", "grapes"]
mycolors = ["red", "yellow", "orange", "green"]
myexplode = (0, 0, 0.1, 0)
plt.pie(x, labels=mylabels, colors=mycolors,
explode=myexplode, shadow = True, autopct='%1.2f%%')
plt.title("Pie Chart")
plt.show()
Subplots in Matplotlib¶
Subplot means multiple plots.
The subplots() function in pyplot module of matplotlib library is used to create a
figure and a set of subplots.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,2,100) #random data set with numpy
y = np.linspace(1,5,100)
d1 = x ** 2
plt.subplot(221)
plt.title("graph-1")
plt.plot(x, d1, color='b')
plt.subplot(222)
plt.title("graph-2")
plt.plot(y, d1, color='r')
plt.show()
221 :
2 means we have two plots(vertically) ,
2 means we will plot 2 plot horizontally.
1 means this plot will be first. & if 2 then it will be second for (222)
subplot for vertically 2 graphs¶
plt.subplot(211)
plt.title("graph-1")
plt.plot(x, d1, color='b')
plt.subplot(212)
plt.title("graph-2")
plt.plot(y, d1, color='r')
plt.show()
plotted two graphs vertically i.e up and down not side by side