Cara menggunakan MATPLOTLIB教程 pada Python

Matplotlib is a Python plotting library which helps you to create visualization of the data in 2 -D graph. The Matplotlib Tutorial article is completely for beginners. In this tutorial, you will know the different ways to plot graph in Python Programming language.In Today’s world, you can find complications in different ways everywhere. Therefore we try to find best and quick solution to handle these complications. You all know big data, machine learning, data science e.t.c are the future trend. Companies using big data technologies have large data sets. They use these data sets for many purposes from predictions to recommendation e.t.c. For example, a finance company will try to choose the best stocks for investing using data visualizations.

[toc]

1. Simple Introduction to Matplotlib

Before we begin in details, let’s understand the concept of Matplotlib. Matplotlib is a Python library for data visualizations. It helps you to mostly plot 2- D dimensional graphs. It is built upon Numpy and Scipy framework.There are various types of graph plotting can be done using Matplotlib. The following graph plots can be done.

  1. Bar Graph
  2. Histogram
  3. Pie Plot
  4. Scatter Plot
  5. Area Plot

Matplotlib library is already installed default to the newer version of python. However, if want to know how to install Matplotlib in details, then there is official Matplotlib website for it.

2. Why Matplotlib for Plotting graphs: Advantages

Matplotlib is opensource visualization library. There are also other Data Visualization tools but most of the tools in the list are paid. However, let’s focus on the some of the major advantages of Matplotlib.

  1. Matplotlib is open source library, thus it makes everyone to use it without paying any licenses fees.
  2. It runs on any operating system Windows, Linux, MacOs e.t.c.
  3. Matplotlib is a complete package for data visualization. Data can be acquired, elaborate and plot easily using this tool.
  4. It uses in any many application as it has a lot of graphs types, features. Thus making it more customizable according to various use cases.
  5. The last advantage of Maplotlib is that it uses Python which is very popular language among the data scientist.

The above are all the advantages of Matplotlib. Now you must be thinking what will be the disadvantage of it. Then there is only one disadvantage. It cannot be used with other programming languages except for Python. It means you can use it within Python code only. However, There is one programming language, where you can use it. That is Julia but via  PyPlot package.

In the next section of Matplotlib Tutorial, you will learn how to plot different types of the graph with the simple example. We are using Pycharm Python IDE for programming.But you are free to use other IDEs. Read the Best Python IDEs for Data Science article to find out the other IDEs. In addition, you make sure that you should type all code yourself to learn easily.

3. Line Chart Plotting

You must import matplotlib library module before plotting the figures. The methods plot(x,y) is use for plotting the graph and show()  for displaying the figures.

First Simple Matplotlib Plotting

import matplotlib.pyplot as plt #import the Python Matplotlib sub-module for graph plotting pyplot.
x = [1,2,3,4] # x axis
y = [1,2,3,4] # y axis
plt.plot(x,y)# plotting the graph
plt.show() #Displaying the figures

The above code is very basic and simple example of Line Plotting.Below is the definition of each line code. It describes  what these lines are mean by ( functionality ). Please remember it will be very useful for you to understand the other examples.

Line 1: import matplotlib.pyplot as plt will import the Python Matplotlib sub-module for graph plotting pyplot.

Line 2 : plt.plot(x,y)  is actually a plotting command. This command will plot the values from x values to the horizontal axis and y values to the Y- axis.

Line 3: plt.show() command will open the window contains the image of the plot. The output of the above codes is below.

这段代码中,除了

python3 test.py
0函数之外都是我们熟悉的内容。
python3 test.py
0函数的前两个参数指定了subplot数量,即:它们是以矩阵的形式来分割当前图形,两个整数分别指定了矩阵的行数和列数。而第三个参数是指矩阵中的索引。

因此,下面这行代码指的是:2行1列subplot中的第1个subplot。

plt.subplot(2, 1, 1)

下面这行代码指的是:2行1列subplot中的第2个subplot。

plt.subplot(2, 1, 2)

所以这段代码的结果是这个样子:

Cara menggunakan MATPLOTLIB教程 pada Python

python3 test.py
0函数的参数不仅仅支持上面这种形式,还可以将三个整数(10之内的)合并一个整数。例如:
python3 test.py
3可以写成
python3 test.py
4,
python3 test.py
5可以写成
python3 test.py
6。