Scatter Plots & Histograms



Scatter Plots

Lets define a couple of variables to draw the scatter plot.


Description:

As usual, we need matplotlib.pyplot module to draw the graphs and 'numpy' is another module that deals with mathematical python programming. 'x' & 'y' are 2 variables generated using numpy's random number generation methods. And 'data' is a dictionary type having 'key' & 'value' pair.

Lets draw a simple scatter plot to get a feel of it.



As simple as that. just use plt.scatter() method to generate a scatter plot. This method takes in many parameters of which  we have used the data to be plotted on and and y axes using variables x and y respectively.

Lets see a more sophisticated scatter plot.



Description:

This is also known as Bubble Plot (You know why. Obvious from the bubbles)

Notice the plt.scatter() method used here. Lets disect it:

data = data : We've defined a Dictionary type earlier as 'data' (it could be any name) which is being passed as an input to the parameter 'data'. This can be a Pandas Dataframe or a numpy array.

c='c' : Used to color the plot and can take on any COLOR as a hex value or the name of the color itself.

What does 'c' in our code mean?

'c' is a key in the 'data' dictionary we defined and takes on random values generated using numpy.
Since we've used the data=data, the corresponding values associated with the key 'c' in the dictionary
'data' are used as COLORS to plot. Hence the variation in the colors of the bubbles.

s=10*(np.random.randint(0,70,70) :  Is used to define the SIZE of the bubbles. Again it could be as
simple as 's=100'. But I wanted to show how the sizes could be assigned dynamically, hence the use
of numpy's random number generation method to assign different sizes to different bubbles.


Histogram

Lets plot a histogram using matplotlib



Description:

plt.hist() is used to plot histogram type graphs. This method has many parameters to beautify the way
it looks and aligns. For instance, we can use orientation='horizontal'  for horizontal bars or histtype='barstacked as shown below.
 

 

Comments