Matplotlib is a Python-language 2D drawing library that supports a variety of platforms and is powerful enough to easily draw a variety of professional images. This article is for the Python drawing library matplotlib Getting Started tutorial, interested friends to learn about it
Operating Environment
Since this is a Python-language package, you need an environment where the Python language is installed first on your machine. To do this, search the network for a method of obtaining it yourself.
For more information on how to install Matplotlib, see here: Matplotlib installing.
I recommend you through the way of PIP installation, the specific method is as follows:
sudo pip3 install matplotlib
The source code and test data in this article can be obtained here: matplotlib_tutorial
The code example in this article uses a different Python library: NumPy. Readers are advised to be familiar with NumPy, and I have written a NumPy basic tutorial, see here: Python Machine Learning Library NumPy Tutorial.
The code in this article is tested in the following environment:
Introduced
The matplotlib is suitable for a variety of environments, including:
With Matplotlib, you can easily generate various types of images, such as: histograms, spectrogram, bar charts, scatter plots, etc. And it can be very easy to achieve customization.
Getting Started code sample
Let's take a look at one of the simplest examples of code that lets us feel what matplotlib is like:
# Test.pyimport Matplotlib.pyplot as Pltimport numpy as Npdata = Np.arange (201) Plt.plot (data) plt.show ()
The body logic of this code is only three rows, but it draws a very intuitive linear graph, as follows:
To follow this line chart, let's explain the logic of the three lines of code:
By np.arange(100, 201) generating an array of integers between [100, 200], its value is: [100, 101, 102, ..., 200]
By matplotlib.pyplot drawing it out. Obviously, the plotted values correspond to the ordinate (Y-axis) in the graph. The matplotlib itself sets the horizontal axis of the graph (x-axis): [0, 100], because we have exactly 100 values
By plt.show() showing this graphic
This piece of code is very simple and works the same way. If you already have the context of this article, save the above code to a text file (or get the source of this article via GitHub), then you can see the graphic above on your own computer with the following command:
Python3 test.py
Note 1: In the following tutorial, we will step through how to customize every detail in the diagram. For example: axes, graphics, coloring, line styles, and so on.
NOTE 2: If not necessary, the outer border of the graphic is removed below, leaving only the drawing body.
Draw multiple shapes at once
Sometimes, we might want to draw multiple graphs at once, for example: a comparison of two sets of data, or a different presentation of a set of data.
You can create multiple graphs in the following ways:
Multiple figure
Can be simply understood as one figure is a graphical window. matplotlib.pyplotthere will be a default figure , and we can also plt.figure() create more by creating one. As shown in the following code:
# Figure.pyimport Matplotlib.pyplot as Pltimport numpy as Npdata = Np.arange (+, 201) plt.plot (data) data2 = Np.arange (200, 301) Plt.figure () Plt.plot (data2) plt.show ()
This code draws a graph of two windows, each of which is a line graph of different intervals, as shown below:
Note: The initial state of these two windows is completely coincident.
Multiple subplot
In some cases, we want to display multiple graphs in the same window. At this point, you can use multiple subplot. Here is a sample code:
# Subplot.pyimport Matplotlib.pyplot as Pltimport numpy as Npdata = Np.arange (+, 201) Plt.subplot (2, 1, 1) plt.plot (data) d Ata2 = Np.arange (301) Plt.subplot (2, 1, 2) plt.plot (DATA2) plt.show ()
In this code, we are subplot familiar with everything except the function. subplotthe first two parameters of the function specify the number of subplot, that is, they divide the current drawing in the form of a matrix, and two integers specify the number of rows and columns of the matrix, respectively. The third parameter refers to the index in the matrix.
Thus, the following line of code refers to the 1th subplot in column 2, row 1 of subplot.
Plt.subplot (2, 1, 1)
The following line of code refers to the 2nd subplot in column 2, row 1 of subplot.
Plt.subplot (2, 1, 2)
So the result of this piece of code is this:
subplotThe parameters of the function are not only supported in this form, but can also be combined with an integer of three integers (within 10). For example: 2, 1, 1 can be written 211 , 2, 1, 2 can be written 212 .
Therefore, the result of the following code is the same:
Import Matplotlib.pyplot as Pltimport numpy as Npdata = Np.arange (+, 201) Plt.subplot (211) Plt.plot (data) data2 = Np.arang E (301) Plt.subplot (212) Plt.plot (data2) plt.show ()
subplotFor a detailed description of the function, see here: Matplotlib.pyplot.subplot
Common graphic Examples
Matplotlib can generate a lot of graphic styles, to the point of awe. Here you can: Matplotlib Gallery feel it.
In this article, as the first introductory tutorial, let's look at some of the most commonly used graphic drawing.
Linear graphs
In the previous example, the points on the horizontal axis of the linear graph are automatically generated, and we most likely want to set it up proactively. In addition, we may want to customize the lines as well. Take a look at the following example:
# Plot.pyimport Matplotlib.pyplot as Pltplt.plot ([1, 2, 3], [3, 6, 9], '-R ') Plt.plot ([1, 2, 3], [2, 4, 9], ': G ') plt.show ()
This code allows us to get a graph like this:
This code illustrates the following:
plotThe first array of functions is the value of the horizontal axis, the second array is the value of the vertical axes, so they are one straight line, one is a polyline, and the last parameter is made up of two characters, each of which is the style and color of the line. The former is a red line, and the latter is a green dot line. For descriptions of styles and colors see plot API Doc:matplotlib.pyplot.plot for functions
Scatter chart
scatterfunction is used to plot a scatter plot. Again, this function requires two pairs of paired data to specify the coordinates of the x and Y axes. Here is a sample code:
# Scatter.pyimport Matplotlib.pyplot as Pltimport numpy as NpN = 20plt.scatter (Np.random.rand (N) *, Np.random.ran D (N) *, c= ' R ', s=100, alpha=0.5) plt.scatter (Np.random.rand (N) *, Np.random.rand (N) *, c= ' G ', s= alpha=0.5) Plt.scatter (Np.random.rand (N) *, Np.random.rand (N) *, c= ' B ', s=300, alpha=0.5) Plt.show ()
This code illustrates the following:
This image contains three sets of data, each containing 20 random coordinates of the position parameter c representing the color of the point, the s size of the point, the alpha transparency
The drawing of this code is as follows:
scatterFor a detailed description of the function, see here: Matplotlib.pyplot.scatter
Pie chart
piefunction is used to draw a pie chart. A pie chart is often used to express percentages of parts in a collection.
# Pie.pyimport Matplotlib.pyplot as Pltimport numpy as Nplabels = [' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ', ' Sun ']data = Np.random.rand (7) * 100plt.pie (data, labels=labels, autopct= '%1.1f%% ') plt.axis (' Equal ') plt.legend () plt.show ()
This code illustrates the following:
datais a group of 7 data in a random numerical chart by labels specifying the autopct precision format of the specified numeric value to specify the plt.axis('equal') axis size consistently plt.legend() indicates that you want to draw the legend (see Upper-right corner)
The graph for this code output is as follows:
pieFor a detailed description of the function, see here: Matplotlib.pyplot.pie
Bar chart
barfunction is used to draw a bar chart. Bar charts are often used to describe the comparison of a set of data, for example: Seven days a week, the daily traffic flow of the city.
Here is a code example:
# Bar.pyimport Matplotlib.pyplot as Pltimport numpy as NpN = 7x = Np.arange (N) data = Np.random.randint (low=0, high=100, si ze=n) colors = Np.random.rand (n * 3). Reshape (n,-1) labels = [' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ', ' Sun ']plt.title ("Wee KDAY data ") Plt.bar (x, Data, alpha=0.8, Color=colors, Tick_label=labels) plt.show ()
This code illustrates the following:
This image shows a set of results with 7 random numbers, each of which is a random number of [0, 100] whose color is also generated by random numbers. To np.random.rand(N * 3).reshape(N, -1) represent the number of random numbers (N x 3), and then assemble them into 7 rows, each line is three numbers, which corresponds to the three components of the color. If you don't understand this line of code, take a look at the Python Machine Learning Library NumPy Tutorial Specifies the title of the title graphic, the labels label specified, and the alpha transparency
The graph for this code output is as follows:
barFor a detailed description of the function, see here: Matplotlib.pyplot.bar
Histogram
histfunction to draw a histogram. The histogram looks something like a bar chart. But they do not have the same meaning, and the histogram describes how often the data appears in a range within the data. So there's some abstraction, and we're going to get a good idea of what we're describing with a code example:
# Hist.pyimport Matplotlib.pyplot as Pltimport numpy as Npdata = [Np.random.randint (0, N, N) for n in [, 4000, 5000]]l Abels = [' 3K ', ' 4K ', ' 5K ']bins = [0, +, +,, 4000, 5000]plt.hist (data, Bins=bins, Label=labels) Plt.leg End () plt.show ()
In the above code, [np.random.randint(0, n, n) for n in [3000, 4000, 5000]] an array containing three arrays is generated, which:
The first array contains 3,000 random numbers, the range of which is [0, 3000) The second array contains 4,000 random numbers, the range of which is [0, 4000) The third array contains 5,000 random numbers, the range of which is [0, 5000]
The bins array is used to specify the bounds of the histogram we display, i.e. [0, 100] There will be a data point, [100, 500] there will be a data point, and so on. So the final result will show a total of 7 data points. Similarly, we have specified labels and legends.
The output of this code is as follows:
In this picture, we see that the three groups of data have data below 3000, and the frequency is similar. But the blue bar is only 3000 below the data, orange Bar only 4000 below the data. This coincides with our random array data.
histFor a detailed description of the function, see here: matplotlib.pyplot.hist
Conclusion
Through this article, we have already known the approximate use of matplotlib and some of the most basic drawing methods.
It is necessary to note that because it is an introductory tutorial, we only give the most basic methods of using these functions and graphs in this article. But in fact, they are much more functional than that. So in this article we have posted the API addresses of these functions for further study by the reader.