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

Python - Graph Plotting - Code

The document contains code examples for creating different types of plots using Matplotlib in Python, including: 1) A simple line plot with labeled x- and y- axes and a title; 2) A plot with two lines on the same graph with a legend; 3) A customized line plot with adjustments to colors, styles, widths, markers, and axis ranges; 4) Bar charts and histograms to visualize categorical and continuous data; 5) A pie chart showing portion breakdowns with labels, colors, and exploded slices.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python - Graph Plotting - Code

The document contains code examples for creating different types of plots using Matplotlib in Python, including: 1) A simple line plot with labeled x- and y- axes and a title; 2) A plot with two lines on the same graph with a legend; 3) A customized line plot with adjustments to colors, styles, widths, markers, and axis ranges; 4) Bar charts and histograms to visualize categorical and continuous data; 5) A pie chart showing portion breakdowns with labels, colors, and exploded slices.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Plotting a line

()
-----------------
# 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-------------------------------
#Plotting two or more lines on same plot
-----------------------------------
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()
---------------------------
#Customization of Plots
----------------------
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('Some cool customizations!')

# function to show the plot


plt.show()
---------------------------------------
#Bar Chart
----------------
import matplotlib.pyplot as plt

# x-coordinates of left sides of bars


left = [1, 2, 3, 4, 5]
# heights of bars
height = [10, 24, 36, 40, 5]

# labels for bars


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

# plotting a bar chart


plt.bar(left, height, tick_label=tick_label,
width=0.8, color=['red', 'green'])

# 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()
---------------------------------
#Histogram
----------------
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()
-----------------------------
#Pie-chart
-------------
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()
----------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy