Creating Scatter Charts
The scatter chart can be created through two functions of pyplot library:
i. The plot() function
ii. The scatter() function
In plot() whenever you specify marker type/style, whether with colour or without colour,
and do not give linestyle argument, plot creates a scatter chart.
For example:
when linestyle is specified In absence of linestyle
import matplotlib.pyplot as pl import matplotlib.pyplot as pl
marks=[78,67,98,55,88] marks=[78,67,98,55,88]
test=[1,2,3,4,5]
test=[1,2,3,4,5]
pl.xlabel('test')
pl.xlabel('test')
pl.ylabel('Marks')
pl.title('Marks Analysis') pl.ylabel('Marks')
pl.plot(test,marks,color='y',linestyle='solid', pl.title('Marks Analysis')
linewidth=4,marker='P',markeredgecolor='r') pl.plot(test,marks,'r+')
pl.show() pl.show()
Creating Scatter Charts using scatter( ) Function
Syntax is:
Matplotlib.pyplot.scatter(x,y,s=None,c=None,marker=None)
Parameters Description
x, y The data positions
s The marker size in points**2 optional
c Marker color, sequence, or sequence of color,
optional argument.
marker MarlerStyle, Optional argument
For example:
import matplotlib.pyplot as pl
marks=[78,67,98,55,88]
test=[1,2,3,4,5]
pl.xlabel('test')
pl.ylabel('Marks')
pl.title('Marks Analysis')
pl.scatter(test,marks)
pl.show()
Specifying varying color and sizes for data points:
scatter( ) allows us to specify different sizes and colours for data ponts.
For this array of colour and array of sizes should be specified for argument c and argument s
like given below:
import matplotlib.pyplot as pl
marks=[78,67,98,55,88]
test=[1,2,3,4,5]
pl.xlabel('test')
pl.ylabel('Marks')
pl.title('Marks Analysis')
pl.scatter(test,marks,c=['r','m','c','g','b'],
s=[20,50,80,60,100])
pl.show()
Program based on Scatter Chart/graph:
Q1 Write a program to plot a scatter graph taking a random distribution in X and Y(both
with shape as (100) having randomly generated integers and plotted against each other.
import numpy as np
import matplotlib.pyplot as pl
X=np.random.randint(1,100,size=(100,))
Y=np.random.randint(1,100,size=(100,))
pl.scatter(X,Y,color='b')
pl.xlabel("Values of X")
pl.ylabel("Values of Y")
pl.show()
Q2 Write a program to plot a scatter graph taking a random distribution in X and Y(both
with shape as (250) having randomly generated integers and plotted against each other
with varying sizes and varying colors.
import numpy as np
import matplotlib.pyplot as pl
X=np.random.randint(1,100,size=(250,))
Y=np.random.randint(1,100,size=(250,))
size=range(1,60,5)
color123=['r','b','c','y','k','g']
pl.scatter(X,Y,color=color123,s=size)
pl.xlabel("Values of X")
pl.ylabel("Values of Y")
pl.show()
Q3 Write a program to plot a scatter graph taking three random distribution in X1,y1 and
Z1(all three with shape as (250) having randomly generated integers) and plotted against
each other with varying sizes.
import numpy as np
import matplotlib.pyplot as pl
X=np.random.randint(1,100,size=(250,))
Y=np.random.randint(1,100,size=(250,))
Z=np.random.randint(1,100,size=(250,))
size=range(1,60,5)
pl.scatter(X,Y,color='r',s=size)
pl.scatter(X,Z,color='b',s=size)
pl.xlabel("Values of X")
pl.ylabel("Values of Y and Z")
pl.show()