Using Matplotlib,pylab for Python plotting

Source: Internet
Author: User

When it comes to Python drawing, Matplotlib is the most famous drawing library in Python, which contains a full set of graphing APIs like Matlab. Therefore, as the children who want to learn the Python drawing, they have to install the Matplotlib library in their own Python environment, the installation method here is not much to say, there are many ways to give a reference.

This article will teach the novice how to quickly use the interface for drawing operations in an installed matplotlib environment, and present a very intuitive drawing example, as well as a way to control some of the details in the drawing.

Now that the drawing is going to use Matplotlib's package, and we've already installed it, we must first introduce this package: import matplotlib.pyplot as Plt

  of course, you can also replace it with the introduction of Pylab (a sub-package of matplotlib, which is ideal for interactive drawing, as this article will take as an example): import pylab as PL

The next step is to draw the exact data. For example, we want to draw a y=x^2 curve, so we can write the code:

x = Range (x)  #  The data for the  axis of the in X]  #  Vertical axis Data pl.plot (×, y)  #  call Pylab's plot function to draw the curve pl.show ()  # show the plotted diagram

After execution, you can see the diagram drawn:

  

As you can see, it is very simple to display a graph, as long as you have two lists as input data, you can call plot and show function successively. Be sure to remember to show it only after calling show! only plot is no good!

In practice, it may be that a simple, crude line might not be the best result we want, for example, to display the original data point on a graph, as simple as adding an argument to the plot function: pl.plot (x, y, 'ob- ') # display data points and draw the graph with blue

This parameter usage is more flexible and can be combined from the following values:

 Color (abbreviated as C): Blue:'b'(blue) Green:'g'(green) Red:'R'(red) turquoise (dark green):'C'(cyan) Red-purple (magenta):'m'(Magenta) Yellow:'y'(yellow) Black:'k'(Black) White:'W'(white) Linetype (linestyle abbreviated to LS): Solid line:'-'Dashed Line:'--'Virtual dot line:'-.'Point line:':'points:'.' dot Type (tag marker): pixels:','Round:'o'Upper triangle:'^'Lower triangle:'v'Left triangle:'<'Right triangle:'>'Square:'s'Plus:'+'Fork Shape:'x'Prism:'D'Fine Prism:'D'tripod facing down:'1'(like ' ya ') tripod facing up:'2'tripod facing left:'3'tripod facing right:'4'Hexagon Shape:'h'rotate six angle:'H'five-angle shape:'P'Vertical line:'|'Horizontal line:'_'

The line is adjusted, but also want to add the horizontal ordinate instructions? It is also very simple to add the following code before calling the show function:

Pl.xlabel (U" i am the horizontal axis ") Pl.ylabel (u" I am the longitudinal shaft ")

The effect is as follows:

  

It is important to remember that the passed string must be Unicode encoded , if it is passed directly to the string, the form like u' here is the string to be written ' Can.

Now more intuitive, and finally like a normal figure, but also want to add a legend in the figure to do? It is not difficult to continue to pass parameters to plot:

' ob- ', label=u'y=x^2 graph ')  #  plus a label parameter add legend pl.legend ()  # Let the legend take effect

It is also true that thelabel string parameter must be prefixed with the U ' ' Declaration as Unicode encoding , otherwise the legend will be added to the failure. As follows:

  

Oh, see the image is bare, just want to add a title: pl.title (U' image title ') # string also needs to be Unicode encoded

  Sometimes our data may not be so concentrated, for example, when we want to draw some data from a project, we find that large amounts of data are clustered around 0, and that a small amount of data results in poor image display, such as:

x = Range (Ten) +[100for in'ob-', label=u' y=x^2 Curve ')

  

At this point, we want to limit the range of coordinates that need to be displayed:

Pl.xlim ( -1, one)  # Limits The range of the horizontal axis Pl.ylim ( -1)  #  limit the range of the longitudinal axis

Again on:

  

Okay, here's where plot's common drawing usage is done, and if you need to display multiple lines in a graph, you can continue to call the plot function before the show function call, passing in the data and graphical display requirements that need to be drawn.

Matplotlib is a very useful library, whether it is necessary to write paper, or data research to see the relevance of data, are a good helper. The background of writing this article is that I used this in the project to do a correlation between the characteristics and results of the study used in the drawing, learning a bit, the following is a mask of real data after rewriting a very similar (interested can go to my github to see the source, The complete code and comments for this article can also be found in this link only):

    

This article briefly introduces some of the ways to get started with the next Python drawing, and if anything is wrong, please correct me. I also recently started to really use Python, this powerful and convenient language will allow us to achieve their own ideas faster, we have a better Python information is also welcome message, study together, thank you!

Reprint Please specify source: Use Matplotlib,pylab for Python drawing (http://www.cnblogs.com/webary/p/5813855.html)

Using Matplotlib,pylab for Python plotting

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.