How to Use matplotlib of Python to draw data graphs in Linux

Source: Internet
Author: User
Tags install matplotlib
This article mainly introduces how to use matplotlib of Python to draw data graphs in Linux. matplotlib is an extension of Scientific Computing Based on Numpy, if you want to obtain an efficient, automated, and high-quality scientific drawing solution in Linxu, you should try the matplotlib library. Matplotlib is an open source scientific ing package based on python and is released based on the python Software Foundation license. A large number of documents and examples, integration of Python and Numpy scientific computing packages, and automation capabilities are several reasons for the reliable selection of scientific plotting in Linux environments. This tutorial provides several examples of drawing with matplotlib.
Features

  • Supports many chart types, such as bar, box, contour, histogram, scatter, line plots ....
  • Python-based syntax
  • Integrated Numpy scientific computing package
  • Data sources can be python lists, key-value pairs, and arrays.
  • Customizable chart formats (coordinate axis scaling, tag location, and tag content)
  • Customizable text (font, size, location ...)
  • Supports TeX format (equations, symbols, Greek fonts ...)
  • Compatible with IPython (allows interaction with charts in python shell)
  • Automation (using Python to create charts cyclically)
  • Generate images cyclically using Python
  • Save the format of the painted image as an image file, such as png, pdf, ps, eps, and svg.

Matplotlib Based on Python syntax is the basis of many of its features and efficient workflow. There are many scientific drawing packages for drawing high-quality graphs in the world, but do these packages allow you to use them directly in your Python code? In addition, do these packages allow you to create images that can be saved as image files? Matplotlib allows you to complete all these tasks. Therefore, you can save time and use it to create more images in less time.
Install

Installing Python and Numpy packages is a prerequisite for using Matplotlib.

Run the following command to install Matplotlib In Debian or Ubuntu:

  $ sudo apt-get install python-matplotlib 

The following command is available in the Fedora or CentOS/RHEL environment:

  $ sudo yum install python-matplotlib 

Matplotlib example

This tutorial provides several drawing examples to demonstrate how to use matplotlib:

  • Discrete and linear Graphs
  • Bar Chart
  • Pie Chart

In these examples, we will use the Python script to execute the Mapplotlib command. Note that the numpy and matplotlib modules need to be imported in the script using the import command.

Np is the namespace reference of the nuupy module, and plt is the namespace reference of matplotlib. pyplot:

  import numpy as np  import matplotlib.pyplot as plt

Example 1: discrete and linear Graphs

In the first script, script1.py completes the following tasks:

  • Create three datasets (xData, yData1, and yData2)
  • Create an image with a width of 8 inch and a height of 6 inch (value 1)
  • Set the title, X axis label, and Y axis label of the image. The font size is 14)
  • Draw the first dataset: yData1 is a function of the xData dataset. The Discrete blue line identified by dots is marked as "y1 data"
  • Draw the second dataset: yData2 is a function of the xData dataset, marked as "y2 data" by a red line"
  • Place the legend in the upper left corner of the graph.
  • Save the image as a PNG file

The content of script1.py is as follows:

 import numpy as np  import matplotlib.pyplot as plt     xData = np.arange(0, 10, 1)  yData1 = xData.__pow__(2.0)  yData2 = np.arange(15, 61, 5)  plt.figure(num=1, figsize=(8, 6))  plt.title('Plot 1', size=14)  plt.xlabel('x-axis', size=14)  plt.ylabel('y-axis', size=14)  plt.plot(xData, yData1, color='b', linestyle='--', marker='o', label='y1 data')  plt.plot(xData, yData2, color='r', linestyle='-', label='y2 data')  plt.legend(loc='upper left')  plt.savefig('images/plot1.png', format='png')

The figure is as follows:


Example 2: bar chart

The second script, script2.py, completes the following tasks:

  • Create a normal distribution dataset containing 1000 random samples.
  • Create an image with a width of 8 inch and a height of 6 inch (value 1)
  • Set the title, X axis label, and Y axis label of the graph (the font size is 14)
  • Use the samples dataset to draw a bar chart with 40 columns and edges ranging from-10 to 10.
  • Add text to display Greek letters mu and sigma in TeX format (font size: 16)
  • Save the image as PNG.

The script2.py code is as follows:

  import numpy as np  import matplotlib.pyplot as plt     mu = 0.0  sigma = 2.0  samples = np.random.normal(loc=mu, scale=sigma, size=1000)  plt.figure(num=1, figsize=(8, 6))  plt.title('Plot 2', size=14)  plt.xlabel('value', size=14)  plt.ylabel('counts', size=14)  plt.hist(samples, bins=40, range=(-10, 10))  plt.text(-9, 100, r'$\mu$ = 0.0, $\sigma$ = 2.0', size=16)  plt.savefig('images/plot2.png', format='png')

The results are shown in the following link:


Example 3: Pie Chart

The third script, script3.py, completes the following tasks:

  • Create a list containing five Integers
  • Create an image with a width of 6 inch and a height of 6 inch (value 1)
  • Add an axis chart with a aspect ratio of 1
  • Set the graph title (font size: 14)
  • Use the data list to draw a pie chart containing tags
  • Save the image as PNG

The script script3.py code is as follows:

  import numpy as np  import matplotlib.pyplot as plt     data = [33, 25, 20, 12, 10]  plt.figure(num=1, figsize=(6, 6))  plt.axes(aspect=1)  plt.title('Plot 3', size=14)  plt.pie(data, labels=('Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'))  plt.savefig('images/plot3.png', format='png')

The result is as follows:

Summary

This tutorial provides several examples of drawing with the matplotlib scientific drawing package. Matplotlib is an excellent solution for solving scientific drawing in the Linux environment, its ability to seamlessly connect to Python and Numpy, automate, and provide a variety of custom high-quality drawing products.

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.