In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "Python how to achieve the horizontal stack map of gradient", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve the horizontal stack map of gradient"!
Keywords: python, matplotlib, barh, stack
Origin: when doing data processing, you need to create a picture as follows:
Sample diagram
I happened to be learning Python recently, and then I wanted to try to implement it with python, because I had a little contact with the library matplotlib, and then I wanted to use it to implement it.
First of all, by looking up the data, we know that the name of this kind of graph is "horizontal stacking bar chart", and then the effect of horizontal stacking can be achieved by using Matplotlib.plot.barh ().
There are many tutorials on the effect of horizontal or vertical stacking, so I will not describe them here, mainly to record how to achieve gradual discoloration.
At first, I looked for information, but I didn't find out how to make the gradient color of this stacked diagram.
If an eldest brother has found a simple way, please let me know! Then try to write some statements to implement it; now make a record of the research process.
The first is the understanding of drawing function.
The barh function has a lot of arguments, but we only focus on a few:
X represents coordinates like E1 E2 E3 in the above picture (it can be understood as a scale value)
Width represents the length of the current stack, which is critical!
The left value is the starting point of the current stack, and this value is also critical
Color represents the color of the current stack, following the RGB order, but its range (0MMI 0.1), that is, we need to divide the actual (Rmae Gmail B) value by 255 before passing in.
Alpha represents the color transparency of the current stack, which is formed together with the above color parameter (RGBA)
Label represents a graphic example of the current stack, that is, a legend like "label= secondary indicator 1" in the above figure. If this parameter is empty, no legend is created. If this parameter is not empty, a legend is created based on the passed string. Chinese is not supported by default. If you want to use Chinese, you need to add the following two sentences after import matplotlib.
# used to display Chinese plt.rcParams ['font.sans-serif'] = [' SimHei','Times New Roman'] plt.rcParams ['axes.unicode_minus'] = False` normally
And then talk about how to think.
X is the scale value displayed on the number axis (you can also rename it)
Width represents the stacking (filling) length, and left is the starting point of this filling, so if we combine these two parameters below, we can divide the length that should have been filled at one time into several fillings, and then use the adjacent RGB values for these fillings, which will show a gradual effect.
# import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei','Times New Roman'] # used to normally display the Chinese tag plt.rcParams ['axes.unicode_minus'] = False # RGB color scheme Color1 = np.array ([131,175,155]) Color2 = np.array ([200,200,169]) Color3 = np.array ([249205]) ) x = ["E1"] Y1 = np.array ([0.1]) Y2 = np.array ([0.3]) plt.barh (x, Y1, align= "center", color=Color1 / 255, alpha=1, label= "ONE1") plt.barh (x, Y2, left=Y1, color=Color2 / 255, alpha=1, label= "ONE2") plt.legend () plt.show ()
The effect of the above code after running is like this
Then, based on our above analysis, we will fill the "ONE2" color block with the parameter: XMagneWidth.leftPowerColor to achieve the gradient effect:
First, let's define a variable divid, which will determine how many times you fill.
# the number of filling, the higher the value, the better the gradient effect, but the amount of calculation will also increase divid=20 accordingly.
Secondly, we divide our original filling amount and color gradient difference by divid respectively.
Y2_rate=Y2/divid# uses the next color minus the current fast color; this will also explain that in my later complete code, I will strictly require the number of Color greater than Y # in fact, Color and Y are row vectors in most cases, but they are still expressed as "number", in order to make it easier to understand Color_rate= (Color3-Color2) / divid.
Then we will use these two values in the barh function
For i in range (divid): if i==int (divid / 2): # here is to go to the center of the original color block and use RGB as the legend color plt.barh (XMed YgramrateLegend Y1colorratealpha=1.0 = (Color1+i*Color_rate) / 255, alpha=1.0,label='ONE2') else: plt.barh (xMed YgramrateLeftmY1colors / 255, alpha=1.0)
The effect after running is
And in order to show that divid affects the gradient effect here, we will demonstrate it with several increasing div values.
In divid=2, there is a clear dividing line.
In divid=8, the dividing line is watered down, but it is still vaguely visible.
In divid=50, you can hardly see the dividing line.
Thank you for your reading, the above is the content of "Python how to achieve the horizontal stack map of gradient". After the study of this article, I believe you have a deeper understanding of how to achieve the horizontal stack map of gradient in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.