Data Visualisation: - Data visualisation basically refers to the graphical or visual representation of information and data using visual elements like charts, graphs and maps etc.
Using Pyplot of MATPLOT LIBRARY:- Pyplot is a collection of methods within MATPLOT library of Python which allows user to construct 2D plots (bar, line histogram pie chart) easily and interactively.
firstly we need to install Matplotlib liberary to python console. To INSTALLING AND IMPORTING MATPLOT LIBRARY We needs one DOS command
At first you have to open Command Prompt Then write this code – pip install matplotlib
After doing this process, Now you have to import MATPLOT library. During this process you need active internet connection to your PC
As stated earlier make sure to import matplotlib.pyplot library interface and other required libraries before you start using any of their functions.
Let us know about various parts of a Chart:-
- Figure: - Pyplot by default plots every chart into an area called Figure. A figure contains other elements of the plot in it.
- Axes: - The axes define the area (mostly rectangular in shape for simple plots) on which actual plot (line or bar or graph etc.) will appear. Axes have properties like label, limits and tick marks on them.
- There are two axes in a plot: (i) X-axis, the horizontal axis, (ii) Y-axis, the vertical axis.
- Axis label: It defines the name for an axis. It is individually defined tor X-axis and Y-axis each.
- Limits: These define the range of values and number of values marked on X-axis and Y-axis.
- Tick_Marks. The tick marks are individual points marked on the X-axis or Y-axis.
- Title: - This is the text that appears on the top of the plot. It defines what the chart is about.
- Legends: - These are the different colors that identify different sets of data plotted on the plot. The legends are shown in a corner of the plot.
Line Chart: - A line chart or line graph is a type of chart which displays information as a series of data points called 'marker' connected by straight line segments. The PyPlot interface offers plot() function for creating a line graph.
import matplotlib.pyplot as plt
a = [1,2,3,4,5,6]
b = [2, 4, 6,4,8,9]
plt.plot(b, a)
plt.show() # this very important function to show graph on screen.
Changing Line Style, Line Width and Line Color in a Line chart:- To change line Color: - You can specify the color code next to data being plotting in plot () function as shown below:
Syntax: < matplotlib.pyplot >.plot (<data1>, <data2>, <color code>,linewidth =<width>,linestyle = ['solid' | 'dashed', 'dashdot', 'dotted'] ,marker = <valid marker type >, marker size =<in points>, markercolor =<valid color>)
if you do not specify the linestyle separately along with linecolor-and-markerstyle-combination-string (eg, 'rd'), Python will only the markers and not the line. To get the line, specify linestyle argument.
"rd" red colors markers but no line between them.
Some different Colors codes: -
Character Color
'r' red
'b' blue
'g' green
'm' magenta
'y' yellow
'k' black
'c' cyan
'w' White
In addition, you can also specify Colors in many other ways, including full Color names ('red', 'green' ,etc.), hex string ('#008000')
Bar Graph
A bar graph / chart is a graphical display of data using bars of different heights. A bar chart can be drawn vertically or horizontally using bars of different heights/widths Pyplot offers bar () function to create a bar chart.
import matplotlib.pyplot as pl
a = [1, 2, 3]
b = [2, 4, 6]
pl.bar (a, b)
pl.show( )
Syntax <matplotlib.pyplot>.bar (<x-sequence>, <y-sequence>, width=<float value>| <width values sequence>,color = <color code/ name>| <color name/ codes sequence>)
To create a horizontal bar chart, you need use barh() function (bar horizontal), in place of bar().The label that you gave to x-axis in an bar() will become y-axis label in barh() and vice versa.
For example:
import matplotlib.pyplot as pl
a = [10, 20, 30]
b = [29, 46, 65]
pl.barh (a, b, color = 'r', height=5)
pl.show( )
Histogram A histogram is quite similar to vertical bar graph with no space in between vertical bars. When you have data which has data points fall between a particular range, you can use histogram to visualize this data. It is helpful to display statistical data or data inserted in measurable quantities. For ex. Marks, scores, units etc.
matplotlib.pyplot.hist(x, bins=value,cumulative=bool_val, histtype=type, align=alignment, orientation=orientation) where,
x: It is list to be plotted on histogram
bins: bins can be integer number computed with + 1 or generated by default.
cumulative: It is a boolean value i.e. either True or False. If provided True then bins are calculated where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives total number of data points. The default values is false.
hisstype: It is an option parameter. It can be any one of these:
- bar: Bar type histogram, it arranges data side by side if given data is multiple. It is by default histtype.
- barstacked: When multiple data are stacked on top of each other
- step: Generates a lineplot that is by default unfilled
- stepfilled: Generates a lineplot that is by default filled
orientation: It can be used for bar – type histogram
Some important Data visualization code
0 Comments