Custom line color
[Link](year, kvpasspercentage, color='orange')
Change the value in color [Link] ‘b’ for blue,’r’,’c’,…..
• Custom line style
[Link]( [1,1.1,1,1.1,1], linestyle='-' , linewidth=4). set linestyle to any of '-‘ for solid
line style, '--‘ for dashed, '-.‘ , ':‘ for dotted line
• Custom line width [Link]( 'x', 'y', data=df, linewidth=22) set linewidth as required
• Title
[Link]('JNV KV PASS % till 2018') – Change it as per requirement
Label -
[Link](‘Year') - change x or y label as per requirement
• Legend -
[Link](('jnv','kv'),loc='upper right‘,frameon=False)
import [Link] as plt
house=['A','B','C','D','E'] Line color combined with
mark=[78,67,90,34,67] marker type,in this case
line style compulsory
sportsscore=[89,90,120,100,99]
[Link](house,mark,'r+',linestyle='--',markeredgecolor='g')
[Link]('C:\\Users\\MACH-001\\Desktop\\[Link]')
[Link]()
import [Link] as plt
house=['A','B','C','D','E']
mark=[78,67,90,34,67]
sportsscore=[89,90,120,100,99]
[Link](house,mark,'r+') Otherwise it gives scatter chart
[Link]('C:\\Users\\MACH-001\\Desktop\\[Link]')
[Link]()
import [Link] as plt
x=[1,2,3,4]
y=[10,20,30,40]
[Link](x,y)
[Link]()
Setting Limits and Ticks
(i) Setting Xlimits and Ylimits
- Both xlim( ) and ylim( ) are used as per following format:
<[Link]>.xlim(<xmin> , <xmax>)
<[Link]>.ylim(<ymin> , <ymax>)
\import [Link] as plt
import numpy as np
x=[Link](4)
y=[5.,25.,45.,20.]
[Link](-2.0,4.0)
[Link](x,y)
[Link]("A Simple Bar Chart")
[Link]()
Output:
Note: While setting up the limits for axes, you must keep in mind that only the data that falls into the limits
of X and Y-axes will be plotted; rest of the data will not show in the plot.
import [Link] as plt
import numpy as np
x=[Link](4)
y=[5.,25.,45.,20]
[Link](-4.0,1.0)
[Link](x,y)
[Link]("A Simple Bar Chart")
[Link]()
Setting Ticks for Axes - To set own tick marks:
For X-axis , you can use xticks( ) function as per format: xticks(, [optional sequence containing tick labels])
For Y-axis , you can use yticks( ) function as per format: yticks(, [optional sequence containing tick labels])
[Link](), [Link]()
x=[1,2,3,4]
y=[10,20,30,40]
[Link](x,y)
[Link]([100,200,300,400])
[Link]()
x=[1,2,3,4]
y=[10,20,30,40]
[Link](x,y)
[Link]([8,7,9,10])
[Link]()
If Ticks values alone sent,x values
will be marked in bar
x=[1,2,3,4]
y=[10,20,30,40]
[Link](x,y)
[Link](x,[8,7,9,10])
[Link]()
When you pass x and ticks
arguments parallelly-Just it
replaced x points 1,2,3,4 as
ticks values 8,7,9,10
x=['a','b','c','d']
y=[10,20,30,40]
[Link](x,y)
[Link](x,[8,7,9,10])
[Link]()
X axis vales a,b,c,d
has been replaced
with8,7,9,10
Adding Legend
- When we plot multiple ranges on a single plot, it becomes necessary that legends are specified.
- Two step process:
(i) In the plotting functions like plot( ) , bar( ) etc., give a specific label to data range using
argument label.
(ii) Add legend to the plot using legend( ) as per format:
<[Link]>.legend(loc = <position number or string>)
The loc argument can either take values 1, 2, 3, 4 signifying the position strings ‘upper
right’,’upper left’,’lower left’,’lower right’ respectively.
Default position is ‘upper right’ or 1.
import [Link] as plt
import numpy as np
val=[[5.,25.,45.,20.],[4.,23.,49.,17.],[6.,22.,47.,19.]]
x=[Link](4)
#step1: specify label for each range being plotted using label
[Link](x+0.00,val[0],color='b',width=0.25,label='range1')
[Link](x+0.25,val[1],color='g',width=0.25,label='range2')
[Link](x+0.50,val[2],color='r',width=0.25,label='range3')
#step2:add legend,i.e.
[Link](loc='upper left')
[Link]("Multi Range Bar chart")
[Link]('X')
[Link]('Y')
[Link]()
To use legend call [Link](),label
should be mentioned in plot() or bar()
What is 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.
To create histogram in python hist() function is used. The syntax of hist() function is like this:
[Link](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 value is false.
hisstype: It is an option parameter. It can be any one of these:
1. bar: Bar type histogram, it arranges data side by side if given data is multiple. It is by default
histtype.
2. barstacked: When multiple data are stacked on top of each other
3. step: Generates a lineplot that is by default unfilled
4. stepfilled: Generates a lineplot that is by default filled
import [Link] as m
import numpy as np
x=[Link](100)
[Link](x)
[Link]()
import [Link] as plt
height = [189, 185, 195, 149, 189, 147, 154,
174, 169, 195, 159, 192, 155, 191,
153, 157, 140, 144, 172, 157, 181,
182, 166, 167]
[Link](height, edgecolor="red", bins=5)
[Link]()
import [Link] as plt
marks = [1, 2, 3, 2, 1, 2, 3, 2,
1, 4, 5, 4, 3, 2, 5, 4,
5, 4, 5, 3, 2, 1, 5]
[Link](marks, bins=[1, 2, 3, 4, 5], edgecolor="black")
[Link]()
import [Link] as plt
data = [189, 185, 195, 149, 189, 147,
154, 174, 169, 195, 159, 192,
155, 191, 153, 157, 140, 144,
172, 157, 181, 182, 166, 167]
[Link](data, bins=[140, 150, 160, 175, 185, 200],
edgecolor="yellow", color="grey")
[Link]()
import [Link] as plt
import numpy as np
x=[-10,-8,3,6,9,10,-10,2,1,-8,3,6,10]
[Link](-10,10)
[Link](x)
[Link]() #hist1
[Link](x,bins=50)
[Link]() #hist2
[Link](x,bins=100)
[Link]() #hist3
import [Link] as m
english=[77,66,88,99,55,44,33,79,68,83]
maths=[56,89,70,50,60,65,90,80,47,82]
[Link](english,histtype="step")
[Link]()
histtype="step"
import [Link] as m
english=[77,66,88,99,55,44,33,79,68,83]
maths=[56,89,70,50,60,65,90,80,47,82]
[Link](english,histtype="stepfilled")
[Link]()
import [Link] as m
english=[77,66,88,99,55,44,33,79,68,83]
maths=[56,89,70,50,60,65,90,80,47,82]
[Link](english,histtype="bar")
[Link]()
import [Link] as plt import numpy as
np x=[-10,-8,3,6,9,10,-10,2,1,-8,3,6,10]
y=[8,10,9,2,1,5,-10,-2,3,-8,7,9,10]
[Link](-10,10)
[Link]([x,y]) #plotting two histogram - plot1
[Link]()
import [Link] as m
english=[77,66,88,99,55,44,33,79,68,83]
maths=[56,89,70,50,60,65,90,80,47,82]
[Link]([english,maths],histtype="barstacked")
[Link]()
import [Link] as plt
values = [87, 53, 66, 61, 67, 68, 62,
In the above graph, the width of
110, 104, 61, 111, 123, 117, each bin is :
119, 116, 104, 92, 111, 90,
103, 81, 80, 101, 51, 79, 107, width = ( 145 – 51 ) / 7 = 13.4
110, 129, 145, 139, 110]
[Link](values, bins=7, edgecolor="yellow", color="green")
[Link]()
Changing the look of histogram
It can be provided with bins=n or directly the number next to x parameter. If you
want to apply border colors for the bars you can use edgecolor parameter. To
change the fill color of bars facecolor parameter is used. Observe the following
code:
import [Link] as m
import numpy as np
x=[Link](100)
[Link](x,20,edgecolor="blue",facecolor="r")
[Link]()
import [Link] as m
import numpy as np
x=[Link](100)
[Link](x,20,edgecolor="blue",orientation='horizontal',facecolor="r")
[Link]()
import [Link] as m
import numpy as np
x=[Link](100)
[Link](x,20,edgecolor="blue",orientation='horizontal',facecolor="r",hatch='O')
[Link]()
import [Link] as m
import numpy as np
x=[Link](100)
[Link](x,20,cumulative= True, edgecolor="blue",facecolor="r")
[Link]()
import [Link] as plt
import numpy as np
[Link](figsize=(10,7))
Info=['gold','silver','bronze','total']
Australia=[80,59,59,198]
England=[45,45,46,136]
India=[26,20,20,66]
Canada=[15,40,27,82]
X=[Link](len(Info))
[Link](Info,Australia,width=.15,label=”AUS” )
[Link](X+0.15,England,width=.15,label=”ENG” )
[Link](X+0.30,India,width=.15,,label=”ind” )
[Link](X+0.45,Canada,width=.15,label=’canada’ )
[Link]()
[Link]() In a multiple bar graph, it's
essential to specify the width of each bar
explicitly. For instance, the second bar should be positioned immediately
next to the first one on the x-axis. So Xaxis position should add with width of
the first bar to make sure to place the next to the first bar. To determine the
x-axis position for the third bar, you add the widths of the previous two bars
together. This process continues for subsequent bars, ensuring they are
correctly spaced along the x-axis
Creating Multiple Bars chart Say we want to plot ranges , A = [2 , 4 , 6 , 8] and B =[2.8 , 3.5 , 6.5 , 7.7]
1. Deciding X points and thickness. Say, we want the thickness of each bar as 0.35, then for the
first range, X point will be X and for the second range, the X will shift by first bar’s thickness,i.e. X+0.35.
2. Deciding [Link] we want red color for the first range and blue color for the second range.
3. The width argument will take value as 0.35 in this case.
4. Plot using multiple bar( ) functions.
import [Link] as plt
import numpy as np
A=[2,4,6,8]
B=[2.8,3.5,6.5,7.7]
X=[Link](len(A))
[Link](X,A,color="red",width=0.35)
[Link](X+0.35,B,color="blue",width=0.35)
[Link]()
Multiple bar graph rules
In a multiple bar graph, each bar should have its width explicitly defined. Here’s how bars are typically positioned on the x-axis:
1. Specify Bar Width: Define the width of each bar. This ensures consistency in the visual representation of data.
2. Positioning Bars:
○ The second bar is placed immediately next to the first one on the x-axis. You don’t leave any space between them.
○ For the third bar, you position it by adding the widths of the previous two bars together. This cumulative width determines
where the third bar starts on the x-axis.
3. Continuing the Process:
○ This process continues for each subsequent bar. Each new bar's x-axis position is determined by summing up the widths
of all previous bars.
This method ensures that bars in the multiple bar graph are correctly spaced along the x-axis according to their specified widths, making
the graph visually clear and accurate for data comparison.
Saving the histogram as image
You can use savefig() method to save the histogram.
Consider above example and add this line into the program.
[Link](“[Link]”)
Saving formats:
png
jpg
pdf
svg
eps
import [Link] as m
english=[77,66,88,99,55,44,33,79,6
8,83]
[Link](english,histtype="stepfilled")
[Link]()
# Implementation of [Link]()
# function
import numpy as np
import [Link] as plt
# values of x and y axes
valx = [30, 35, 50, 5, 10, 40, 45, 15, 20, 25]
valy = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]
[Link](valx, valy)
[Link]('X-axis')
[Link]('Y-axis')
[Link]([Link](0, 60, 5))
[Link]([Link](0, 15, 1)) yticks
[Link]()
Specifically dataframe’s two
columns passing inside plot- here
first column (month)taken as x
axis,second column (monthly
saving) taken as y axis
Csv file
Converting as dataframe
import pandas as pd
import [Link] as plt
df=pd.read_csv(“[Link]”)
[Link](df) Converting df to graph
[Link]() [Link](df)
# Python Program to illustrate Linear Plotting
import [Link] as plt
year = [1972, 1982, 1992, 2002, 2012]
e_india = [100.6, 158.61, 305.54,
394.96, 724.79]
e_bangladesh = [10.5, 25.21, 58.65,
119.27, 274.87]
# formatting of line style and
# plotting of co-ordinates
[Link](year, e_india, color ='orange',marker ='o', markersize = 12, label ='India')
[Link](year, e_bangladesh, color ='g',linestyle ='dashed', linewidth = 2,label ='Bangladesh')
[Link]('Years')
[Link]('Power consumption in kWh')
[Link]('Electricity consumption per \
capita of India and Bangladesh')
[Link]()
[Link]()
A histogram is a graphical representation which organizes a group of data
points into user-specified ranges. Histogram provides a visual interpretation of
numerical data by showing the number of data points that fall within a specified
range of values (“bins”). It is similar to a vertical bar graph but without gaps
between the bars
Dataframe to plot() graph
import pandas as pd
# Creating a dataframe from a dictionary
data = {
'code': [10, 20, 30, 40],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link](df)
[Link]()
output
code Age City
0 10 25 1
1 20 30 2
2 30 35 3
3 40 40 4
import pandas as pd
import [Link] as plt
# Creating a dataframe from a
dictionary
data = {
'code': ['a','b','c','d'],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link](df)
[Link]()
code Age City
0 a 25 1
1 b 30 2
2 c 35 3
3 d 40 4
import pandas as pd
import [Link] as plt
# Creating a dataframe from a
dictionary
data = {
'code': [10, 20, 30, 40],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link](kind='bar')
[Link]()
code Age City
0 10 25 1
1 20 30 2
2 30 35 3
3 40 40 4
import pandas as pd
import [Link] as plt
# Creating a dataframe from a
dictionary
data = {
'code': [10, 20, 30, 40],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link](kind='hist')
[Link]()
code Age City
0 10 25 1
1 20 30 2
2 30 35 3
3 40 40 4
import pandas as pd
import [Link] as plt
# Creating a dataframe from a
dictionary
data = {
'code': [10, 20, 30, 40],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link]([Link],[Link])
[Link]()
code Age City
0 10 25 1
1 20 30 2
2 30 35 3
3 40 40 4
import pandas as pd
import [Link] as plt
# Creating a dataframe from a
dictionary
data = {
'code': [10, 20, 30, 40],
'Age': [25, 30, 35, 40],
'City': [1, 2, 3, 4]
}
df = [Link](data)
print(df)
[Link]([Link],[Link])
[Link]()
code Age City
0 10 25 1
1 20 30 2
2 30 35 3
3 40 40 4
Customization of Histogram – By default bars of histogram is displayed in blue color but
we can change it to other color with following code .
[Link]([1,11,21,31,41, 51], bins=[0,10,20,30,40,50, 60], weights=[10,1,0,33,6,8],
facecolor='y', edgecolor="red") In above code we are passing ‘y’ as facecolor means
yellow color to be displayed in bars.
To give a name to the histogram write below code before calling show()
[Link]("Histogram Heading")
Edge color and bar color can be set using following parameter in hist() method
edgecolor='#E6E6E6',color='#EE6666 .color value can be rgb in hexadecimal form For
x and y label below code can be written [Link]('Value') [Link]('Frequency')
Csv to dataframe
Save this excel as csv file
Output: (csv has been converted into
dataframe,row index generated automatically
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]")
print(x)
[Link](x)
[Link]()
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]")
print(x)
[Link]()
[Link]()
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]")
print(x)
[Link](kind='bar')
[Link]()
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]")
print(x)
[Link](kind='hist')
[Link]()
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\ne
[Link]",names=['first','second','third'])
print(x)
[Link](kind='bar')
[Link]()
first second third
0 1999 2000 2001
1 98 89 78
2 45 78 56
3 23 44 88
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]",names=['first','second','third'],nrows=2)
print(x)
first second third
0 1999 2000 2001
1 98 89 78
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]",names=['first','second','third'],header=0)
print(x)
first second third
0 98 89 78
1 45 78 56
2 23 44 88
>>>
import pandas as pd
import [Link] as plt
x=pd.read_csv("C:\\Users\\lap2-120\\Desktop\\[Link]",header=None)
print(x)
0 1 2
0 1999 2000 2001
1 98 89 78
2 45 78 56
3 23 44 88