100% found this document useful (1 vote)
162 views

Assignment 1.1 - Jupyter Notebook

This Jupyter notebook document contains code examples for creating different types of plots and graphs using Matplotlib, including: bar graphs, line graphs, multiple bar graphs, histograms, scatter plots, pie charts, multiple plots, box plots, and frequency density curves. The code cells demonstrate how to generate each type of plot with sample data values and customize aspects like titles, labels, legends, colors.

Uploaded by

SHWETABH KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
162 views

Assignment 1.1 - Jupyter Notebook

This Jupyter notebook document contains code examples for creating different types of plots and graphs using Matplotlib, including: bar graphs, line graphs, multiple bar graphs, histograms, scatter plots, pie charts, multiple plots, box plots, and frequency density curves. The code cells demonstrate how to generate each type of plot with sample data values and customize aspects like titles, labels, legends, colors.

Uploaded by

SHWETABH KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/7/2019 Assignment 1.

1 - Jupyter Notebook

In [1]: #Assignment 1.1


#By Shwetabh Kumar Gupta

In [14]: #Simple Bar Graph-A simple bar chart is used to represent data involving only
one variable classified on a spatial, quantitative or temporal basis. In a
simple bar chart, we make bars of equal width but variable length, i.e. the
magnitude of a quantity is represented by the height or length of the bars.

from matplotlib import pyplot as plt


x=[1,2,3]
y=[3,4,5]

plt.bar(x,y)
plt.title('info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

In [33]: #Line Graph-A line graph, also known as a line chart, is a type of chart used
to visualize the value of something over time.

from matplotlib import pyplot as plt


x=[1,2,3]
y=[3,4,5]

plt.plot(x,y)
plt.title('info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

In [13]: #Multiple Bar Graph-A multiple bar graph shows the relationship between
different values of data. Each data value is represented by a column in the
graph. In a multiple bar graph, multiple data points for each category of
data are shown with the addition of columns.

from matplotlib import pyplot as plt

plt.bar([2,4,5,6,8,8],[4,8,5,4,3,5],label='Ex one')

plt.bar([2,5,2,2,6,9],[3,5,7,2,0,4],label='Ex two',color='g')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')

plt.title('info')
plt.show()

localhost:8888/notebooks/Assignment 1.1.ipynb 1/4


11/7/2019 Assignment 1.1 - Jupyter Notebook

In [43]: #Histogram-A histogram is a graphical display of data using bars of different


heights. In a histogram, each bar groups numbers into ranges

import matplotlib.pyplot as plt

population_ages=[22,55,62,45,221,22,34,42,42,4,56,8,78,90]
bins=[0,10,20,30,40,50,60,70,80,90,100,110,120,130]

plt.hist(population_ages,bins,histtype='bar',rwidth=0.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Histogrm')
plt.legend()
plt.show()

No handles with labels found to put in legend.

In [28]: #Scatter Plot-a graph in which the values of two variables are plotted along two axes,
the pattern of the resulting points revealing any correlation present.

import matplotlib.pyplot as plt

x=[1,2,3,4,5,6,7,8]
y=[5,2,4,2,1,4,5,2]
plt.scatter(x,y,label='skitscat',color='k')
plt.xlabel('x')
plt.ylabel('y')
plt.title('scatter plot')
plt.legend()
plt.show()

In [30]: #Pie Plot-A pie chart is a circular statistical graphic, which is divided into
slices to illustrate numerical proportion. In a pie chart, the arc length of
each slice, is proportional to the quantity it represents.

import matplotlib.pyplot as plt


slices=[7,2,2,13]
activities=['sleeping','eating','working','playing']
cols=['c','m','r','b']

plt.pie(slices,labels=activities,colors=cols,startangle=90,
shadow=True,explode=(0,0.1,0,0),autopct='%1.1f%%')
plt.title('Pie Plot')
plt.show()

localhost:8888/notebooks/Assignment 1.1.ipynb 2/4


11/7/2019 Assignment 1.1 - Jupyter Notebook

In [32]: #Multiple Plots

import numpy as np
import matplotlib.pyplot as plt

def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)

plt.subplot(211)
plt.plot(t1,f(t1),'bo',t2,f(t2))
plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2))
plt.show()

In [35]: #Box Plot-a simple way of representing statistical data on a plot in which a
rectangle is drawn to represent the second and third quartiles, usually with
a vertical line inside to indicate the median value. The lower and upper
quartiles are shown as horizontal lines either side of the rectangle.

import matplotlib.pyplot as plt

value1 = [82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]

box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,patch_artist=True,labels=['course1','course2','course3','course4'])
plt.show()

In [37]: #Frequency Density Curve-A density curve is a graph that shows probability.
The area under the density curve is equal to 100 percent of all probabilities

# libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import kde

# create data
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)

# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
nbins=300
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

# Make the plot


plt.pcolormesh(xi, yi, zi.reshape(xi.shape))
plt.show()

# Change color palette


plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.Greens_r)
plt.show()

In [ ]:

localhost:8888/notebooks/Assignment 1.1.ipynb 3/4


11/7/2019 Assignment 1.1 - Jupyter Notebook

localhost:8888/notebooks/Assignment 1.1.ipynb 4/4

You might also like