Data Visualization with Python

Source: Internet
Author: User
Keywords data visualization python data visualization python tutorial
Data visualization is a very important part of data analysis. It can help us to obtain information more intuitively and efficiently from complex data.
matplotlib is one of the toolkits used to create charts. The purpose is to build a Matlab-style drawing interface for Python. When I first contacted, I felt that this product was very similar to the chart drawn by matlab. Although the directly created maps are not so tall, but matplotlib is indeed the basis for visualization.

Getting started with matplotlib API 

Introduce:

#In Anaconda's out, you can directly draw the picture.
% matplotlib inline
#Introducing pyplot
import matplotlib.pyplot as plt


Figure and Subplot
The matplotlib images are located in the Figure object. Create a new Figure: fig = plt.figure ()
After creating Figure, you must add_subplot to create one or more subplots: ax1 = fig.add_subplot (2,2,1) (2,2,1) means to create 2X2 images, and currently select 4 subplots The first in. If you create the following as follows (this part feels very similar to matlab):

ax2 = fig.add_subplot (2,2,2)
ax3 = fig.add_subplot (2,2,3)

Figure with three subplots

Then start drawing:

from numpy.random import randn
plt.plot (randn (50) .cumsum (), 'k--')
_ = ax1.hist (randn (100), bins = 20, color = 'k', alpha = 0.3)
ax2 = scatter (np.arange (30), np.arange (30) + 3 * randn (30))


Create Firure and subplot can use a simpler method: axes = plt.subplots (2,3) to directly create a 2X3 image.
Then treat axes as a two-dimensional array and use axes [0,1] to obtain subplot instances for operation. At the same time, you can also use sharex and sharey to specify that the subplot should have the same x-axis or y-axis, which is quite convenient when used to compare data in the same range. axes = plt.subplots (2,3, sharex = True)

Adjust the spacing around the subplot
Under normal circumstances, matplot will leave a certain margin around the subplot. When you don't need or want to adjust, you can use the following subplots_adjust method:

subplots_adjust (left = None, bottom = None, right = None, top = None, wspace = None, hspace = None)

Among them wspace and hpace are used to control the percentage of width and height.

axes = plt.subplots (2,3)
plt.subplots_adjust (wspace = None, hspace = 0)

Although the graph looks ugly, it can be clearly seen that hspace represents the height percentage between adjacent subplots.


Color, marking and line style
We used k above-in fact, the meaning is k black,-is a dotted line. Or more explicitly write the parameter linestyle = ‘–’, color = ’k’ is the same effect. RGB values (such as '#CECECE') can also be used to specify colors.

Sometimes we need to emphasize the value of each real data point, not just a line, you can use ‘ko–’ and add an o.

By default, the two data points are connected by a straight line. If you want to change it, you can use the parameter drawstyle. For example, the effect of drawstyle = ‘step-post’ will not be demonstrated. If you are interested, you can try it.

Title, scale, label
For most chart decoration items, there are two main ways of implementation: using the procedural pyplot interface (MATLAB users are very familiar with it) and the more object-oriented native matplotlib API.
The pyplot interface is designed for interactive use and contains methods such as xlim, xticks and xticklabels. They control the range, scale position, scale labels, etc. of the chart respectively. There are two ways to use it:
1. When called without parameters, the current parameters are returned. For example, plt.xlim () returns the current X-axis drawing range.
2. When calling with parameters, the parameter stream is set. Therefore, .plt.xlim ([0, 10]) will set the X axis range to 0 to 10.
Modify the scale: set_xticks, set_xticklabels. The former is responsible for the relative position, the latter is responsible for the specific scale display value.
Edit title: set_title
Modify the X axis name: set_xlabel

Annotation and drawing on Subpplot
Annotations (text, arrow, annotate): ax.text (x, y, 'hello world!', Family = 'monospace', fontsize = 10)
There are many ways to use it, and you can even draw an arrow to point you annotated.

Drawing graphics: This is achieved by creating a shp block object and then ax.add_patch (shp) it.
For example, draw a circle and add:

flg = plt.figure ()
ax = flg.add_subplot (1,1,1)
(0.7, 0.2) is the center of the circle, 0.15 is the radius, the color is blue, and alpha is the transparency
circ = plt.Circle ((0.7,0.2), 0.15, color = 'b', alpha = 0.3)
ax.add_patch (circ)


Save chart to file
Save as figpath.svg file, svg format. plt.savefig (‘figpath.svg’)
Or it is not necessary to write to disk, it is feasible to write any file-type object, such as StringIO:

from io import StringIO
buffer = StringIO ()
plt.savefig (buffer)
plot_data = buffer.getvalue ()


matplotlib configuration
Through this configuration, you can set the default size of the generated image, the default xtick, legend and so on.
For example, to set the default size to 10X10, you can execute: plt.rc (‘figure’, figsize = (10,10))

The first parameter of rc is the object you want to customize. For example, 'figure'. 'axes', 'xtick'. 'ytick', 'grid', 'legend', etc. Then you can keep up with a series of keyword parameters The easiest way is to write these options as a dictionary:

font_options = {'family': 'monospace', 'weight': 'bold', 'size': 'small'}
plt.rc ('font', ** font options)


To understand all the customization options, please refer to matplotlib's configuration file matplotlibrc (located in matplotlib / mpl-data directory). If you customize this file and place it in your own .matplotlibrc directory, it will be loaded every time you use matplotlib.

to sum up
This chapter preliminarily introduces the API of matplotlib, which will basically be used to generate some simple linear graphs and then modify the display on some graphs, highlight the effects and so on.
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.