In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to draw horizontal bar bar chart by python". In daily operation, I believe that many people have doubts about how to draw horizontal bar bar chart by python. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to draw horizontal bar bar chart by python". Next, please follow the editor to study!
The specific code is as follows
Import matplotlibimport randomimport matplotlib.pyplot as plt # Chinese garbled code and axis negative sign processing. Matplotlib.rc ('font', family='SimHei', weight='bold') plt.rcParams [' axes.unicode_minus'] = False # city data City_name = ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Chengdu'] # array inversion. City_name.reverse () # loads random data. Data = [] for i in range (len (city_name)): data.append (random.randint (100,150)) # drawing Fig, ax = plt.subplots () b = ax.barh (range (len (city_name)), data, color='#6699CC') # add data labels to the right side of the horizontal bar chart. For rect in b: W = rect.get_width () ax.text (w, rect.get_y () + rect.get_height () / 2,'% d'% int (w), ha='left', va='center') # sets the tick mark label on the Y axis ordinate. Ax.set_yticks (range (len (city_name)) ax.set_yticklabels (city_name) # do not want the label tag on the X Abscissa. Plt.xticks (()) plt.title ('horizontal bar chart', loc='center', fontsize='25', fontweight='bold', color='red') plt.show ()
Running result:
You can simply draw a horizontal bar chart:
Import matplotlib.pyplot as pltimport randomimport matplotlib matplotlib.rc ('font', family='SimHei', weight='bold') city_name = [' Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Chengdu'] city_name.reverse () data = [] for i in range (len (city_name)): data.append (random.randint (100,200)) colors = ['red',' yellow', 'blue',' green' 'gray'] colors.reverse () plt.barh (range (len (data)), data, tick_label=city_name, color=colors) # No X Abscissa labels. # plt.xticks () plt.show ()
The output is shown in the figure:
At this point, the study on "how to draw a horizontal bar chart by python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.