8/19/24, 9:21 AM zeta_Updated_matplotlib.
ipynb - Colab
pip install matplotlib
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.2.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.51.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.5)
Requirement already satisfied: numpy>=1.20 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.25.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (24.0)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
x = np.array([2000, 2021, 2022, 2023, 2024])
y = np.array([4, 4.5, 9, 7.78, 6.54])
plt.plot(x, y)
plt.show()
# Default X-Points
#import matplotlib.pyplot as plt
import numpy as np
y = np.array([3, 8, 1, 10, 5, 7])
plt.plot(y)
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 1/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Plotting Without Line
To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x, y," o" )
plt.show()
keyboard_arrow_down Markers
You can use the keyword argument marker to emphasize each point with a specified marker:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y, marker = '*')
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 2/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Marker Size
ms to set the size of the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y, marker = '*',ms = 20)
plt.show()
Marker Color shorter mec to set the color of the edge of the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y, marker = '*',ms = 20, mec= "yellow")
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 3/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Set the FACE color to red:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r')
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 4/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Dotted plot
Use argument linestyle, or shorter ls
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted')
plt.show()
Line Color
You can use the keyword argument color or the shorter c to set the color of the line
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="brown")
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 5/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Line Width
You can use the keyword argument linewidth or the shorter lw to change the width of the line.
The value is a floating number, in points:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.5')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.5')
plt.plot(y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="green", linewidth = '2.5')
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 6/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
#Draw two lines by specifiyng the x- and y-point values for both lines:
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])
plt.plot(x1, y1, x2, y2)
plt.show()
Create Labels for a Plot
With Pyplot, you can use the xlabel() and ylabel() functions
to set a label for the x- and y-axis.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.0')
plt.xlabel("Demand")
plt.ylabel("Supply")
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 7/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Create a Title for a Plot
With Pyplot, you can use the title() function to set a title for the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.0')
plt.title("Demand-Supply Management")
plt.xlabel("Demand")
plt.ylabel("Supply")
plt.show()
Set Font Properties for Title and Labels
You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font properties for the title and labels.
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 8/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
font1 = {'family':'serif','color':'blue','size':18}
font2 = {'family':'serif','color':'darkred','size':18}
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.0')
plt.title("Demand-Supply Management", fontdict = font1)
plt.xlabel("Demand", fontdict = font2)
plt.ylabel("Supply", fontdict = font2)
plt.show()
Add Grid Lines to a Plot
With Pyplot, you can use the grid() function to add grid lines to the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 6, 8, 10])
y = np.array([3, 8, 1, 10, 23])
font1 = {'family':'serif','color':'blue','size':18}
font2 = {'family':'serif','color':'darkred','size':18}
plt.plot(x,y,marker = '*',ms = 20, mec= "yellow", mfc = 'r', linestyle = 'dotted', color="magenta", linewidth = '2.0')
plt.title("Demand-Supply Management", fontdict = font1)
plt.xlabel("Demand", fontdict = font2)
plt.ylabel("Supply", fontdict = font2)
plt.grid()
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 9/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Display Multiple Plots
With the subplots() function you can draw multiple plots in one figure:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
Matplotlib Bars
Creating Bars With Pyplot, you can use the bar() function to draw bar graphs:
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 10/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([30, 80, 10, 100])
plt.bar(x,y)
plt.show()
Matplotlib Histograms
Histogram A histogram is a graph showing frequency distributions
Example: Say you ask for the height of 250 people, you might end up with a histogram like this:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()
Creating Pie Charts
With Pyplot, you can use the pie() function to draw pie charts:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([40, 30, 25, 5])
plt.pie(x)
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 11/12
8/19/24, 9:21 AM zeta_Updated_matplotlib.ipynb - Colab
Legend
To add a list of explanation for each wedge, use the legend() function:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([35, 25, 25, 15])
mylabels = ["Apple", "Microsoft", "Cognizant", "Google"]
plt.pie(x, labels = mylabels)
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apple", "Microsoft", "Cognizant", "Google"]
plt.pie(y, labels = mylabels)
plt.legend(title = "Students Selected")
plt.show()
https://colab.research.google.com/drive/1M6IBhhFq-ZZh24mfaACI_dwuVzLrkEby#printMode=true 12/12