Special Plot Types
Matplotlib allows you to create different kinds
of plots ranging from histograms and scatter plots to bar graphs and bar
charts.
A Line Chart or Line Graph is a type of chart
which displays information as a series of data points called ‘markers’
connected by straight line segments. A Line Chart is often used to visualize a
trend in data over intervals of time – a time series – thus the line is often
drawn chronologically.
Example: Line plot
import matplotlib.pyplot as plt
years = [2014, 2015, 2016, 2017, 2018]
total_populations = [8939007, 8954518, 8960387,
8956741, 8943721]
plt.plot (years, total_populations)
plt.title ("Year vs Population in
India")
plt.xlabel ("Year")
plt.ylabel ("Total Population")
plt.show()
In this program,
Plt.title() → specifies title to the graph
Plt.xlabel() → specifies label for X-axis
Plt.ylabel() → specifies label for Y-axis
Output
A BarPlot (or BarChart) is one of the most
common type of plot. It shows the relationship between a numerical variable and
a categorical variable.
Bar chart represents categorical data with
rectangular bars. Each bar has a height corresponds to the value it represents.
The bars can be plotted vertically or horizontally. It’s useful when we want to
compare a given numeric value on different categories. To make a bar chart with
Matplotlib, we can use the plt.bar() function.
Example
import matplotlib.pyplot as plt
# Our data
labels = ["TAMIL",
"ENGLISH", "MATHS", "PHYSICS",
"CHEMISTRY", "CS"] usage = [79.8, 67.3, 77.8, 68.4, 70.2,
88.5]
#Generating the y positions.
Later, we'll use them to replace them with labels. y_positions = range (len(labels))
#Creating our bar plot
plt.bar (y_positions, usage)
plt.xticks (y_positions, labels)
plt.ylabel ("RANGE")
plt.title ("MARKS")
plt.show()
Output
The above code represents the following:
Labels → Specifies labels for the bars.
Usgae → Assign values to the labels specified.
Xticks → Display the tick marks along the
x-axis at the values represented. Then specify the label for each tick mark.
Range → Create sequence of numbers.
The differences between Histogram and bar graph
are as follows
1. Histogram refers to a graphical
representation; that displays data by way of bars to show the frequency of
numerical data. A bar graph is a pictorial representation of data that uses
bars to compare different categories of data.
2. A histogram represents the frequency
distribution of continuous variables. Conversely, a bar graph is a diagrammatic
comparison of discrete variables.
3. Histogram presents numerical data whereas
bar graph shows categorical data.
4. The histogram is drawn in such a way that
there is no gap between the bars. On the ot her hand, there is proper spacing
between bars in a bar graph that indicates discontinuity.
5. Items of the histogram are numbers, which
are categorised together, to represent ranges of data. As opposed to the bar
graph, items are considered as individual entities.
6. In the case of a bar graph, it is quite
common to rearrange the blocks, from highest to lowest. But with histogram,
this cannot be done, as they are shown in the sequence of classes.
7. The width of rectangular blocks in a histogram may or may not be same while the width of the bars in a bar graph is always same.
Pie Chart is probably one of the most common
type of chart. It is a circular graphic which is divided into slices to
illustrate numerical proportion. The point of a pie chart is to show the
relationship of parts out of a whole.
To make a Pie Chart with Matplotlib, we can use
the plt.pie()
function. The autopct parameter allows us to display the percentage value using
the Python string formatting.
Example
import matplotlib.pyplot as plt
sizes = [89, 80, 90, 100, 75]
labels = ["Tamil",
"English", "Maths", "Science",
"Social"]
plt.pie (sizes, labels = labels, autopct =
"%.2f ")
plt.axes().set_aspect ("equal")
plt.show()
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.