0% found this document useful (0 votes)
14 views

Graphical Visualization - Ipynb

The document discusses different ways to create line plots, scatter plots, histograms and subplots using Matplotlib in Python. It shows how to generate and plot random data, add labels, legends and titles to plots, and arrange multiple plots horizontally and vertically using subplots.

Uploaded by

Tejas Tadka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Graphical Visualization - Ipynb

The document discusses different ways to create line plots, scatter plots, histograms and subplots using Matplotlib in Python. It shows how to generate and plot random data, add labels, legends and titles to plots, and arrange multiple plots horizontally and vertically using subplots.

Uploaded by

Tejas Tadka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Line Plot

import matplotlib as mpl


#import the pyplot module from matplotlib as plt (short name used for referring the object
import matplotlib.pyplot as plt

# import the NumPy package


import numpy as np
# generate random number using NumPy, generate two sets of random numbers and store in x,
x = np.linspace(0,50,100)
y = x * np.linspace(100,150,100)
print("\n Line Plot")
# Create a basic plot
plt.plot(x,y, color ='black')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Line Plot

(2)

import numpy as np
import pandas as pd
# Import Library
import matplotlib.pyplot as plt
%matplotlib inline
x = [1,2,3,4]
y = [10,20,25,30]
fig = plt.figure()
ax = fig.add_subplot()
ax.plot(x, y, color='red', linewidth=5)
ax.scatter([2,4,6],[5,15,25],color='darkgreen',marker='^')
ax.set_xlim(1, 6.5)
plt.savefig('fig.png')
plt.show()

(3)

x = np.linspace(0, 10, 100)


y = np.cos(x)
z = np.sin(x)

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(x, y, color='lightblue', linewidth=5) # cos
ax.plot(x, z, color='red', linewidth=5) # Sin

[<matplotlib.lines.Line2D at 0x7f3d1b5835d0>]

(4)

# we use NumPy to randomly generate an array with 250 values,


#where the values will concentrate around 170, and the standard deviation is 10
x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()
(5)

objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')


y_pos = np.arange(len(objects))
y_pos

array([0, 1, 2, 3, 4, 5])

import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt
# import the NumPy package
import numpy as np
# generate random number using NumPy, generate two sets of random numbers and store in x,
x = np.linspace(0,50,100)
y = x * np.linspace(100,150,100)
# Create a basic plot
plt.plot(x,y)

[<matplotlib.lines.Line2D at 0x7f3d1b7f5690>]

# add color, style, width to line element


plt.plot(x, y, c = 'r', linestyle = '--', linewidth=2)
# add markers to the plot, marker has different elements i.e., style, color, size etc.,
plt.plot (x, y, marker='*', c='g', markersize=3)
[<matplotlib.lines.Line2D at 0x7f3d1b395810>]

Double-click (or enter) to edit

# add grid using grid() method


plt.plot (x, y, marker='*', markersize=3, c='b', label='normal')
plt.grid(True)
# add legend and label
plt.legend()

<matplotlib.legend.Legend at 0x7f3d1b3a44d0>

(6)

# lets plot two lines Sin(x) and Cos(x)


# loc is used to set the location of the legend on the plot
# label is used to represent the label for the line in the legend
# generate the random number
x= np.arange(0,1500,100)
plt.plot(np.sin(x),label='sin function x')
plt.plot(np.cos(x),label='cos functon x')
plt.legend(loc='upper right')
<matplotlib.legend.Legend at 0x7f3d1b726bd0>

(7)

x= np.linspace(0,100,50)
plt.plot(x,'r',label='simple x')
plt.show()
plt.plot(x*x,'g',label='two times x')
plt.show()
plt.legend(loc='upper right')
Double-click (or enter) to edit

#subplots are used to create multiple plots in a single figure


# let’s create a single subplot first following by adding more subplots
x = np.random.rand(50)
y = np.sin(x*2)
#need to create an empty figure with an axis as below, figure and axis are two separate ob
fig, ax = plt.subplots()
#add the charts to the plot
ax.plot(y)

[<matplotlib.lines.Line2D at 0x7f3d1b33ef90>]

No handles with labels found to put in legend.


<matplotlib.legend.Legend at 0x7f3d1b6cbf10>

#(12) # Let’s add multiple plots using subplots() function


# Give the required number of plots as an argument in subplots(), below function creates 2
fig, axs = plt.subplots(2)
#create data
x=np.linspace(0,100,10)
# assign the data to the plot using axs
axs[0].plot(x, np.sin(x**2))
axs[1].plot(x, np.cos(x**2))
# add a title to the subplot figure
fig.suptitle('Vertically stacked subplots')
Text(0.5, 0.98, 'Vertically stacked subplots')

#(13) # Create horizontal subplots


# Give two arguments rows and columns in the subplot() function
# subplot() gives two dimensional array with 2*2 matrix
# need to provide ax also similar 2*2 matrix as below
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
#FDS-ASSIGNMENT 4 | Prepared by Prof. Amit Mogal
# add the data to the plots
ax1.plot(x, x**2)
ax2.plot(x, x**3)
ax3.plot(x, np.sin(x**2))
ax4.plot(x, np.cos(x**2))
# add title
fig.suptitle('Horizontal plots')

Text(0.5, 0.98, 'Horizontal plots')

You might also like