Visualization Tools Commonly Used in Python Matplotlib

Source: Internet
Author: User
Keywords matplotlib matplotlib tutorial matplotlib python
Matplotlib is a Python 2D drawing library and some basic 3D charts, which can generate pictures in various formats. Matplotlib can be used in Python scripts, Python shells, Jupyter notebooks, web application servers, etc.

Matplotlib is the brainchild of John Hunter (1968-2012), and he and many contributors have invested immeasurable time and energy to create a set of software used by thousands of scientists around the world.

View Matplotlib version

>>> import matplotlib
 
>>> matplotlib.__version__
 
To call Matplotlib in Python, usually import matplotlib.pyplot is used to call Matplotlib's integrated quick drawing pyplot module.

Figure (whole image)

Before any drawing, you need a Figure object, which can be understood as a drawing board to start drawing.

import matplotlib.pyplot as plt
 
fig = plt.figure()
In Matplotlib, the entire image is a Figure object. The Figure object can contain one or more Axes objects. Each Axes object is a drawing area with its own coordinate system.

A simple visualization tool Matplotlib commonly used in Python
Axes (axis)

After you have the Figure object, you need an axis before drawing. If there is no axis, there is no datum, so you need to add Axes. It can also be understood as paper that can really be painted.

ax = fig.add_subplot(111)
 
ax.set(xlim=[0, 5], ylim=[0, 6], title='An Example Axes',
 
ylabel='Y-Axis', xlabel='X-Axis')
 
plt.show()
The above code adds an Axes to a picture, and then sets the value range of the X axis and Y axis of this Axes, and some text information. The effect is as follows:

A simple visualization tool Matplotlib commonly used in Python
Under Matplotlib, a Figure object can contain multiple subplots (Axes), which can be quickly drawn using subplot(). The call form is as follows:

subplot(numRows, numCols, plotNum)
The entire drawing area of the chart is divided into numRows rows and numCols columns;
Then number each sub-region in the order from left to right and from top to bottom. The number of the sub-region on the upper left is 1;
The plotNum parameter specifies the area where the created Axes object is located;
For the above fig.add_subplot(111) is to add Axes, the explanation of the parameters is to generate an Axes object at the ***th position in the first row and first column of the artboard to prepare for painting.

Axes can also be generated by fig.add_subplot(2, 2, 1). The first two parameters determine the division of the panel.

If the three numbers numRows, numCols and plotNum are less than 10, they can be abbreviated as an integer, for example, subplot(221) and subplot(2,2,1) are the same.

subplot creates an axis object in the area specified by plotNum. If the newly created axis overlaps with the previously created axis, the previous axis will be deleted.

A simple visualization tool Matplotlib commonly used in Python
Multiple Axes

The following generates all Axes at once:

A simple visualization tool Matplotlib commonly used in Python
Simply summarize

At the top is the canvas, called the figure;
Can be drawn on different areas on the canvas, these areas are called subplot;
Each sub-picture area can be divided as follows:

axis is the x,y coordinate axis;
tick is the scale of each coordinate axis;
label is the label on the coordinate axis;
title is the title of each sub-picture;
data is the image drawn by the input data;
Matplotlib drawing demo code

This image is divided into 8 sub-regions, and each sub-region draws a different image.

import numpy as np
import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[3,5,10,25]
# Create subgraph
plt.subplot(241)
plt.plot(x,y)
plt.title("plot")
plt.subplot(242)
plt.scatter(x, y)
plt.title("scatter")
plt.subplot(243)
plt.pie(y)
plt.title("pie")
plt.subplot(244)
plt.bar(x, y)
plt.title("bar")
plt.subplot(245)
plt.boxplot(y, sym="o")
plt.title("box")
# sin/cos images
plt.subplot(246)
x = np.linspace(0, np.pi)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.plot(x, y_sin)
plt.plot(x, y_cos)
# g-- Set line style and color
plt.subplot(247)
plt.plot(x, y_sin,'g--')
plt.title("sin")
# Load local image
import matplotlib.image as mpimg
img=mpimg.imread('666.jpg')
plt.subplot(248)
plt.imshow(img)
plt.title("cool...")
 plt.show()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.