0% found this document useful (0 votes)
214 views

DATA VISUALIZATION - Part 2

This document discusses using Python Pandas and Matplotlib to create line plots from data. It provides 5 examples of creating line plots with different features: 1) A simple single line plot; 2) A plot with two lines and labels; 3) Adding color; 4) Customizing line styles, widths, labels, and legend; 5) Creating two subplots to show two plots separately. It also discusses options for formatting labels, legends, and adjusting subplot spacing.

Uploaded by

Adithya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

DATA VISUALIZATION - Part 2

This document discusses using Python Pandas and Matplotlib to create line plots from data. It provides 5 examples of creating line plots with different features: 1) A simple single line plot; 2) A plot with two lines and labels; 3) Adding color; 4) Customizing line styles, widths, labels, and legend; 5) Creating two subplots to show two plots separately. It also discusses options for formatting labels, legends, and adjusting subplot spacing.

Uploaded by

Adithya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DATA VISUALIZATION USING

PYTHON PANDAS
INFORMATICS PRACTICES
CLASS XII
LINE PLOT example1:
Question: Draw a line graph where the x axis shows the individual classes and the y
axis shows the number of students participating in an interhouse event.

import matplotlib.pyplot as plt


x=[4,5,6,7]
y=[6,10,14,13]
plt.plot(x,y)
plt.title(" MY first Line chart")
plt.xlabel("CLASS")
plt.ylabel("NO of students participating")
plt.show()
LINE PLOT example2:
Question: Draw two line graph where the x axis shows the individual classes and the y axis
shows the number of students participating in ART/COMPUTER interhouse event.
import matplotlib.pyplot as plt
x=[4,5,6,7]
y=[6,10,14,13]
z=[10,12,18,20]
plt.plot(x,y,label="Students participating in ART competition")
plt.plot(x,z,label="Students participating in COMPUTER
competition")
plt.legend()
plt.title(" MY first Line chart")
plt.xlabel("CLASS")
plt.ylabel("NO of students participating")
plt.grid(True)
plt.show()
CHANGING COLOURS OF THE LINES:

Table of the abbreviations used to select colors:

Colour abbreviation Colour name


b blue
c cyan
g green
k black
m magenta
r red
w white
y yellow
LINE PLOT Example3:
Question: Draw two line graph where the x axis shows the individual classes and the y axis
shows the number of students participating in ART/COMPUTER interhouse event. Use
different colours for the plots.
import matplotlib.pyplot as plt
x=[4,5,6,7]
y=[6,10,14,13]
z=[10,12,18,20]
plt.plot(x,y,'g',label="Students participating in ART competition")
plt.plot(x,z,'m',label="Students participating in COMPUTER
competition")
plt.legend()
plt.title(" MY first Line chart")
plt.xlabel("CLASS")
plt.ylabel("NO of students participating")
plt.grid(True)
plt.show()
Some MORE VARIATIONS which we can
add with the following functions:
a) plot()
• linestyle- set linestyle to any of '-‘ for solid line style, '--‘ for dashed, '-.‘ ,
':‘ for dotted line
• linewidth- sets the width of the line
• color- sets the color of the line
b) xlabel(), ylabel():
• fontsize: can specify the size of the font for the labels
• color: can specify the color of the font used to display the x and y
labels.
Some MORE VARIATIONS which we can
add with the following functions:
c) legend()
• loc- specify the location of the legend as “upper left” or “upper
right”, “lower left”,”lower right”
• To give different names other than the labels mentioned in the
plot(). (“new names s eparated by commas in their own half
circle bracket”)
• Ex: plt.legend((‘Art’,’Comp’))
LINE PLOT Example4:
Question: Draw two line graph where the x axis shows the individual classes and the y axis
shows the number of students participating in ART/COMPUTER interhouse event. Use
different colors for the x and y labels. Specify the position and text for the legend. Also change
the style and width of the lines.
import matplotlib.pyplot as plt
x=[4,5,6,7]
y=[6,10,14,13]
z=[10,12,18,20]
plt.plot(x,y,'g',linestyle="--", linewidth=10)
plt.plot(x,z,'m',linestyle="dotted” )
plt.legend(('ART','COMP’), loc='upper left')
plt.title(" MY first Line chart")
plt.xlabel("CLASS", fontsize="14",color="red")
plt.ylabel("NO OF STUDENTS PARTICIPATING", fontsize="14",
color="blue")
plt.grid(True)
plt.show()
Creating multiple views in the same chart
Using subplot(): allows u to draw two or more graphs on the same area.
Ex: subplot(2,1,1)
2 represents number of graphs
The Last 1 represent the first one out of two
WE can adjust the horizontal and vertical space in corresponding to the 2
plots
subplots_adjust ( hspace=0.4, wspace=0.4 )
• hspace -> Horizontal space between 2 plots
• vspace --. Vertical space
LINE PLOT Example5:
Question: Draw two line graph where the x axis shows the individual classes and
the y axis shows the number of students participating in ART/COMPUTER
interhouse event, in two different plots
• import matplotlib.pyplot as plt
x=[4,5,6,7]
y=[6,10,14,13]
z=[10,12,18,20]
plt.subplot(2,1,1)
plt.plot(x,y,'g',linestyle="--", linewidth=5)
plt.title(" Students participating for ART")
plt.xlabel("CLASS", fontsize="8",color="red")
plt.ylabel("NO OF STUDENTS PARTICIPATING", fontsize="8", color="blue")
plt.grid(True)
plt.show()

plt.subplot(2,1,2)
plt.plot(x,z,'m',linestyle="dotted")
plt.title(" Students participating for COMPUTERS")
plt.xlabel("CLASS", fontsize="8",color="red")
plt.ylabel("NO OF STUDENTS PARTICIPATING", fontsize="8", color="blue")
plt.grid(True)
plt.show()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy