0% found this document useful (0 votes)
41 views7 pages

Python Line Plotting with Matplotlib

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Python Line Plotting with Matplotlib

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Line Plot

import matplotlib as mpl


#import the pyplot module from matplotlib as plt (short name used for referring the object
import [Link] 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 = [Link](0,50,100)
y = x * [Link](100,150,100)
print("\n Line Plot")
# Create a basic plot
[Link](x,y, color ='black')
[Link]('x')
[Link]('y')
[Link]()

Line Plot

(2)

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

(3)

x = [Link](0, 10, 100)


y = [Link](x)
z = [Link](x)

fig = [Link]()
ax = fig.add_subplot()
[Link](x, y, color='lightblue', linewidth=5) # cos
[Link](x, z, color='red', linewidth=5) # Sin

[<[Link].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 = [Link](170, 10, 250)

[Link](x)
[Link]()
(5)

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


y_pos = [Link](len(objects))
y_pos

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

import matplotlib
import matplotlib as mpl
import [Link] 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 = [Link](0,50,100)
y = x * [Link](100,150,100)
# Create a basic plot
[Link](x,y)

[<[Link].Line2D at 0x7f3d1b7f5690>]

# add color, style, width to line element


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

Double-click (or enter) to edit

# add grid using grid() method


[Link] (x, y, marker='*', markersize=3, c='b', label='normal')
[Link](True)
# add legend and label
[Link]()

<[Link] 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= [Link](0,1500,100)
[Link]([Link](x),label='sin function x')
[Link]([Link](x),label='cos functon x')
[Link](loc='upper right')
<[Link] at 0x7f3d1b726bd0>

(7)

x= [Link](0,100,50)
[Link](x,'r',label='simple x')
[Link]()
[Link](x*x,'g',label='two times x')
[Link]()
[Link](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 = [Link](50)
y = [Link](x*2)
#need to create an empty figure with an axis as below, figure and axis are two separate ob
fig, ax = [Link]()
#add the charts to the plot
[Link](y)

[<[Link].Line2D at 0x7f3d1b33ef90>]

No handles with labels found to put in legend.


<[Link] 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 = [Link](2)
#create data
x=[Link](0,100,10)
# assign the data to the plot using axs
axs[0].plot(x, [Link](x**2))
axs[1].plot(x, [Link](x**2))
# add a title to the subplot figure
[Link]('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)) = [Link](2, 2)
#FDS-ASSIGNMENT 4 | Prepared by Prof. Amit Mogal
# add the data to the plots
[Link](x, x**2)
[Link](x, x**3)
[Link](x, [Link](x**2))
[Link](x, [Link](x**2))
# add title
[Link]('Horizontal plots')

Text(0.5, 0.98, 'Horizontal plots')

You might also like