Check out this video for an overview of the article. Then, read the article and solve exercises to strengthen your understanding.
Drawing graphs is essential for visualizing data and getting insights of the data. One popular library used for creating graphs in “Python” language is Matplotlib (https://matplotlib.org/) library, which provides a wide range of customizable options for plotting different types of graphs. With Matplotlib, users can create line plots, bar charts, scatter plots, histograms, and more. This makes it easy for programmers and data analysts to generate professional-looking graphs quickly.
By representing data visually through graphs, patterns and trends can be easily identified and understood by viewers. Furthermore, Matplotlib library allows users to label axes, add titles, customize colors and styles, and save the resulting graph as a file for further sharing. Overall, mastering the use of Matplotlib in “Python” programming is a valuable skill for anyone working with data analysis or visualization tasks.
1 Importing Matplotlib Library
When using Matplotlib library, we will almost always use matplotlib.pyplot
module. This module provides us the interface to create graphs. You can import the module into your “Python” codes using the following statement:
import matplotlib.pyplot as plt
In this import
statement, we usually use the alias plt
to refer to the pyplot
module in Matplotlib library.
NOTE
- An alias means “another name.” Rather than using
pyplot
directly, the simplerplt
is often used as an alias.- A module is a file containing Python code.
- In “Python” language, a library is a collection of modules. They are made up of modules that provide specific functionality.
2 Plotting Simple Line Plot
2.1 Preparing for the Plot
One of the most common types of plots is a simple line plot, which visualizes the relationship between two variables over a continuous range. In Matplotlib, you can create a line plot using the plt.plot()
. Here’s an example of how to create a basic line plot in Matplotlib:
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
fig, ax = plt.subplots(1, 1)
ax.plot(x, y)
In this example, we first import Matplotlib library and then define two lists x
and y
representing the x-axis and y-axis values, respectively. We then call plt.subplots()
. The two arguments 2
and 1
determines the number of rows and columns. For example, if you run the following code, it creates a graph consists of two rows and one column:
plt.subplots(2, 1)
Finally, we use the ax.plot(x, y)
to create the line plot.
NOTE: Figures and Axes in Matplotlib Library
In Matplotlib library, the concepts of axis and figure play a crucial role in creating and customizing plots.
Figure:
- A
figure
represents the entire image or window where the plot elements are displayed.- You can customize the figure size, resolution, background color, and other properties.
- You can create multiple subplots within a single
figure
to display different plots in one figure.Axis:
- Axes represent the individual plotting area within a
figure
. Anaxes
object is where the data points, grid lines, labels, and plot elements are rendered.- Each
axes
object has its own methods and attributes for customizing the appearance of the plot, such as setting axis limits, labels, ticks, grid lines, and more.Although you cad draw a graph without using
plt.subplots()
:x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y)
I think, however,
plt.subplots()
can be applied to create a more complex figures in the future. So I introduced it here.
2.2 Customizing the Plot
To enhance the readability and visual appeal of the plot, you can customize it by adding labels, titles, and colors, etc. Here’s how you can customize a plot:
fig, ax = plt.subplots(1, 1)
ax.plot(x, y, color='green', marker='o', linestyle='--')
ax.set_xlabel('$x$-axis label')
ax.set_ylabel('$y$-axis label')
ax.set_title('Simple Line Plot')
Text(0.5, 1.0, 'Simple Line Plot')
In this customized plot, we have specified the color of the line plot as green, added circular markers with marker='o'
, and set the line style as dashed with linestyle='--'
. Additionally, we have added labels to the x-axis and y-axis using ax.set_xlabel()
and ax.set_ylabel()
, respectively. The plot is given a title using ax.set_title()
. You can also change the font used in the plot with plt.rcParams['font.family'] = 'FONTNAME'
NOTE
You can use math expressions in the plots. For example,
$x$
is rendered as a letter in math expressions. You can change the font for the math expression using:plt.rcParams['mathtext.fontset'] = 'FONT'
.
3 Plotting Function Graphs
Plotting function graphs is a useful way to visualize and understand mathematical functions with ease.
NOTE: Mathematical Functions
In mathematics, a function is a relation between a set of inputs (domain) and a set of outputs (range), where each input is related to exactly one output. If an input has multiple outputs, then it is not a function. Functions are often denoted by f(x) or y = f(x), where x is the input and f(x) is the output.
We start with drawing the graph of a linear function y = 2x + 3, x \in \mathbb R, where the domain is all real numbers represented by x \in \mathbb R. First, we define the mathematical function using def
keyword:
def f(x):
return 2*x + 3
If you pass an input to this function, it returns the output:
print(f(1))
print(f(2))
print(f(-1))
5
7
1
When you conduct math-related programming, NumPy (https://numpy.org/) library is really useful. In this article, however, I will use built-in lists for the purpose of reviewing what you have learned in this crash course.
We, next, prepare input values for the graph:
xs = [-5 + 0.01*i for i in range(1000)]
print(min(xs))
print(max(xs))
-5.0
4.99
This code generates 1000 decimals between -5 and 5. Then, using xs
as the input, we will generate the output values:
ys = [f(x) for x in xs]
print(ys)
[-7.0, -6.98, -6.960000000000001, -6.9399999999999995, -6.92, -6.9, -6.880000000000001, -6.859999999999999, -6.84, -6.82, -6.800000000000001, -6.779999999999999, -6.76, -6.74, -6.720000000000001, -6.699999999999999, -6.68, -6.66, -6.640000000000001, -6.619999999999999, -6.6, -6.58, -6.5600000000000005, -6.539999999999999, -6.52, -6.5, -6.48, -6.460000000000001, -6.4399999999999995, -6.42, -6.4, -6.380000000000001, -6.359999999999999, -6.34, -6.32, -6.300000000000001, -6.279999999999999, -6.26, -6.24, -6.220000000000001, -6.199999999999999, -6.18, -6.16, -6.140000000000001, -6.119999999999999, -6.1, -6.08, -6.0600000000000005, -6.039999999999999, -6.02, -6.0, -5.98, -5.960000000000001, -5.9399999999999995, -5.92, -5.9, -5.879999999999999, -5.859999999999999, -5.84, -5.82, -5.800000000000001, -5.779999999999999, -5.76, -5.74, -5.720000000000001, -5.699999999999999, -5.68, -5.66, -5.640000000000001, -5.619999999999999, -5.6, -5.58, -5.5600000000000005, -5.539999999999999, -5.52, -5.5, -5.48, -5.460000000000001, -5.4399999999999995, -5.42, -5.4, -5.379999999999999, -5.359999999999999, -5.34, -5.32, -5.300000000000001, -5.279999999999999, -5.26, -5.24, -5.220000000000001, -5.199999999999999, -5.18, -5.16, -5.140000000000001, -5.119999999999999, -5.1, -5.08, -5.0600000000000005, -5.039999999999999, -5.02, -5.0, -4.98, -4.96, -4.9399999999999995, -4.92, -4.9, -4.88, -4.859999999999999, -4.84, -4.82, -4.8, -4.779999999999999, -4.76, -4.74, -4.72, -4.699999999999999, -4.68, -4.66, -4.640000000000001, -4.62, -4.6, -4.58, -4.5600000000000005, -4.54, -4.52, -4.5, -4.48, -4.46, -4.4399999999999995, -4.42, -4.4, -4.38, -4.359999999999999, -4.34, -4.32, -4.3, -4.279999999999999, -4.26, -4.24, -4.22, -4.199999999999999, -4.18, -4.16, -4.140000000000001, -4.12, -4.1, -4.08, -4.0600000000000005, -4.04, -4.02, -4.0, -3.9800000000000004, -3.96, -3.9399999999999995, -3.92, -3.9000000000000004, -3.88, -3.8599999999999994, -3.84, -3.8200000000000003, -3.8, -3.7799999999999994, -3.76, -3.74, -3.7199999999999998, -3.6999999999999993, -3.6799999999999997, -3.66, -3.6400000000000006, -3.62, -3.5999999999999996, -3.58, -3.5600000000000005, -3.54, -3.5199999999999996, -3.5, -3.4800000000000004, -3.46, -3.4399999999999995, -3.42, -3.4000000000000004, -3.38, -3.3599999999999994, -3.34, -3.3200000000000003, -3.3, -3.2799999999999994, -3.26, -3.24, -3.2199999999999998, -3.1999999999999993, -3.1799999999999997, -3.16, -3.1400000000000006, -3.12, -3.0999999999999996, -3.08, -3.0600000000000005, -3.04, -3.0199999999999996, -3.0, -2.9799999999999995, -2.96, -2.9399999999999995, -2.92, -2.9000000000000004, -2.88, -2.8600000000000003, -2.84, -2.8200000000000003, -2.8, -2.7800000000000002, -2.76, -2.74, -2.7199999999999998, -2.7, -2.6799999999999997, -2.66, -2.6399999999999997, -2.62, -2.5999999999999996, -2.58, -2.5599999999999996, -2.54, -2.5199999999999996, -2.5, -2.4799999999999995, -2.46, -2.4399999999999995, -2.42, -2.3999999999999995, -2.38, -2.3600000000000003, -2.34, -2.3200000000000003, -2.3, -2.2800000000000002, -2.26, -2.24, -2.2199999999999998, -2.2, -2.1799999999999997, -2.16, -2.1399999999999997, -2.12, -2.0999999999999996, -2.08, -2.0599999999999996, -2.04, -2.0199999999999996, -2.0, -1.9799999999999995, -1.96, -1.9399999999999995, -1.92, -1.8999999999999995, -1.88, -1.8600000000000003, -1.8399999999999999, -1.8200000000000003, -1.7999999999999998, -1.7800000000000002, -1.7599999999999998, -1.7400000000000002, -1.7199999999999998, -1.7000000000000002, -1.6799999999999997, -1.6600000000000001, -1.6399999999999997, -1.62, -1.5999999999999996, -1.58, -1.5599999999999996, -1.54, -1.5199999999999996, -1.5, -1.4799999999999995, -1.46, -1.4399999999999995, -1.42, -1.3999999999999995, -1.38, -1.3600000000000003, -1.3399999999999999, -1.3200000000000003, -1.2999999999999998, -1.2800000000000002, -1.2599999999999998, -1.2400000000000002, -1.2199999999999998, -1.2000000000000002, -1.1799999999999997, -1.1600000000000001, -1.1399999999999997, -1.12, -1.0999999999999996, -1.08, -1.0599999999999996, -1.04, -1.0199999999999996, -1.0, -0.9799999999999995, -0.96, -0.9399999999999995, -0.9199999999999999, -0.8999999999999995, -0.8799999999999999, -0.8599999999999994, -0.8399999999999999, -0.8200000000000003, -0.7999999999999998, -0.7800000000000002, -0.7599999999999998, -0.7400000000000002, -0.7199999999999998, -0.7000000000000002, -0.6799999999999997, -0.6600000000000001, -0.6399999999999997, -0.6200000000000001, -0.5999999999999996, -0.5800000000000001, -0.5599999999999996, -0.54, -0.5199999999999996, -0.5, -0.47999999999999954, -0.45999999999999996, -0.4399999999999995, -0.41999999999999993, -0.39999999999999947, -0.3799999999999999, -0.35999999999999943, -0.33999999999999986, -0.3200000000000003, -0.2999999999999998, -0.28000000000000025, -0.2599999999999998, -0.2400000000000002, -0.21999999999999975, -0.20000000000000018, -0.17999999999999972, -0.16000000000000014, -0.13999999999999968, -0.1200000000000001, -0.09999999999999964, -0.08000000000000007, -0.05999999999999961, -0.040000000000000036, -0.019999999999999574, 0.0, 0.020000000000000462, 0.040000000000000036, 0.0600000000000005, 0.08000000000000007, 0.10000000000000053, 0.1200000000000001, 0.14000000000000057, 0.16000000000000014, 0.17999999999999972, 0.20000000000000018, 0.21999999999999975, 0.2400000000000002, 0.2599999999999998, 0.28000000000000025, 0.2999999999999998, 0.3200000000000003, 0.33999999999999986, 0.3600000000000003, 0.3799999999999999, 0.40000000000000036, 0.41999999999999993, 0.4400000000000004, 0.45999999999999996, 0.4800000000000004, 0.5, 0.5200000000000005, 0.54, 0.5600000000000005, 0.5800000000000001, 0.6000000000000005, 0.6200000000000001, 0.6400000000000006, 0.6600000000000001, 0.6799999999999997, 0.7000000000000002, 0.7199999999999998, 0.7400000000000002, 0.7599999999999998, 0.7800000000000002, 0.7999999999999998, 0.8200000000000003, 0.8399999999999999, 0.8600000000000003, 0.8799999999999999, 0.9000000000000004, 0.9199999999999999, 0.9400000000000004, 0.96, 0.9800000000000004, 1.0, 1.0199999999999996, 1.040000000000001, 1.0600000000000005, 1.08, 1.0999999999999996, 1.120000000000001, 1.1400000000000006, 1.1600000000000001, 1.1799999999999997, 1.1999999999999993, 1.2200000000000006, 1.2400000000000002, 1.2599999999999998, 1.2799999999999994, 1.3000000000000007, 1.3200000000000003, 1.3399999999999999, 1.3599999999999994, 1.3800000000000008, 1.4000000000000004, 1.42, 1.4399999999999995, 1.4600000000000009, 1.4800000000000004, 1.5, 1.5199999999999996, 1.540000000000001, 1.5600000000000005, 1.58, 1.5999999999999996, 1.620000000000001, 1.6400000000000006, 1.6600000000000001, 1.6799999999999997, 1.700000000000001, 1.7200000000000006, 1.7400000000000002, 1.7599999999999998, 1.7799999999999994, 1.8000000000000007, 1.8200000000000003, 1.8399999999999999, 1.8599999999999994, 1.8800000000000008, 1.9000000000000004, 1.92, 1.9399999999999995, 1.9600000000000009, 1.9800000000000004, 2.0, 2.0199999999999996, 2.040000000000001, 2.0600000000000005, 2.08, 2.0999999999999996, 2.120000000000001, 2.1400000000000006, 2.16, 2.1799999999999997, 2.200000000000001, 2.2200000000000006, 2.24, 2.26, 2.2799999999999994, 2.3000000000000007, 2.3200000000000003, 2.34, 2.3599999999999994, 2.380000000000001, 2.4000000000000004, 2.42, 2.4399999999999995, 2.460000000000001, 2.4800000000000004, 2.5, 2.5199999999999996, 2.540000000000001, 2.5600000000000005, 2.58, 2.5999999999999996, 2.620000000000001, 2.6400000000000006, 2.66, 2.6799999999999997, 2.700000000000001, 2.7200000000000006, 2.74, 2.76, 2.7799999999999994, 2.8000000000000007, 2.8200000000000003, 2.84, 2.8599999999999994, 2.880000000000001, 2.9000000000000004, 2.92, 2.9399999999999995, 2.960000000000001, 2.9800000000000004, 3.0, 3.0199999999999996, 3.040000000000001, 3.0600000000000005, 3.08, 3.0999999999999996, 3.120000000000001, 3.1400000000000006, 3.16, 3.1799999999999997, 3.200000000000001, 3.2200000000000006, 3.24, 3.26, 3.2799999999999994, 3.3000000000000007, 3.3200000000000003, 3.34, 3.3599999999999994, 3.380000000000001, 3.4000000000000004, 3.42, 3.4399999999999995, 3.460000000000001, 3.4800000000000004, 3.5, 3.5199999999999996, 3.540000000000001, 3.5600000000000005, 3.58, 3.5999999999999996, 3.620000000000001, 3.6400000000000006, 3.66, 3.6799999999999997, 3.700000000000001, 3.7200000000000006, 3.74, 3.76, 3.7799999999999994, 3.8000000000000007, 3.8200000000000003, 3.84, 3.8599999999999994, 3.880000000000001, 3.9000000000000004, 3.92, 3.9399999999999995, 3.960000000000001, 3.9800000000000004, 4.0, 4.02, 4.040000000000001, 4.0600000000000005, 4.08, 4.1, 4.120000000000001, 4.140000000000001, 4.16, 4.18, 4.200000000000001, 4.220000000000001, 4.24, 4.26, 4.279999999999999, 4.300000000000001, 4.32, 4.34, 4.359999999999999, 4.380000000000001, 4.4, 4.42, 4.4399999999999995, 4.460000000000001, 4.48, 4.5, 4.52, 4.540000000000001, 4.5600000000000005, 4.58, 4.6, 4.620000000000001, 4.640000000000001, 4.66, 4.68, 4.700000000000001, 4.720000000000001, 4.74, 4.76, 4.779999999999999, 4.800000000000001, 4.82, 4.84, 4.859999999999999, 4.880000000000001, 4.9, 4.92, 4.9399999999999995, 4.960000000000001, 4.98, 5.0, 5.02, 5.040000000000001, 5.0600000000000005, 5.08, 5.1, 5.120000000000001, 5.140000000000001, 5.16, 5.18, 5.200000000000001, 5.220000000000001, 5.24, 5.26, 5.280000000000001, 5.300000000000001, 5.32, 5.34, 5.359999999999999, 5.380000000000001, 5.4, 5.42, 5.4399999999999995, 5.460000000000001, 5.48, 5.5, 5.52, 5.540000000000001, 5.5600000000000005, 5.58, 5.6, 5.620000000000001, 5.640000000000001, 5.66, 5.68, 5.700000000000001, 5.720000000000001, 5.74, 5.76, 5.780000000000001, 5.800000000000001, 5.82, 5.84, 5.859999999999999, 5.880000000000001, 5.9, 5.92, 5.9399999999999995, 5.960000000000001, 5.98, 6.0, 6.02, 6.040000000000001, 6.0600000000000005, 6.08, 6.1, 6.120000000000001, 6.140000000000001, 6.16, 6.18, 6.200000000000001, 6.220000000000001, 6.24, 6.26, 6.280000000000001, 6.300000000000001, 6.32, 6.34, 6.359999999999999, 6.380000000000001, 6.4, 6.42, 6.4399999999999995, 6.460000000000001, 6.48, 6.5, 6.52, 6.540000000000001, 6.5600000000000005, 6.58, 6.6, 6.620000000000001, 6.640000000000001, 6.66, 6.68, 6.700000000000001, 6.720000000000001, 6.74, 6.76, 6.780000000000001, 6.800000000000001, 6.82, 6.84, 6.859999999999999, 6.880000000000001, 6.9, 6.92, 6.9399999999999995, 6.960000000000001, 6.98, 7.0, 7.02, 7.040000000000001, 7.0600000000000005, 7.08, 7.1, 7.120000000000001, 7.140000000000001, 7.16, 7.18, 7.200000000000001, 7.220000000000001, 7.24, 7.26, 7.280000000000001, 7.300000000000001, 7.32, 7.34, 7.359999999999999, 7.380000000000001, 7.4, 7.42, 7.4399999999999995, 7.460000000000001, 7.48, 7.5, 7.52, 7.540000000000001, 7.5600000000000005, 7.58, 7.6, 7.620000000000001, 7.640000000000001, 7.66, 7.68, 7.700000000000001, 7.720000000000001, 7.74, 7.76, 7.780000000000001, 7.800000000000001, 7.82, 7.84, 7.859999999999999, 7.880000000000001, 7.9, 7.92, 7.9399999999999995, 7.960000000000001, 7.98, 8.0, 8.02, 8.040000000000001, 8.06, 8.08, 8.1, 8.120000000000001, 8.14, 8.16, 8.18, 8.200000000000001, 8.22, 8.24, 8.26, 8.280000000000001, 8.3, 8.32, 8.34, 8.36, 8.38, 8.4, 8.42, 8.44, 8.46, 8.48, 8.5, 8.52, 8.540000000000001, 8.56, 8.58, 8.6, 8.620000000000001, 8.64, 8.66, 8.68, 8.700000000000001, 8.72, 8.74, 8.76, 8.780000000000001, 8.8, 8.82, 8.84, 8.860000000000001, 8.88, 8.9, 8.92, 8.94, 8.96, 8.98, 9.0, 9.02, 9.04, 9.059999999999999, 9.080000000000002, 9.100000000000001, 9.120000000000001, 9.14, 9.16, 9.18, 9.2, 9.219999999999999, 9.240000000000002, 9.260000000000002, 9.280000000000001, 9.3, 9.32, 9.34, 9.36, 9.379999999999999, 9.399999999999999, 9.420000000000002, 9.440000000000001, 9.46, 9.48, 9.5, 9.52, 9.54, 9.559999999999999, 9.580000000000002, 9.600000000000001, 9.620000000000001, 9.64, 9.66, 9.68, 9.7, 9.719999999999999, 9.740000000000002, 9.760000000000002, 9.780000000000001, 9.8, 9.82, 9.84, 9.86, 9.879999999999999, 9.899999999999999, 9.920000000000002, 9.940000000000001, 9.96, 9.98, 10.0, 10.02, 10.04, 10.059999999999999, 10.080000000000002, 10.100000000000001, 10.120000000000001, 10.14, 10.16, 10.18, 10.2, 10.219999999999999, 10.240000000000002, 10.260000000000002, 10.280000000000001, 10.3, 10.32, 10.34, 10.36, 10.379999999999999, 10.400000000000002, 10.420000000000002, 10.440000000000001, 10.46, 10.48, 10.5, 10.52, 10.54, 10.559999999999999, 10.580000000000002, 10.600000000000001, 10.620000000000001, 10.64, 10.66, 10.68, 10.7, 10.719999999999999, 10.740000000000002, 10.760000000000002, 10.780000000000001, 10.8, 10.82, 10.84, 10.86, 10.879999999999999, 10.900000000000002, 10.920000000000002, 10.940000000000001, 10.96, 10.98, 11.0, 11.02, 11.04, 11.059999999999999, 11.080000000000002, 11.100000000000001, 11.120000000000001, 11.14, 11.16, 11.18, 11.2, 11.219999999999999, 11.240000000000002, 11.260000000000002, 11.280000000000001, 11.3, 11.32, 11.34, 11.36, 11.379999999999999, 11.400000000000002, 11.420000000000002, 11.440000000000001, 11.46, 11.48, 11.5, 11.52, 11.54, 11.559999999999999, 11.580000000000002, 11.600000000000001, 11.620000000000001, 11.64, 11.66, 11.68, 11.7, 11.719999999999999, 11.740000000000002, 11.760000000000002, 11.780000000000001, 11.8, 11.82, 11.84, 11.86, 11.879999999999999, 11.900000000000002, 11.920000000000002, 11.940000000000001, 11.96, 11.98, 12.0, 12.02, 12.04, 12.059999999999999, 12.080000000000002, 12.100000000000001, 12.120000000000001, 12.14, 12.16, 12.18, 12.2, 12.219999999999999, 12.240000000000002, 12.260000000000002, 12.280000000000001, 12.3, 12.32, 12.34, 12.36, 12.379999999999999, 12.400000000000002, 12.420000000000002, 12.440000000000001, 12.46, 12.48, 12.5, 12.52, 12.54, 12.559999999999999, 12.580000000000002, 12.600000000000001, 12.620000000000001, 12.64, 12.66, 12.68, 12.7, 12.719999999999999, 12.740000000000002, 12.760000000000002, 12.780000000000001, 12.8, 12.82, 12.84, 12.86, 12.879999999999999, 12.900000000000002, 12.920000000000002, 12.940000000000001, 12.96, 12.98]
Now, it’s time to visualize the graph of y = 2x + 3.
fig, ax = plt.subplots(1, 1)
ax.plot(xs, ys, color='grey')
You have drawn the graph, but this does not look good. So let’s customize the plot:
fig, ax = plt.subplots(1, 1, dpi=150)
ax.plot(xs, ys, color='red', linewidth=0.8)
# Set aspect ratio to be equal
fig.gca().set_aspect('equal', adjustable='box')
# Change the font size of tick labels
ax.tick_params(axis='both', which='major', labelsize=5)
# Add grid to the axes
ax.grid(True, linewidth=0.3)
# Add x and y axes
ax.axhline(0, color='black', linewidth=0.6)
ax.axvline(0, color='black', linewidth=0.6)
# Add function name
ax.text(1.5, 12, '$y = 2x + 3$', fontsize=8)
# Set the upper and lower limits of each axis
ax.set_xlim(min(xs), max(xs))
ax.set_ylim(min(ys)-1, max(ys)+1)
# Set tick mark spacing
ax.set_xticks([x for x in range(int(min(xs)), int(max(xs))+2)])
ax.set_yticks([y for y in range(int(min(ys))-1, int(max(ys))+3)])
What do you think of this new graph? I think this is much better than the previous one to visualize the graph of a mathematical function.
You might think it is really tiresome to write the above customization code whenever you draw a graph of a mathematical function. Don’t worry, we can rely on a function in Python language.
Here is the example function to draw the graphs of math functions. I use **kwargs
to receive the options for the text location. This is out of the scope of this crash course so don’t mind about it.
def plot_math_function(func, x_min, x_max, **kwargs):
"""
Plots the graph of a mathematical function.
Args:
- func: The mathematical function to be plotted.
- x_min: The minimum value of x for the domain.
- x_max: The maximum value of x for the domain.
- **kwargs: Additional keyword arguments that can be passed to customize the plot.
- x_pos: x position of the function name
- y_pos: y position of the function name
- label: function name to be shown on the plot
Returns:
- figure
- axis
"""
# These are the option for the function name.
options = {
'label': '',
'x_pos': x_max-1,
'y_pos': func(x_max-1)
}
options.update(kwargs)
# Generate data for plotting
step = 0.01
num_iter = int((x_max - x_min) / step)
xs = [x_min + step*i for i in range(num_iter)]
ys = [func(x) for x in xs]
fig, ax = plt.subplots(1, 1, dpi=150)
ax.plot(xs, ys, color='red', linewidth=0.8)
# Set aspect ratio to be equal
fig.gca().set_aspect('equal', adjustable='box')
# Change the font size of tick labels
ax.tick_params(axis='both', which='major', labelsize=5)
# Add grid to the axis
ax.grid(True, linewidth=0.3)
# Add x and y axes
ax.axhline(0, color='black', linewidth=0.6)
ax.axvline(0, color='black', linewidth=0.6)
# Add function name to the specified position with the given label and font size
ax.text(
options.get('x_pos'), # x-coordinate for the text
options.get('y_pos'), # y-coordinate for the text
options.get('label'), # the text to be displayed
fontsize=8 # the font size of the text
)
# Set the upper and lower limits of each axis
ax.set_xlim(min(xs), max(xs))
ax.set_ylim(min(ys)-1, max(ys)+1)
# Set tick mark spacing
ax.set_xticks([x for x in range(int(min(xs)), int(max(xs))+2)])
ax.set_yticks([y for y in range(int(min(ys))-1, int(max(ys))+3)])
return fig, ax
I edited some of the previous code to create this function. Check the differences to understand the function.
I define two function g()
and h()
and plot them:
def g(x):
return x**2 - 3
def h(x):
return 1/x
plot_math_function(g, -5, 5, label='$y=x^2$', x_pos=2, y_pos=22)
plot_math_function(h, 0.1, 10, label=r'$y=\frac{1}{x}$', y_pos=1)
Using plot_math_function()
, you can plot new functions without customizing the plot each time.
4 Conclusion
Although there are many other types of graphs, such as bar plots, box plots, and histograms, you have learned how to draw graphs using the “Python” language.