[go: up one dir, main page]

0% found this document useful (0 votes)
130 views23 pages

""" Import Matplotlib - Pyplot As PL

The document contains code snippets demonstrating various plotting techniques using Matplotlib library in Python. These include line plots, bar plots, histograms, customizing plots by changing colors, labels, legends etc. Multiple code examples are provided to plot single and multiple data sets, customize appearances, set labels and titles. Techniques like plt.plot(), plt.bar(), plt.hist() are used with different parameters to create different chart types.

Uploaded by

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

""" Import Matplotlib - Pyplot As PL

The document contains code snippets demonstrating various plotting techniques using Matplotlib library in Python. These include line plots, bar plots, histograms, customizing plots by changing colors, labels, legends etc. Multiple code examples are provided to plot single and multiple data sets, customize appearances, set labels and titles. Techniques like plt.plot(), plt.bar(), plt.hist() are used with different parameters to create different chart types.

Uploaded by

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

"""

import matplotlib.pyplot as pl

a=[1,2,3,4]
b=[2,4,6,8]
c=[1,4,9,16]

pl.xlabel("Some Values")
pl.ylabel("Doubled Values")
pl.title("A Simple Line chart")
pl.plot(a,b,color='y')

pl.show()

color ( line/marker)
marker type
marker size
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [2,4,1,5,2,6]
plt.plot(x, y, color='green', linestyle='dashed', linewidth =
3,marker='X', markeredgecolor='blue', markersize=12)
#setting x and y axis range
plt.ylim(1,8)
plt.xlim(1,8)
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Some cool customizations!')
plt.show()

#color-line,marker
#marker type
#marker size

import matplotlib.pyplot as plt


import numpy as np

x=np.arange(0.,10,0.1)
a=np.cos(x)
b=np.sin(x)
plt.plot(x,a,'b')
plt.plot(x,b,'g')
plt.show()

#Changing Marker Type, Size and color


# marker=<valid marker type>, markersize=<in points>,
markeredgecolor=<valid color>

import matplotlib.pyplot as plt

p=[1,2,3,4]
q=[2,4,6,8]
#plt.plot(p,q,'r+',linestyle='solid')

plt.plot(p,q,'r+',linestyle='solid',markeredgecolor='b')
plt.show()

"""
#To plot an algebraic expression 10x+14 using chart

import matplotlib.pyplot as plt


import numpy as np

x=np.arange(12,20)
y=10*x+14
plt.title("Graph for an algebraic expression")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.plot(x,y,color='m')
plt.show()

"""
import matplotlib.pyplot as plt
import numpy as np
y=np.arange(1,3)
plt.plot(y,'--',y+1,'-.',y+2,':')
plt.show()

plt.plot(y,color='y')
plt.plot(y+1,'m')
plt.plot(y+2,'c')
plt.show()

#example 3.1
import numpy as np
import matplotlib.pyplot as plt

a=np.arange(1,20,1.25)

b=np.log(a)

plt.plot(a,b)

plt.xlabel("Random values")
plt.ylabel("Logarithmic Values")

plt.show()

#Bar Graphs plt.bar(x,y)

import matplotlib.pyplot as plt


a,b,c=[1,2,3,4],[2,4,6,8],[1,4,9,16]

plt.bar(a,b)

plt.xlabel("Values")
plt.ylabel("Doubles")

plt.title("Bar Graph")
plt.show()

plt.bar(a,c)
plt.xlabel('Values')
plt.ylabel('Squares')

plt.title("Squares of Numbers")
plt.show()
import matplotlib.pyplot as plt
Cities=['Delhi','Mumbai','Bangalore','Hyderabad']

Population=[23456123,20083104,18456123,13411093]

plt.bar(Cities,Population,width=[0.25,0.5,0.6,0.9],color=['red','b
','g','black'])
plt.xlabel('Cities')
plt.ylabel('Population')
plt.show()

#Example 11
import matplotlib.pyplot as plt

Info=['Gold','Silver','Bronze','Total']
Australia=[80,59,59,198]

plt.barh(Info,Australia,color='orange')

plt.xlabel("Medals Type")
plt.ylabel("Australia Medal Count")
plt.show()

import matplotlib.pyplot as plt


Info=['Gold','Silver','Bronze','Total']
Australia=[80,59,59,198]
India=[26,20,20,66]
plt.bar(Info,Australia,color='r')
plt.bar(Info,India,color='g')
plt.xlabel("Medals Type")
plt.ylabel("Australia and India Medal Count")
plt.show()
#Changing widths of the Bars in a Bar Chart
# plt.bar(<x-sequence>,<y-sequence>,width=<width values
sequence>)

import matplotlib.pyplot as plt


Info=['Gold','Silver','Bronze','Total']
Australia=[80,59,59,198]
plt.bar(Info,Australia,width=[0.7,0.5,0.3,1])

plt.xlabel("Medals Type")
plt.ylabel("Australia Medal Count")
plt.show()

import matplotlib.pyplot as plt


Cities=['Delhi','Mumbai','Bangalore','Hyderabad']
Population=[23456123,20083104,18456123,13411093]
plt.bar(Cities,Population,width=[0.5,0.6,0.7,0.8])
plt.xlabel('Cities')
plt.ylabel('Population')
plt.show()

#Changing Colors of the Bars in a Bar Chart


#plt.bar(<x-sequence>,<y-sequence>,color=<color
code/name>)

import matplotlib.pyplot as plt


Cities=['Delhi','Mumbai','Bangalore','Hyderabad']
Population=[23456123,20083104,18456123,13411093]
plt.bar(Cities,Population,color=['red','b','g','black'])
plt.xlabel('Cities')
plt.ylabel('Population')
plt.show()

import matplotlib.pyplot as plt


Info=['Gold','Silver','Bronze','Total']
Australia=[80,59,59,198]
India=[26,20,20,66]
plt.bar(Info,India,color=['gold','yellow','brown','black'])
plt.bar(Info,Australia,color=['r','m','g','b'])
plt.xlabel("Medals Type")
plt.ylabel("Australia, India Medal Count")
plt.show()

#Adding legend to bar chart


#<matplotlib.pyplot>.legend(loc=<position number or string>)
#1,2,3,,4 'upper right','upper left','lower left','lower right'

#Example 3.2 page 125

import numpy as np
import matplotlib.pyplot as plt

Val=[[5.,25.,45.,20.],[4.,23.,49.,17.],[6.,22.,47.,19.]]
# Val[0] Val[1] Val[2]
X=np.arange(4)
#X=[0,1,2,3]

#Step1 : specify label for each range beoing plotted using label
argument
plt.bar(X+0.00,Val[0],color='b',width=0.25,label='range1')
plt.bar(X+0.25,Val[1],color='g',width=0.25,label='range2')
plt.bar(X+0.5,Val[2],color='r',width=0.25,label='range3')

#Step2 : add legend


plt.legend(loc=3)

plt.title("Multirange Bar Chart")


plt.xlabel('X')
plt.ylabel('Y')
plt.show()

# Settind X and Y labels,limits and Ticks


#Setting Xlimits and Ylimits
#Functions : xlim() and ylim()

#import numpy as np
import matplotlib.pyplot as plt
X=[0,1,2,3]
#X=np.arange(4)
Y=[5.,25.,45.,20.]
plt.xlim(-2.0,4.0)
plt.ylim(0,60)
plt.xlabel(" X - axis")
plt.ylabel(" Y - axis")
plt.bar(X,Y)
plt.title("A Simple Bar Chart")
plt.show()

#Setting Ticks for Axes

#function xticks() and yticks()


import numpy as np
import matplotlib.pyplot as plt
q=range(4)
s=[23.5,25,26,28.5]
plt.xticks([1,2,3,4])
plt.yticks([4,6,8,10])
plt.bar(q,s,width=0.25)
plt.show()

#plotting a Horizontal Bar Chart function barh()

import matplotlib.pyplot as plt


height=[5.1,5.5,6.0,5.0,6.3]
Names=['Asmi','Bela','Chris','Diya','Saqib']
plt.barh(Names,height)
plt.xlabel("Height")
plt.ylabel("Names")
plt.show()
#Example 3.3. page 130

Amount A to F =[8000,12000,9800,11200,15500,7300]
(a) Create a bar chart showing collection amount
(b) Plot the collected amount vs days using a bar chart
(c) Plot the collected amount vs sections using a bar chart
"""

import matplotlib.pyplot as plt


import numpy as np
amt=[8000,12000,9800,11200,15500,7300]
days=np.arange(6)
plt.title(" Weekly Amount collected")
plt.bar(days,amt,color=['r','b','g','m','k','y'],width=0.25)
plt.xticks(days,['A','B','C','D','E','F'])
plt.xlabel("SECTIONS")
plt.ylabel("COLLECTION")
plt.show()

"""

#QExample 26 page 206 Create multiple line charts on common


plot where three data ranges are plotted on the same chart.
They are:
# Data=[[5.,25,,45.,20.],[8.,13.,29.,27.],[9.,29.,27.,39.]]

import matplotlib.pyplot as plt


import numpy as np
Data=[[5.,25,45.,20.],[8.,13.,29.,27.],[9.,29.,27.,39.]]
X=np.arange(4)

plt.plot(X,Data[0],color='m',label='range1')
plt.plot(X,Data[1],color='b',label='range2')
plt.plot(X,Data[2],color='g',label='range3')
plt.legend(loc='upper right')
plt.title("Multirange Line Chart")
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

plt.savefig("multibar.pdf")

#Histogram using hist() function

Parameters of hist()
x , bins , cumulative : bool, histtype - ('bar','barstacked',
'step','stepfilled' ),orientation - ('horizontal', 'vertical' )

#plt.hist(x,bins=None,cumulative=False,histtype='bar','barstack
ed','step','stepfilled',align='mid',orientation='vertical'
'horizontal')
#Example 27 page 212
# A survey gathers height and weight of 100 participants and
recorded the participants ages a:
import matplotlib.pyplot as pl
import numpy as np
x=[1,1,2,3,3,5,7,8,9,10,
10,11,11,13,13,15,16,17,18,18,
18,19,20,21,21,23,24,24,25,25,
25,25,26,26,26,27,27,27,27,27,
29,30,30,31,33,34,34,34,35,36,
36,37,37,38,38,39,40,41,41,42,
43,44,45,45,46,47,48,48,49,50,
51,52,53,54,55,55,56,57,58,60,
61,63,64,65,66,68,70,71,72,74,
75,77,81,83,84,87,89,90,90,91]

#x=np.array([-0.04773042,-0.54508323,0.85572,0.44027,-
0.26309,-
0.877323,0.33,0.22,0.47,0.65,0.77,0.99,0.24,0.66,0.99,0.77,0.1
0,0.55])

pl.hist(x,bins=20,color='m',histtype='barstacked')
pl.xlabel("Ages")
pl.ylabel("Frequency")
pl.title("Histogram of Ages")
pl.show()

import matplotlib.pyplot as plt


#frequencies
ages=[2,5,70,40,30,45,50,45,43,40,44,60,7,13,57,18,90,77,32,2
1,20,40]

#setting ranges and no of intervals


range=(0,100)
bins=10
#plotting a histogram

plt.hist(ages,bins,range,color='green',histtype='bar',rwidth=0.8)
plt.xlabel('age')
plt.ylabel('No of people')
plt.title('My histogram')
plt.show()
# create a histogram for the following test scores :
[99,97,94,88,84,81,80,77,71,25]

import matplotlib.pyplot as plt


scores=[99,97,94,88.84,81,80,77,71,25]
bins=15
plt.hist(scores,bins,color='red',histtype='bar')
plt.show()

#x=np.array([-0.04773042,-0.54508323,0.85572,0.44027,-
0.26309,-
0.877323,0.33,0.22,0.47,0.65,0.77,0.99,0.24,0.66,0.99,0.77,0.1
0,0.55])

#Given the following data :


#weight=[78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69]

#(a) create a simple histogram from the above data


import matplotlib.pyplot as plt

weight=[78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69]
plt.hist(weight,cumulative='True',orientation='vertical',histtype
='barstacked')
plt.title("Simple Histogram")
plt.show()

import matplotlib.pyplot as plt


import numpy as np
data=np.random.randn(1000)
plt.hist(data,color='g',edgecolor='r')
plt.show()

# Python Program illustrating


# numpy.random.randn() method
import numpy as np
import matplotlib.pyplot as plt

# 1D Array
array = np.random.randn(10)
print("1D Array filled with random values : \n", array);
plt.hist(array)
plt.show()

import matplotlib.pyplot as plt


import numpy as np

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,10,20,30,40,50,60,70,80,90,100])
plt.title("histogram")
plt.show()
"""

You might also like