In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how to draw python line chart with color blocks of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.
Foreword:
If you are reading the article and do not know how to draw a line chart, please go to my home page to see this article on how to draw a line chart using matplotlib, which will show you how to draw a line chart in detail. Back to the point, add a color block to the line chart, which can be regarded as the additional content of the line chart. It can be done by adding a function. The effect image is as follows:
General steps to draw a line chart with color blocks:
Step 1: draw the basic line chart:
Before drawing a color block, we can use the general operation of drawing a line chart to draw a line chart as a basic preparation.
Step 2: calculate the upper and lower bounds corresponding to each coordinate point:
In fact, the color block looks like the lines obtained by translating the coordinate points up and down in the y-axis direction are spliced in the x-axis direction. Before drawing the color block, we need to calculate the upper and lower bounds. This is the same as getting the coordinates of the image point.
# calculate the standard deviation yTop = [y [I] + np.std (data [I]) for i in range (len (data))] yBottom = [y [I]-np.std (data [I]) for i in range (len (data))]
This is the program I wrote to get the upper and lower bound. Of course, the upper and lower boundaries depend on yourself, and the content is self-defined. But it has to be in the list, and the requirements are the same as getting the coordinate points.
Step 3: draw color blocks:
Drawing color blocks is actually very simple, just call the function fill_between.
Plt.fill_between (x, yTop, yBottom, color= "lightgreen", label= "Standard deviation") # fill blocks
The first value of the function corresponds to the x-coordinate of the point, the second value is the upper bound of each point, and the third value is the lower bound of each point. After that, there are some basic attribute settings, needless to say.
Code example:
Import numpy as npimport matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] data = [1, 2, 3, 4, 5, 5, 6], [2, 4, 6, 8, 10, 12], [1, 3, 5, 7, 9, 11], [1.1,2.2,3.3,4.4,5.5,6.6], [3,6,9,12,15] 18], [4, 8, 12, 16, 20, 24], [5, 10, 15, 20, 25, 30], [1.5,2.5,3.5,4.5,5.5,6.5], [1,4,7,10,13,16], [2,5,8,11,14,17] y = [np.mean (I) for i in data] # drawing plt.plot (x Y, linewidth=1, color= "orange", marker= "o", label= "Mean value") # calculate the standard deviation yTop = [y [I] + np.std (Data [I]) for i in range (len (data))] yBottom = [y [I]-np.std (Data [I]) for i in range (len (data))] plt.fill_between (x, yTop, yBottom, color= "lightgreen", label= "Standard deviation") # fill blocks # set horizontal and vertical coordinates plt.xticks ([0,2,4,6) 8, 10, 12]) plt.yticks ([0, 5, 10, 15, 20, 25]) plt.legend (["Mean value", "Standard deviation"], loc= "upper left") # set line identification plt.grid () # set grid mode # set numerical for i in range (10): plt.text (x [I], y [I], y [I], fontsize=12, color= "black", weight= "light", verticalalignment='center' Horizontalalignment='right', rotation=90) plt.show ()
These are all the contents of the article "how to draw a line chart with color blocks in python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.