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

Matplotlib File

The document contains code samples demonstrating how to create different types of plots using Matplotlib in Python, including bar plots, scatter plots, pie charts, and histograms. The code samples show how to customize properties of the plots such as colors, labels, titles, legends, and more. Multiple code samples are provided for each plot type to demonstrate additional features and customizations that can be applied.

Uploaded by

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

Matplotlib File

The document contains code samples demonstrating how to create different types of plots using Matplotlib in Python, including bar plots, scatter plots, pie charts, and histograms. The code samples show how to customize properties of the plots such as colors, labels, titles, legends, and more. Multiple code samples are provided for each plot type to demonstrate additional features and customizations that can be applied.

Uploaded by

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

Bar Plot

import matplotlib.pyplot as plt


x=["python","c","c++","Java"]
y=[85,70,60,82]
plt.xlabel("Language",fontsize=15,color="y")
plt.ylabel("popularity",fontsize=15,color="r")
c=["b","g","r","y"]
plt.bar(x,y,width=0.4,color=c,align="edge",edgecolor="m",linewidth=3,linestyle=":",alpha=0.5)
plt.show()

import matplotlib.pyplot as plt


x=["python","c","c++","Java"]
y=[85,70,60,82]
plt.xlabel("Language",fontsize=15,color="y")
plt.ylabel("popularity",fontsize=15,color="r")
c=["b","g","r","y"]
plt.bar(x,y,width=0.4,color=c,align="edge",edgecolor="m",alpha=0.8,label='bar_plot')
plt.legend() #to get the label appear on the plot we have to use legend
plt.show()
import matplotlib.pyplot as plt
import numpy as np

x=["python","c","c++","java"]
y=[85,70,60,82]
z=[20,30,40,50]
width=0.2

p=np.arange(len(x))
p1=[j+width for j in p]

plt.xlabel("language",fontsize=15)
plt.ylabel("No.",fontsize=15)
plt.title("wscube",fontsize=15)

plt.bar(p,y,width,color="r",label="popularity")
plt.bar(p1,z,width,color="y",label="popularity1")

plt.xticks(p+width/2,x,rotation=15) # plt.xticks(position,Name,)
plt.legend()
plt.show()
Scatter Plot
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(8,6))
ax=fig.add_subplot(111)
day=[1,2,3,4,5,6,7]
no=[2,3,1,4,5,3,6]
ax.set(title="scatter plot",xlabel="Day",ylabel="No")
plt.scatter(day,no,color="r")
plt.show()

import matplotlib.pyplot as plt


day=[1,2,3,4,5,6,7]
no=[2,3,1,4,5,3,6]
colors=["r","y","g","b","r","g","r"]
sizes=[70,80,70,100,90,75,95]
plt.scatter(day,no,c=colors,s=sizes,alpha=0.5)
plt.title("Scatter plot",fontsize=15)
plt.xlabel("Day",fontsize=15)
plt.ylabel("No",fontsize=15)
plt.show()

import matplotlib.pyplot as plt


day=[1,2,3,4,5,6,7]
no=[2,3,1,4,5,3,6]
colors=[10,49,30,29,56,20,30]
sizes=[70,80,70,100,90,75,95]
plt.scatter(day,no,c=colors,s=sizes,cmap="BrBG")
plt.colorbar() # See the color bar gets added at the right side of the scatter plot. Value
corresponding to colors
plt.title("Scatter plot",fontsize=15) #will be used for particular marker.
plt.xlabel("Day",fontsize=15)
plt.ylabel("No",fontsize=15)
plt.show()
Pie Plot
from matplotlib import pyplot as plt
import numpy as np

cars = ['AUDI', 'BMW', 'FORD',


'TESLA', 'JAGUAR', 'MERCEDES']

data = [23, 17, 35, 29, 12, 41]

fig = plt.figure(figsize =(10, 7))


plt.pie(data, labels = cars)

plt.show()

from matplotlib import pyplot as plt


import numpy as np

cars = ['AUDI', 'BMW', 'FORD',


'TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
ex=[0.3,0.0,0.0,0.0,0.0,0.0]
c=[‘r’,’y’,’b’,’g’,’r’,’y’]
fig = plt.figure(figsize =(10, 7))
plt.pie(data, labels =
cars,explode=ex,colors=c,autopct=”%0.1f%%,shadow=True,radius=1.5,labeldistance=1.1,startangle=180,
textprops={“fontsize”:15},counterclock=False,wedgeprops={“linewidth”:10}”) # explode to take certain
portion out from center

# show plot
plt.show()
Hist Plot
from matplotlib import pyplot as plt

import numpy as np

# Creating dataset
a = np.array([22, 87, 5, 43, 56,
73, 55, 54, 11,
20, 51, 5, 79, 31,
27])

# Creating histogram
fig, ax = plt.subplots(figsize =(10, 7))
ax.hist(a, bins = [0, 25, 50, 75, 100])

# Show plot
plt.show()

You might also like