MATPLOTLIB


 

Data visualisation means graphical or pictorial representation of the data using graph, chart, etc. The purpose of plotting data is to visualise variation or show relationships between variables.


Visualisation of data is effectively used in fields like health, finance, science, mathematics, engineering, etc. 


The pyplot module of matplotlib contains a collection of functions that can be used to work on a plot.

 The plot() function of the pyplot module is used to create a figure.

 A figure contains a plotting area, legend, axis labels, ticks, title, etc. 


Program 1

import matplotlib.pyplot as plt

 #list storing date in string format 

date=["25/12","26/12","27/12"] 

#list storing temperature values 

temp=[8.5,10.5,6.8]

 #create a figure plotting temp versus date

 plt.plot(date, temp)

plt.show()




Customisation of Plots Pyplot library gives us numerous functions, which can be used to customise charts such as adding titles or legends.


Program -2 

Plotting a line chart of date versus temperature by adding Label on X and Y axis, and adding a Title and Grids to the chart.

 import matplotlib.pyplot as plt 

date=["25/12","26/12","27/12"] 

temp=[8.5,10.5,6.8] 

plt.plot(date, temp) 

plt.xlabel("Date")         #add the Label on x-axis 

plt.ylabel("Temperature")                     #add the Label on y-axis 

plt.title("Date wise Temperature")     #add the title to the chart 

plt.grid(True) #add gridlines to the background 

plt.yticks(temp) 

 plt.show()


Marker 

 It is also possible to specify each point in the line through a marker.

A marker is any symbol that represents a data value in a line chart or a scatter plot. 



Colour 

It is also possible to format the plot further by changing the colour of the plotted data.We can either use character codes or the color names as values to the parameter color in the plot().

Character and Colour 

‘b’ blue

 ‘g’ green

 ‘r’ red

 ‘c’ cyan 

‘m’ magenta 

‘y’ yellow 

‘k’ black

 ‘w’ white




 Linewidth and Line Style 

The linewidth and linestyle property can be used to change the width and the style of the line chart. Linewidth is specified in pixels. 

The default line width is 1 pixel showing a thin line.

A number greater than 1 will output a thicker line depending on the value provided.

We can also set the line style of a line chart using the linestyle parameter. 

It can take a string such as "solid", "dotted", "dashed" or "dashdot"



Program 3

 Consider the average heights and weights of persons aged 8 to 16 stored in the following two lists: height = [121.9,124.5,129.5,134.6,139.7,147.3, 152.4, 157.5,162.6] 

weight= [19.7,21.3,23.5,25.9,28.5,32.1,35.7,39.6, 43.2] 

Let us plot a line chart where:

 i. x axis will represent weight 

ii. y axis will represent height 

iii. x axis label should be “Weight in kg”

 iv. y axis label should be “Height in cm”

 v. colour of the line should be green

 vi. use * as marker 

vii. Marker size as10

 viii. The title of the chart should be “Average weight with respect to average height”.

 ix. Line style should be dashed 

x. Linewidth should be 2



code:-

import matplotlib.pyplot as plt

 import pandas as pd 

height=[121.9,124.5,129.5,134.6,139.7,147.3,152.4,157.5,162.6] 

weight=[19.7,21.3,23.5,25.9,28.5,32.1,35.7,39.6,43.2] df=pd.DataFrame({"height":height,"weight":weight}) #Set xlabel for the plot

 plt.xlabel('Weight in kg') #Set ylabel for the plot

plt.ylabel('Height in cm') #Set chart title: 

plt.title('Average weight with respect to average height') #plot using marker'-*' and line colour as green plt.plot(df.weight,df.height,marker='*',markersize=10,color='green ',linewidth=2, linestyle='dashdot') plt.show()









Post a Comment

0 Comments