SoFunction
Updated on 2024-10-29

Example of plotting data in Python using the matplotlib module

matplotlib is the most famous plotting library in python, it provides a whole set of command API similar to matlab, which is very suitable for interactive mapping. It can also be easily embedded as a drawing control in GUI applications. It is well documented, and the Gallery page has hundreds of thumbnails, all of which open with the source program. So if you need to draw a certain type of diagram, just browse/copy/paste it on this page and you're basically set.

In this post, we use matplotlib to move forward from constructing the simplest bar to a complex one step by step. What is the simplest bar, look at the following statement and you will see how simple she is:

import  as plt 
 
(left = 0,height = 1)
()

Implementation effects:

 (656×557)

Yes, three sentences and it's the simplest drawing statement I've ever seen. First we import , then call its bar method directly, and finally display the image with show. I'll explain the two parameters in bar:

  1. left: the position of the left edge of the bar, if we specify 1 then the current x-value of the left edge of the bar is 1.0
  2. height: this is the height of the bar, which is the value of the Y-axis.

left, height can be replaced with tuples in addition to individual values (in this case a bar) (in this case representing multiple rectangles). For example, the following example:

import  as plt
 
(left = (0,1),height = (1,0.5))
()

 (657×518)

You can see that left = (0,1) means that there are two rectangles in total, the first one has a left edge of 0 and the second one has a left edge of 1. The same applies to the height parameter.
Of course, you may still think that these two rectangles are "too fat". We can set their width by specifying the width parameter of the bar.

import  as plt
 
(left = (0,1),height = (1,0.5),width = 0.35)
()

 (587×455)

At this point another demand comes in, I need to label the x, y axis description. For example, the x-axis is the gender, y-axis is the number of people. The realization is also very simple, look at the code:

import  as plt
 
(u'Gender')
(u'Number of people')
(left = (0,1),height = (1,0.5),width = 0.35)
()

 (599×466)

Note that the Chinese characters here must be in u (I don't think it's necessary for 3.0+, I'm using 2.7), because matplotlib only supports unicode. next, let's illustrate each of the bars on the x-axis. For example, the first one is "male" and the second one is "female".

import  as plt
 
(u'Gender')
(u'Number of people')
 
((0,1),(u'Male',u'Female'))
 
(left = (0,1),height = (1,0.5),width = 0.35)
 
()

 (589×470)

The usage is similar to the usage of left,height that we talked about earlier. If you have several bars, then it's a tuple of several dimensions. The first one is the position of the text, the second one is the specific text description. But there is a problem, obviously the position we specified is a bit "offset", ideally it should be in the center of each rectangle. You could change (0,1)=>( (0+0.35)/2 ,(1+0.35)/2 ) but that's tricky. We can center the text by specifying align="center" directly inside the bar method.

import  as plt
 
(u'Gender')
(u'Number of people')
 
((0,1),(u'Male',u'Female'))
 
(left = (0,1),height = (1,0.5),width = 0.35,align="center")
 
()

Loading ...
Next, we can also add a title to the icon.
(u "Gender Ratio Analysis")

 (617×467)

And, of course, there are illustrations to spare: the

import  as plt
 
(u'Gender')
(u'Number of people')
 
 
(u"Gender ratio analysis")
((0,1),(u'Male',u'Female'))
rect = (left = (0,1),height = (1,0.5),width = 0.35,align="center")
 
((rect,),(u"Legend",))
 
()

 (610×474)

Note the legend method here, the parameters in it must be tuples. Even if you have only one legend, otherwise the display will not be correct.
Next, we can also label the top of each rectangle with its specific point Y value. Here, we need to use a generic method:

def autolabel(rects):
  for rect in rects:
    height = rect.get_height()
    (rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))
The parameters of which are:xcoordinate (geometry),ycoordinate (geometry),Text to be displayed。the reason why,The calling code is as follows:
import  as plt
 
def autolabel(rects):
  for rect in rects:
    height = rect.get_height()
    (rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))
 
(u'Gender')
(u'Number of people')
 
 
(u"Gender ratio analysis")
((0,1),(u'Male',u'Female'))
rect = (left = (0,1),height = (1,0.5),width = 0.35,align="center")
 
((rect,),(u"Legend",))
autolabel(rect)
 
()

 (605×476)

By this point the graphic is almost complete, but you can see that you have a rectangle right next to the top of this, which is not very nice. It would be nice to have a distance between them. I didn't find any specific properties for this setting. However, I was able to accomplish it with a little trick. It's the yerr parameter of the bar property. Once this parameter is set, then the corresponding rectangle has a vertical line above it, and I'm not sure what he's doing. But when I set this value very small, the blank above is automatically empty. As shown in the picture:

rect = (left = (0,1),height = (1,0.5),width = 0.35,align="center",yerr=0.000001)

 (619×475)

I haven't found out if the left and right sides can be blanked out (the xerr parameter doesn't work).