Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Matplotlib Sample Program

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Matplotlib Sample Program

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sample Program -1

import matplotlib.pyplot 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
plt.plot(x_values, y_values)
#display the graph
plt.show()

Sample Program -2

import matplotlib.pyplot as plt


plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
Sample program -3
import matplotlib.pyplot as plt
y1 =[]
y2 =[]
x = range(-100,100,10)
for i in x: y1.append(i**2)
for i in x: y2.append(-i**2)
plt.plot(x, y1)
plt.plot(x, y2)
plt.xlabel("x")
plt.ylabel("y")
plt.ylim(-2000, 2000)
plt.axhline(0) # horizontal line
plt.axvline(0) # vertical line
plt.savefig("quad.png")
plt.show()
Sample proram -5

# importing the required


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

import matplotlib.pyplot as plt

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

# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Two lines on same graph!')
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()
import matplotlib.pyplot 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
plt.plot(x, y, color='green', linestyle='dashed',
linewidth = 3, marker='o', markerfacecolor='blue',
markersize=12)
# setting x and y axis range
plt.ylim(1,8)
plt.xlim(1,8)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title(customization of plots!')

# function to show the plot


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

import matplotlib.pyplot 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
plt.barh(names, values, color="yellowgreen")
plt.show()
import matplotlib.pyplot 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
plt.bar(left, height, width=0.8, color=c1)
# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title('My bar chart!')
# function to show the plot
plt.show()
import matplotlib.pyplot 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
plt.hist(ages, bins, range,
color='green',histtype='bar',rwidth=0.8)
# x-axis label
plt.xlabel('age')
# frequency label
plt.ylabel('No. of people')
# plot title
plt.title('My histogram')

# function to show the plot


plt.show()
import matplotlib.pyplot 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
plt.hist(x, bins = 10, color='blue', alpha=0.5)
plt.show()

import matplotlib.pyplot as plt


#create data for plotting
x_values = [0,1,2,3,4,5]
y_values = [0,1,4,9,16,25]
plt.scatter(x_values, y_values, s=30, color=“blue")
plt.show()
import matplotlib.pyplot 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


plt.scatter(x, y, label= "stars", color="green",
marker="*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
import matplotlib.pyplot 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
plt.pie(slices, labels = activities, colors=colors,
startangle=90, shadow = True, explode = (0, 0,
0.1, 0), radius = 1.2, autopct = '%1.1f%%')
# plotting legend
plt.legend()

# showing the plot


plt.show()
# importing the required modules
import matplotlib.pyplot as plt
import numpy as np
# setting the x - coordinates
x = np.arange(0, 2*(np.pi), 0.1)
# setting the corresponding y - coordinates
y = np.sin(x)
# potting the points
plt.plot(x, y)
# function to show the plot
plt.show()

You might also like