In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Python Matplotlib to draw bar graphs. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Bar chart
Bar charts have rich forms of expression, including single group bar chart, multi-group bar chart, stacked bar chart, symmetrical bar chart and so on.
Single group bar chart
Each form of bar chart can be drawn as a vertical bar chart or a horizontal bar chart. Take the two drawing methods of a single group of bar charts as an example.
Vertical bar chart
Import matplotlib.pyplot as pltdata = [10.,20.,5.5,15.] plt.bar (range (len (data)), data) plt.show ()
The Tips:plt.plot () function takes in two parameters, including the x coordinate of each bar and the height of each bar.
The optional parameter width,pyplot.bar () provides a way to control the bar width in a bar chart:
Import matplotlib.pyplot as pltdata = [10.,20.,5.15.] plt.bar (range (len (data)), data, width=0.5) plt.show ()
Horizontal bar chart
If you prefer the horizontal bar appearance, you can use the plt.barh () function, which is basically the same as plt.bar () in usage, but changing the parameters of the bar width (or height in the horizontal bar chart) requires the use of height:
Import matplotlib.pyplot as pltdata = [10.,20.,5.15.] plt.barh (range (len (data)), data, height=0.5) plt.show ()
Multiple groups of bar graphs
We may need multiple sets of bar charts when we need to compare such demand as sales in the corresponding quarters in different years.
Import numpy as npimport matplotlib.pyplot as pltdata = [10.20.30.20.], [40.,25.,53.18.], [6.22.52.19.] x = np.arange (4) plt.bar (x + 0.00, data [0], color = 'baked, width = 0.25) plt.bar (x + 0.25, data [1], color =' g é g é, width = 0.25) plt.bar (x + 0.50, data [2]) Color = 'ritual, width = 0.25) plt.show ()
Stacked bar chart
You can draw a stacked bar chart by using the optional parameters in the plt.bar () function.
Import matplotlib.pyplot as plty_1 = [3,25.,45.22.] plt.show 2 = [6.25.50.25.] x = range (4) plt.bar (x, yellow1, color ='b') plt.bar (x, yellow2, color = 'ringing, bottom = yellow1) plt.show ()
The optional parameter bottom of the Tips:plt.bar () function allows you to specify the starting value of the bar chart.
You can stack more bars using the delayed rendering mechanism in conjunction with the for loop:
Import numpy as npimport matplotlib.pyplot as pltdata = np.array ([[5.30.45.22.], [5.25.50.20.], [1.1.2.1.1.]]) x = np.arange (data.shape [1]) for i in range (data.shape [0]): plt.bar (x, data [I], bottom = np.sum (data [: I], axis = 0)) plt.show ()
Symmetrical bar graph
A simple and useful technique is to draw two bar graphs symmetrically. For example, you want to draw a comparison between the number of men and women of different ages:
Import numpy as npimport matplotlib.pyplot as pltw_pop = np.array ([5.30.45.22.]) m_pop = np.array ([5.25.50.20.]) x = np.arange (4) plt.barh (x, w_pop) plt.barh (x,-m_pop) plt.show ()
The bar chart of the female population in the picture is drawn as usual. However, the bar chart of the male population extends to the left, not to the right. You can use the negative value of the data to quickly realize the drawing of the symmetrical bar chart.
Thank you for reading! This is the end of the article on "how to draw a bar chart with Python Matplotlib". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.