Python Data Visualization: Matplotlib

Source: Internet
Author: User
Keywords data visualization python python data visualization
In today's information explosion era, how to gather a large amount of information and a small chart, let people make judgments at a glance, and easily convey complex views will be the primary proposition for data workers. To do good things, we must first sharpen our tools. matplotlib is a powerful tool for python visualization. I have matplotlib in hand, drawing pictures.

So let's take a look at what classic charts matplotlib has?

1. Comparing single-cell processing analysis Scanpy uses matplotlib to combine the violin diagrams of multiple genes beautifully.


2. Deeptools' classic heatmap and lineProfile are carefully crafted by the Google team using matplotlib~~




3. GenomeTrack, the 3D genome artifact, uses matploltlib to perfectly perform joint visualization and comparison of multi-omics.



In addition, we can also use matplotlib to create some charts that combine elegance and intelligence~~


Now that we understand what matplotlib (https://matplotlib.org/gallery/index.html) can do, how do we get started with matplotlib? Then you need to start with two big pieces: one is the basic chart, and the other is the layout. Drawing is like placing a plate, the chart is like the ingredients, and the layout is like you need to figure out where to put the ingredients.


Depends on the grammar of all the basic graphics covered by matplotlib, which requires you to go to the official website of matplotlib to see what its warehouse (https://matplotlib.org/gallery/index.html#gallery) has.

The basic chart elements of matplotlib include Figure, title, legend, ticks, labels (as shown below)


So, how do these elements correspond to the chart? Let’s take a look at the following "chestnuts"~~




The first step is to import the matplotlib package in your python script.

import matplotlib as mpl

mpl.use('Agg')

from matplotlib import pyplot as plt

plt.style.use('ggplot')



The second step is to make a simple layout~~

fig=plt.figure()

ax=fig.add_axes([0.1,0.1,0.8,0.8],axisbg='w')



The third step is to draw a basic figure~~

x = np.arange(-5, 5, 0.01)

y1 = -5*x*x + x + 10

y2 = 5*x*x + x

ax.plot(x, y1, x, y2, color='black’)

ax.fill_between(x, y1, y2, where=y2 >y1, facecolor='yellow', alpha=0.5)

ax.fill_between(x, y1, y2, where=y2 <=y1,facecolor='red', alpha=0.5)

ax.set_title('Fill Between')

Such a simple chart is produced...


Step 4: Save the chart (pdf and png format)

fig.savefig("xxx.pdf")

fig.savefig("xxx.png")




Looking at the basic chart, let’s look at the second block of matplotlib-the basic layout

First, you can use add_axes to place your chart, use left and bottom to determine the position of the lower left corner of the subgraph in the entire chart, and use the lower left point to extend to the right (width) and downward (height) to determine the chart size.


Second: For complex layouts, gridspec is so easy to use, it can't be used any more!
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.