Using matplotlib to draw line graph

Posted by KDesigns on Fri, 19 Jun 2020 11:15:53 +0200

matplotlib is a module of python, which is used to draw various graphs. Today, I will introduce the drawing of line graph.
Let's start with a simple example:

from matplotlib import pyplot as plt

The above lines of code can draw a simple line chart, in which x and Y constitute several points (Note: x and y can be of range type or list type, but the number of elements of both must be equal). The module can draw corresponding figures according to the points you provide.
We can find that the numbers on the x-axis and y-axis are not the coordinates of our points. In fact, matplotlib allows us to customize the numbers displayed on the x-axis and y-axis, for example, add the following two lines of code


It's like this
We found that: Although we set the x-axis number from 1-9, it starts from 3. Why? Since the coordinate X of our point has a minimum value of 4, it will automatically start from a number smaller than the minimum value and closest to the minimum value.
We can also set the size and clarity of the graph, such as


dpi is the number of pixels per inch, followed by height and width

But when we want x-axis or y-axis to display Chinese characters (only numbers or letters can be displayed if it is not set), what should we do? Let's take an example. We want to plot the temperature from 10 to 12

from matplotlib import pyplot as plot
from  matplotlib import  font_manager
import random
#Set font
x=[ i for i in range(120)]
y=[random.randint(10,40)for i in range(120)]
#Set x scale
x_ticks=["10 spot{}branch".format(i) for i in range(60)]
x_ticks+=["11 spot{}branch".format(i) for i in range(60)]

The fifth line is a way of setting font for windows system. The parameter is the location of font file in the system (you can search the. ttc file to find the location of simplified Chinese). The penultimate line uses the second parameter instead of the first one. Rotation is the degree of rotation, the direction is counter clockwise, fontproperties=my_font use our font file.
When we want to tell people what x-axis and y-axis represent, we can do this

plot.ylabel("Temperature ° C",fontproperties=my_font)

We still need to use the font file above, otherwise Chinese cannot be displayed.

We can also display the grid in the graph


Of course, you can also customize the clarity of the grid


When we want to draw two broken lines in a broken line graph, we only need to plot twice, for example (because the graph above is too dense, I changed to a simple one)

from matplotlib import pyplot as  plt
import random
from  matplotlib import  font_manager
x=[i for i in range(11,31)]
y1=[random.randint(1,6) for i in range(11,31)]
y2=[random.randint(1,6) for i in range(11,31)]
x_ticks=["{}year".format(i) for i in range(11,31)]

plt.ylabel("Number of girlfriends",fontproperties=my_font)

Look at the two lines of bold code to achieve the following effects

We can also mark what two broken lines represent in the figure, just add the following code


matplotlib can also set the color, thickness, transparency and so on of lines. I will not list them here. You can check them again when you use them.

Topics: Windows Python