Tutorials for plotting data graphs using Python's matplotlib under Linux

Source: Internet
Author: User
Tags python script install matplotlib
If you want to get an efficient, automated, high-quality scientific drawing solution in Linxu, you should consider trying out the Matplotlib library. Matplotlib is a python-based open source science mapping package, based on the Python Software Foundation license release. A number of documents and examples, integration of Python and numpy scientific computing packages, and automation capabilities are some of the reasons for a reliable choice of scientific drawing in a Linux environment. This tutorial will provide several examples of drawing using Matplotlib.
features

    • supports a wide range of chart types, such as: Bar,box,contour,histogram,scatter,line plots ....
    • Python-based syntax
    • Integrated NumPy Scientific Computing package
    • A data source can be a list of Python, a key-value pair, and an array
    • Customizable chart formats (axis scaling, label placement, label content, etc.)
    • Customizable text (font, size, position ...)
    • Support Tex format (equation, symbol, Greek font ...)
    • Compatible with Ipython (allows interaction with the chart in a Python shell)
    • Automation (Create a chart with a Python loop)
    • Create a picture with a Python loop iteration
    • Save the picture format as a picture file, such as: Png,pdf,ps,eps,svg, etc.

Matplotlib based on Python syntax is the foundation of many of its features and efficient workflows. There are many scientific drawing packages for high-quality graphs around the world, but are these packages allowed to be used directly in your Python code? Besides, do these packages allow you to create pictures that can be saved as picture files? Matplotlib allows you to do all of these tasks. So you can save time by using it and you can spend less time creating more pictures.
Installation

Installing Python and numpy packages is a prerequisite for using matplotlib.

You can install matplotlib in Debian or Ubuntu with the following command:

  

The following commands are available in Fedora or Centos/rhel environments:

  

matplotlib Example

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

    • Discrete and linear graphs
    • Bar chart
    • Pie chart

In these examples we will use a Python script to execute the mapplotlib command. Note that the NumPy and matplotlib modules need to be imported in the script via the import command.

NP is a namespace reference for the Nuupy module, and the PLT is a namespace reference for Matplotlib.pyplot:

  Import NumPy as NP  import Matplotlib.pyplot as Plt

Example 1: Discrete and linear graphs

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

    • Create 3 Datasets (XDATA,YDATA1 and YDATA2)
    • Create a 8-inch, 6-inch-wide graph (Assignment 1)
    • Set the title of the drawing, x-axis labels, y-axis labels (font size is 14)
    • Draw the first dataset: YData1 is a function of the XData dataset, with a discrete blue line identified by a dot, identified as "Y1 data"
    • Draw the second dataset: YData2 is a function of the XData dataset, using the red solid line, identified as "y2 data"
    • Place the legend in the upper-left corner of the diagram
    • Save picture as a PNG format file

The contents of script1.py are as follows:

Import NumPy as NP  import Matplotlib.pyplot as plt     xData = Np.arange (0, 1)  yData1 = xdata.__pow__ (2.0) 
  
   ydata2 = Np.arange (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 picture is as follows:


Example 2: Bar chart

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

    • Create a normal distribution dataset that contains 1000 random samples.
    • Create a 8-inch, 6-inch-wide graph (Assignment 1)
    • Set the title, x-axis label, y-axis label (font size is 14) for the graph
    • Draw a histogram of 40 bars and edges from 10 to 10 using the samples data set
    • Add text to display the Greek letter Mu and Sigma in tex format (font size is 16)
    • Save the picture in PNG format.

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, ten))  Plt.text (- 9, +, 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 that contains 5 integers
    • Create a 6-inch, 6-inch-wide graph (Assignment 1)
    • Add an axis graph with a aspect ratio of 1
    • Set the caption of the diagram (font size is 14)
    • Use the data list to draw a pie chart with labels
    • Save the image in PNG format

The code for the script script3.py is as follows:

  Import NumPy as NP  import Matplotlib.pyplot as plt     data = [  6, 6] plt.figure (Num=1, figsize= )  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 results are shown in the following link:

Summarize

This tutorial provides several examples of drawing with the Matplotlib Science drawing package, Matplotlib is an excellent solution for scientific drawing in Linux environments, with its seamless and python, numpy connectivity, automation capabilities, and provide a variety of customized high-quality paint products.

  • 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.