Example of plotting a data graph using the Matplotlib module in Python

Source: Internet
Author: User
Matplotlib is Python's most famous drawing library, which provides a complete set of command APIs similar to those of MATLAB, making it ideal for interactive mapping. It can also be easily used as a drawing control, embedded in GUI applications. Its documentation is quite complete, and there are hundreds of thumbnails on the gallery page, and the source program opens. So if you need to draw some kind of diagram, just browse/copy/paste it on this page and basically get it done.

In this article, we use Matplotlib to move from the simplest bar to the complex bar. What is the simplest bar, see the following statement you know how simple she is:

Import Matplotlib.pyplot as Plt  plt.bar (left = 0,height = 1) plt.show ()

Execution effect:

Yes, three sentences is OK, the simplest drawing statement I have ever seen. First we import the Matplotlib.pyplot, then call its bar method directly, and finally show the image with show. Let me explain the two parameters in bar:

    1. Left: The position of the ieft edge of the column, and if we specify 1 then the x value of the left edge of the current bar is 1.0.
    2. Height: This is the column, which is the value of the y-axis.

Left,height In addition to using a separate value (which is a bar at this time), you can also use tuples to replace (this time representing multiple rectangles). For example, the following example:

Import Matplotlib.pyplot as Plt Plt.bar (left = (0,1), height = (1,0.5)) Plt.show ()

You can see that left = (0,1) means that there is a total of two rectangles, the first one has 0, the left edge of the second one is 1. The height parameter is the same.
Of course, you may still think the two rectangles are "too fat". At this point we can set their widths by specifying the bar's width parameter.

Import Matplotlib.pyplot as Plt Plt.bar (left = (0,1), height = (1,0.5), width = 0.35) plt.show ()

At this point again, I need to indicate the x, Y axis. For example, the x axis is gender, and the Y axis is the number. Implementation is also very simple, see the code:

Import Matplotlib.pyplot as Plt plt.xlabel (U ' sex ') Plt.ylabel (U ' number ') Plt.bar (left = (0,1), height = (1,0.5), width = 0.35) plt . Show ()

Note Here the Chinese must use U (3.0 above as if not, I use 2.7), because matplotlib only support Unicode. Next, let's describe each bar on the x-axis. For example, the first one is "male" and the second is "female".

Import Matplotlib.pyplot as Plt plt.xlabel (U ' sex ') Plt.ylabel (U ' number ') plt.xticks ((0,1), (U ' male ', U ' Female ')) Plt.bar (left = (0,1), Height = (1,0.5), width = 0.35) plt.show ()

The usage of plt.xticks is almost the same as the left,height we talked about earlier. If you have a few bars, it is a tuple of several dimensions. The first one is the position of the text, the second is the specific text description. But here's a question, obviously the position we specify is somewhat "offset", and the ideal state should be in the middle of each rectangle. You can change (0,1) = ((0+0.35)/2, (1+0.35)/2) but it's more troublesome. We can get the text centered by directly specifying the align= "center" inside the bar method.

Import Matplotlib.pyplot as Plt plt.xlabel (U ' sex ') Plt.ylabel (U ' number ') plt.xticks ((0,1), (U ' male ', U ' Female ')) Plt.bar (left = (0,1), Height = (1,0.5), width = 0.35,align= "center") plt.show ()

Loading in ...
Next, we can also add a caption to the icon.
Plt.title (U "sex ratio case analysis")

Of course, there are also fewer legends:

Import Matplotlib.pyplot as Plt plt.xlabel (U ' sex ') Plt.ylabel (U ' number ')  plt.title (u "sex Ratio analysis") Plt.xticks ((0,1), (U ' m ', U ' female ') rect = Plt.bar (left = (0,1), height = (1,0.5), width = 0.35,align= "center") plt.legend ((Rect,), (U "Legend",)) Plt.show ()

Note Here the legend method, the parameters inside must be a tuple. Even if you have only one legend, it is not displayed correctly.
Next, we can also annotate each rectangle with its point y value. Here, we need to use a common method:

def autolabel (rects): For  rect in rects:    height = rect.get_height ()    Plt.text (rect.get_x () +rect.get_width ()/2., 1.03*height, '%s '% float (height)) where the parameters of Plt.text are: x-coordinate, y-coordinate, text to display. So the calling code is as follows: Import Matplotlib.pyplot as Plt def AutoLabel (rects): For  rect in rects:    height = rect.get_height () 
  plt.text (rect.get_x () +rect.get_width ()/2., 1.03*height, '%s '% float (height)) plt.xlabel (U ' sex ') Plt.ylabel (U ' number of people ')  plt.title (U "sex ratio case analysis") plt.xticks ((0,1), (U ' male ', U ' Female ')) rect = Plt.bar (left = (0,1), height = (1,0.5), width = 0.35,align= "Center") plt.legend ((Rect,), (U "Legend",)) AutoLabel (rect) plt.show ()

Here the figure is basically complete, but you can see a rectangle close to the top, not very nice. It is better to be able to empty out a distance. This setting I did not find the specific attribute. However, I have achieved this by a small skill. is the Yerr parameter of the Bar property. Once this parameter is set, then the corresponding rectangle will have a vertical line, I do not know what he does. However, when I set this value to a very small time, the above blank is automatically empty.

Rect = Plt.bar (left = (0,1), height = (1,0.5), width = 0.35,align= "center", yerr=0.000001)

For the left and right side can empty blank to temporarily haven't found (xerr parameter not)

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