Python MatPlotLib Module
The MatPlotLib Library for Mathematics and Statistics is one of the indispensable modules for Python.

The Python MatPlotLib module was developed to visualize Data Analysis, the common intersection of Mathematics and Statistics, with graphs. With MatPlotLib, you can convert many types of graphs from 2D line graphs to linear graphs and even multidimensional graphs.
You can use the link below for documents, examples and solutions related to MatPlotLib Library.

Installation, Documentation: https://matplotlib.org
GitHub : https://github.com/matplotlib/matplotlib
GitHub Tutorial: https://github.com/omersahintr/BootCampEdu/tree/main/MatPlot
Along with the MatplotLib library, do not forget to import NumPy and Pandas libraries into your project and install them into the Python kernel.
pip install matplotlib
pip install numpy
pip install pandas
Let’s write the following codes to create our first chart using Python MatPlotLib.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
age = [25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45]
height = [168, 134, 210, 174, 176, 188, 180, 150, 184, 121, 188]
np_age = np.array(age) # convert list to numpy array
np_height = np.array(height) # convert list to numpy array
plt.xlabel('Age') # x-axis label
plt.ylabel('Height') # y-axis label
plt.title('Age vs Height') # title of the graph
plt.plot(np_age, np_height,"g") # g is for green color
plt.show() # display the graph
PythonGraph Output:

This is the simplest graphical representation of the data set entered in the code block with the Python MatPlotLib library.
MatPlotLib Module Chart Types
When you examine the Examples section on the official page of the MatPlotLib library from the links we have given at the beginning of our article, you can find documents about many chart types and how they are used. We will take a look at the most used MatPlotLib chart types.
Curve Graph
We can also plot sinusoidal, parabolic and curve graphs with the plot method. However, this requires a data set with exponential growth. Let’s use a little math experience and create a new data set with exponential growth by squaring the linear data set we created with the NumPy library.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 2 # square of each number in new_numpay1
print(new_numpay2)
plt.plot(new_numpay1, new_numpay2, 'r') # r is for red color
plt.show() # display the graph
PythonGraph Output:

If we swap data sets in the plot method, the graph will change.
- plt.plot(new_numpay2, new_numpay1, ‘r’)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 2 # square of each number in new_numpay1
print(new_numpay2)
plt.plot(new_numpay2, new_numpay1, 'r') # r is for red color
plt.show() # display the graph
PythonGraph Output:

Chart Style Parameters
You can use the color= and alpha= parameters to change the color and opacity values on the graph. We determine the line thickness and shape with linewidth= and linestyle=. Likewise, we add marker= and markersize= parameters to add markers to the intersection points and change its properties.
- plt.plot(new_numpay2, new_numpay1, color=”#renk_hex_kod”, alpha=float_deger, linewidth=cizgi_kalinligi_float, linewidth=5, linestyle=”-.”)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 1.3 # square of each number in new_numpay1
print(new_numpay2)
ax2.plot(new_numpay3, new_numpay3 + 3, color="#00FF88", linewidth=4.5, linestyle=":", marker="+", markersize=10)
plt.show() # display the graph
PythonGraph Output:

In the graph we created using the MatPlotLib Library , you can see that the vertical curve is transparent. You can adjust the alpha value by changing it between 0 and 1. You can also see that the transparent line in gray is thicker.
Creating a Data Point on a Chart
When specifying the chart color, if you write one of the symbols such as * – . + at the end of the color letter, the points in the chart will be represented by that symbol.
- plt.plot(new_numpay2, new_numpay1, ‘r+’)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 2 # square of each number in new_numpay1
print(new_numpay2)
plt.plot(new_numpay2, new_numpay1, 'r+') # r is for red color + is for plus sign
plt.show() # display the graph
PythonGraph Output:

Drawing Graphics with Both Line and Symbol
When drawing a graph in the MatPlotLib Module, if you want the intersection points and the line to overlap, it will be enough to put a hyphen after the symbol.
- plt.plot(new_numpay1, new_numpay2, ‘r.-‘)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 2 # square of each number in new_numpay1
print(new_numpay2)
plt.plot(new_numpay1, new_numpay2, 'r.-') # r is for red color + is for plus sign
plt.show() # display the graph
PythonGraph Output:

Subplotting with Subplot() Method
- plt.subplot(1,2,1) # Draw graph 1 with 1 row and 2 columns.
- plt.plot(new_numpay1, new_numpay2, ‘r*-‘) # red * symbol
- plt.subplot(1,2,2) # 1 row, 2 columns, draw the 2nd graph.
- plt.plot(new_numpay1, new_numpay2, ‘g+-‘) # green + symbol
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay2 = new_numpay1 ** 2 # square of each number in new_numpay1
print(new_numpay2)
plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st position
plt.plot(new_numpay1, new_numpay2, 'r*-') # r is for red color + is for plus sign
plt.subplot(1, 2, 2) # 1 row, 2 columns, 2nd position
plt.plot(new_numpay2, new_numpay1, 'g+-') # g is for green color -- is for dash line
plt.show() # display the graph
PythonGraph Output:

MatPlotLib Chart in Chart
Figure() Metodu
To draw a graph within a graph,
figure(x-axis, y-axis, x-size, y-size)
- figures = plt.figure() # create a figure object
- coord = figures.add_axes([0.1, 0.1, 0.8, 0.8]) # graphic dimensions
- coord.plot(new_numpay11, new_numpay22, ‘b’) # graph the data set.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay11 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay22 = new_numpay11 ** 2 # square of each number in new_numpay1
print(new_numpay22)
figures = plt.figure() # create a figure object
coord = figures.add_axes([0.1, 0.1, 0.8, 0.8]) # add axes to the figure
coord.plot(new_numpay11, new_numpay22, 'b') # b is for blue color
coord.set_xlabel('X-axis') # x-axis label
coord.set_ylabel('Y-axis') # y-axis label
coord.set_title('X vs Y Charts') # title of the graph
plt.show() # display the graphthe graph
PythonGraph Output:

Figure Recording Process
- fig1.savefig(“figure1.png”, dpi=200)
line will save it to the directory where the project files are located or to any file path you give.
Drawing Nested Graphics
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_numpay11 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
new_numpay22 = new_numpay11 ** 2 # square of each number in new_numpay1
print(new_numpay22)
figures = plt.figure() # create a figure object
coord = figures.add_axes([0.1, 0.1, 0.8, 0.8]) # add axes to the figure
coord.plot(new_numpay11, new_numpay22, 'b') # b is for blue color
coord.set_xlabel('X-axis') # x-axis label
coord.set_ylabel('Y-axis') # y-axis label
coord.set_title('Max Chart') # title of the graph
coord2 = figures.add_axes([0.2, 0.5, 0.4, 0.3]) # add axes to the figure
coord2.plot(new_numpay22, new_numpay11, 'r') # r is for red color
coord2.set_xlabel('X-axis') # x-axis label
coord2.set_ylabel('Y-axis') # y-axis label
coord2.set_title('Min Chart') # title of the graph
plt.show() # display the graph
Python
Subplots() – Split the Chart Screen
- (chart1, chart2) = figures.subplots(row, column)
- (chart1, chart2) = figures.subplots(1,2) # 1 row 2 column chart
- (chart1, chart2) = figures.subplots() # schedule
With the help of this method, sub chart areas are created with the specified number of rows and columns.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
## Create a subplots() object
np_distance = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
np_time = np_distance ** 1.3 # square of each number in new_numpay1
(fig1,ax1) = plt.subplots() #
ax1.plot(np_distance, np_time, 'r') # r is for red color
ax1.plot(np_time, np_distance, 'g') # g is for green color
plt.show() # display the graph
PythonGraph Output:

MatPlotLib Chart Types
Scatter
- plt.scatter(dizi1,dizi2)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_array1 = np.linspace(0, 10, 20) # 20 numbers between 0 and 10
plt.scatter(new_array1, new_array1 ** 2, color="red", label="X^2") # scatter plot
plt.show() # display the graph
PythonGraph Output:

Histogram (Bar) Graphs
- plt.hist(numpy_array)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
new_array2 = np.random.randn(30) # random 1000 numbers
plt.hist(new_array2, bins=10, color="orange") # histogram plot
plt.show() # display the graph
PythonGraph Output:
