[Go to site: main page, start]

0% found this document useful (0 votes)
13 views12 pages

Matplotlib Sample Code Snippets

The document contains multiple sample Python programs demonstrating the use of the matplotlib library for data visualization. It includes examples of line plots, scatter plots, bar charts, histograms, and pie charts, showcasing various features such as labeling axes, customizing colors, and adding legends. Each sample program illustrates different plotting techniques and configurations.
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)
13 views12 pages

Matplotlib Sample Code Snippets

The document contains multiple sample Python programs demonstrating the use of the matplotlib library for data visualization. It includes examples of line plots, scatter plots, bar charts, histograms, and pie charts, showcasing various features such as labeling axes, customizing colors, and adding legends. Each sample program illustrates different plotting techniques and configurations.
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

Sample Program -1

import [Link] as plt


#create data for plotting
x_values = [0, 1, 2, 3, 4, 5 ]
y_values = [0, 1, 4, 9, 16,25]
#the default graph style for plot is a line
[Link](x_values, y_values)
#display the graph
[Link]()

Sample Program -2

import [Link] as plt


[Link]([1, 2, 3, 4])
[Link]('some numbers')
[Link]()
Sample program -3
import [Link] as plt
y1 =[]
y2 =[]
x = range(-100,100,10)
for i in x: [Link](i**2)
for i in x: [Link](-i**2)
[Link](x, y1)
[Link](x, y2)
[Link]("x")
[Link]("y")
[Link](-2000, 2000)
[Link](0) # horizontal line
[Link](0) # vertical line
[Link]("[Link]")
[Link]()
Sample proram -5

# importing the required


module
import [Link] as
plt
# x axis values
x = [1,2,3]
# corresponding y axis
values
y = [2,4,1]
# plotting the points
[Link](x, y)
# naming the x axis
[Link]('x - axis')
# naming the y axis
[Link]('y - axis')
# giving a title to my graph
[Link]('My first graph!')
# function to show the plot
[Link]()
Sample Program -7

import [Link] as plt

# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
[Link](x1, y1, label="line 1")

# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
[Link](x2, y2, label = "line 2")
# naming the x axis
[Link]('x - axis')
# naming the y axis
[Link]('y - axis')
# giving a title to my graph
[Link]('Two lines on same graph!')
# show a legend on the plot
[Link]()
# function to show the plot
[Link]()
import [Link] as plt
# x axis values
x = [1,2,3,4,5,6]
# corresponding y axis values
y = [2,4,1,5,2,6]
# plotting the points
[Link](x, y, color='green', linestyle='dashed',
linewidth = 3, marker='o', markerfacecolor='blue',
markersize=12)
# setting x and y axis range
[Link](1,8)
[Link](1,8)
# naming the x axis
[Link]('x - axis')
# naming the y axis
[Link]('y - axis')
# giving a title to my graph
[Link](customization of plots!')

# function to show the plot


[Link]()
import [Link] as plt
#Create data for plotting
values = [5, 6, 3, 7, 2]
names = ["A", "B", "C", "D", "E"]
[Link](names, values, color="green")
[Link]()

import [Link] as plt


#Create data for plotting
values = [5,6,3,7,2]
names = ["A", "B", "C", "D", "E"]
# Adding an "h" after bar will flip the graph
horizontal
[Link](names, values, color="yellowgreen")
[Link]()
import [Link] as plt

# heights of bars
height = [10, 24, 36, 40, 5]

# labels for bars


names = ['one','two','three','four','five']

# plotting a bar chart


c1 =['red', 'green']
c2 =['b', 'g'] # we can use this for color
[Link](left, height, width=0.8, color=c1)
# naming the x-axis
[Link]('x - axis')
# naming the y-axis
[Link]('y - axis')
# plot title
[Link]('My bar chart!')
# function to show the plot
[Link]()
import [Link] as plt

# frequencies
ages=[2,5,70,40,30,45,50,45,43,40,44,60,7,13,57,18,90
,77,32,21,20,40]

# setting the ranges and no. of intervals


range = (0, 100)
bins = 10
# plotting a histogram
[Link](ages, bins, range,
color='green',histtype='bar',rwidth=0.8)
# x-axis label
[Link]('age')
# frequency label
[Link]('No. of people')
# plot title
[Link]('My histogram')

# function to show the plot


[Link]()
import [Link] as plt
#generate fake data
x =
[2,1,6,4,2,4,8,9,4,2,4,10,6,4,5,7,7,3,2,7,5,3,5,9,
2,1]
#plot for a histogram
[Link](x, bins = 10, color='blue', alpha=0.5)
[Link]()

import [Link] as plt


#create data for plotting
x_values = [0,1,2,3,4,5]
y_values = [0,1,4,9,16,25]
[Link](x_values, y_values, s=30, color=“blue")
[Link]()
import [Link] as plt
# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]

# plotting points as a scatter plot


[Link](x, y, label= "stars", color="green",
marker="*", s=30)
# x-axis label
[Link]('x - axis')
# frequency label
[Link]('y - axis')
# plot title
[Link]('My scatter plot!')
import [Link] as plt

# defining labels
activities = ['eat', 'sleep', 'work', 'play']
# portion covered by each label
slices = [3, 7, 8, 6]

# color for each label


colors = ['r', 'y', 'g', 'b']
# plotting the pie chart
[Link](slices, labels = activities, colors=colors,
startangle=90, shadow = True, explode = (0, 0,
0.1, 0), radius = 1.2, autopct = '%1.1f%%')
# plotting legend
[Link]()

# showing the plot


[Link]()
# importing the required modules
import [Link] as plt
import numpy as np
# setting the x - coordinates
x = [Link](0, 2*([Link]), 0.1)
# setting the corresponding y - coordinates
y = [Link](x)
# potting the points
[Link](x, y)
# function to show the plot
[Link]()

You might also like