Matplotlib is the most famous Drawing Library in python. It provides a complete set of command APIs similar to matlab and is very suitable for interactive plotting. It can also be easily used as a drawing control and embedded into GUI applications. Its documentation is quite complete, and there are hundreds of thumbnails on the Gallery page. After opening it, all the source programs are available. Therefore, if you want to draw a certain type of graph, you can simply browse, copy, and paste the graph on this page. ----- Reference from: http://hyry.dip.jp/pydoc/matplotlib_intro.html
You can from http://www.lfd.uci.edu /~ Download and install matplotlib from gohlke/pythonlibs/# matplotlib.
In this article, we use matplotlib to build the simplest bar step by step and move forward to the complicated bar. What is the simplest bar? Read the following statement to see how simple it is:
ImportMatplotlib. pyplot as plt
Plt. bar (left = 0, height = 1)
Plt. show () execution result: Yes, three sentences are enough. It is the simplest drawing statement I have ever seen. First, we import matplotlib. pyplot, then directly call its bar method, and finally use show to display the image. Let me explain two parameters in bar:
- Left: Position of the left edge of the column. If we specify 1, the x value of the left edge of the current column is 1.0.
- Height: This is the height of the column, that is, the value of the Y axis.
Left and height can be replaced by a single value (a column at this time) or a tuples (multiple rectangles at this time ). For example, the following example:
ImportMatplotlib. pyplot as plt
Plt. bar (left = (0.5), height = (1 ))
Plt. show ()
We can see that left = (0, 1) means there are two rectangles in total, the left edge of the first is 0, and the left edge of the second is 1. The height parameter is the same. Of course, you may think these two rectangles are too fat. In this case, we can set the width of bar by specifying the width parameter.
ImportMatplotlib. pyplot as plt
Plt. bar (left = (0.5), height = (1, 0.35), width =)
Plt. show () is required again now. I need to specify the description of x and Y axes. For example, the X axis is gender, and the Y axis is the number of people. The implementation is also very simple. Check the Code:ImportMatplotlib. pyplot as plt
Plt. xlabel (u'gender ')
Plt. ylabel (u'count ')
Plt. bar (left = (0.5), height = (1, 0.35), width =)
Plt. show () Note that the Chinese character here must be u (3.0 or above does not seem to be needed, I use 2.7), because matplotlib only supports unicode. Next, let's describe each bar on the X axis. For example, the first is "male" and the second is "female ".ImportMatplotlib. pyplot as plt
Plt. xlabel (u'gender ')
Plt. ylabel (u'count ')
Plt. xticks (0, 1), (u 'male', u 'female '))
Plt. bar (left = (0.5), height = (1, 0.35), width =)
Plt. show ()
The usage of plt. xticks is similar to that of left and height we mentioned earlier. If you have several bars, they are the tuples of several dimensions. The first is the text position, and the second is the specific text description. However, there is a problem here. Obviously, the specified position 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 this is troublesome. You can center the text by directly specifying align = "center" in the bar method.
ImportMatplotlib. pyplot as plt
Plt. xlabel (u'gender ')
Plt. ylabel (u'count ')
Plt. xticks (0, 1), (u 'male', u 'female '))
Plt. bar (left = (0.5), height = (1, 0.35), width =, align = "center ")
Plt. show ()
Next, we can add a title to the icon. Plt. title (u "Gender Ratio Analysis"), of course, there are still few legends:
ImportMatplotlib. pyplot as plt
Plt. xlabel (u'gender ')
Plt. ylabel (u'count ')
Plt. title (u "Gender Ratio Analysis ")
Plt. xticks (0, 1), (u 'male', u 'female '))
Rect = plt. bar (left = (0.5), height = (1, 0.35), width =, align = "center ")
Plt. legend (rect,), (u "legend ",))
Plt. show ()
Note that the legend method here must contain tuples. Even if you only have one legend, it is not displayed correctly.Next, we can mark the specific vertex Y value on each rectangle. Here, we need to use a general method:
DefAutolabel (rects ):
ForRect
InRects:
Height = rect. get_height ()
Plt. text (rect. get_x () + rect. get_width ()/2., 1.03 * height, '% s' %
Float(Height) the parameters of plt. text are x and y, respectively, and the text to be displayed. Therefore, the call code is as follows:
ImportMatplotlib. pyplot as plt
DefAutolabel (rects ):
ForRectInRects:
Height = rect. get_height ()
Plt. text (rect. get_x () + rect. get_width ()/2., 1.03 * height, '% s' %Float(Height ))
Plt. xlabel (u'gender ')
Plt. ylabel (u'count ')
Plt. title (u "Gender Ratio Analysis ")
Plt. xticks (0, 1), (u 'male', u 'female '))
Rect = plt. bar (left = (0.5), height = (1, 0.35), width =, align = "center ")
Plt. legend (rect,), (u "legend ",))
Autolabel (rect)
Plt. show ()
This graph is basically complete, but you can see that a rectangle is close to the top, not very nice. It is better to leave a distance out. No specific attributes are found for this setting. However, I still use a small trick. Is the yerr parameter of the bar attribute. Once this parameter is set, there will be a vertical line on the corresponding rectangle. I don't know what he is doing. However, when I set this value very small, the blank above will be automatically blank. Rect = plt. bar (left = (0.5), height = (1, 0.35), width = 0.000001, align = "center", yerr =)
Whether the left and right sides can be blank is not found yet (xerr parameter is not supported)