Beautiful graphs can also be made with python at your fingertips! Yiwen teaches you how to draw stacked line graphs in Python

Today, I will talk to you about the things related to Python drawing, and I will continue to cultivate the classic matplotlib library of Python!

Okay, let's get started!

First, let’s talk about how to draw a classic stacked line chart in Python

At this point, some friends may ask: What is a stacked line chart? In fact, this stacked line chart is the easiest to see in our daily life. For example, the common trend chart of the stock market is a typical stacked line chart. Let’s talk about its official definition. A stacked line chart is a line drawn by different data sets. The chart generated by the graph is a combined graph formed by drawing several line graphs in an arrangement sequence that is stacked on each other in the vertical direction without covering each other.

For the official definition of the stacked line chart above, is it a bit obscure and difficult to understand? Okay, it doesn't matter, let's take a "chestnut" to understand:

As you can see, the stackplot() function in the matplotlib library is used to draw the stacked line chart. In this function, pay attention to the several parameters we defined above. If the parameters are set correctly, you can draw the stacked line chart at will Oh.

Okay, let's run it to see the effect. First, run this program with a Python interpreter:

After executing this command, the stacked line chart we drawn will be output:

Well, after learning how to draw a stacked line chart, let's discuss one more, it is as follows:

Secondly, let's talk about how to draw discontinuous bar graphs in Python

If you want to draw a discontinuous bar graph, you have to call the broken_barh() function in the matplotlib library. The syntax and usage of this function are still presented in the form of "chestnuts", so that everyone will first have an intuitive view Get acquainted, okay, give a "chestnut":

For the program we wrote above, there may be some unclear points. Let's explain it below. Take the 10th line to call the function broken_barh() that draws a discontinuous bar graph as an example.

Line 10 code:

plt.broken_barh([(30,100),(180,50),(260,70)],(20,8),facecolors='red')

Among the parameters, the first parameter [(30,100),(180,50),(260,70)] is a list. There are 3 tuples in this list, and the first element (30,100) means from The x-axis value is the starting point of 30, move 100 units along the positive direction of the x-axis. Similarly, the second tuple (180,50) represents the starting point of the x-axis value of 180, and move along the positive direction of the x-axis. 50 units; the third tuple (260,70) means to move 70 units in the positive direction along the x-axis from the starting point where the x-axis value is 260.

The second parameter (20,8) of the function is also a tuple, which means moving 8 units in the positive direction along the y-axis from the starting point of 20 on the y-axis; the third parameter of the function, facecolors, represents the drawn bar The color to be filled in the body

Alright, after talking about the meaning of the function parameters, I believe you will already use the broken_barh() function. Let's run it to see the effect. First, we must call the Python interpreter to run the program:

After running, it will display the intermittent bar graph we drew:

Well, the above is the content of how to draw a stacked line chart and a discontinuous bar chart that we discussed. Are these two charts also very beautiful? I hope you can learn how to call the two functions of drawing these two types of charts. , And then use it in your usual work, let's talk about it today, see you next time! [Goodbye]

Python learning exchange group , welcome all friends to exchange and learn.

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112317759