Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Vertical Bar Chart and horizontal Bar Chart by Python Matplotlib

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve vertical bar chart and horizontal bar chart in Python Matplotlib". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to achieve vertical bar chart and horizontal bar chart in Python Matplotlib" can help you solve the problem.

Bar chart is a commonly used graph, for example, it is loved by various professionals in various PPT presentations. Bar charts can visually show the comparison of numerical values in various scenarios. Matplotlib provides the bar () function to draw bar graphs.

Here is a case study:

Suppose a sales company needs to show the annual sales performance of four employees in PPT, using a bar chart to draw the performance of Xiao Hong, Xiao Wang, Xiao Li and Xiao Zhang, which is 2.6 million yuan, 3 million yuan, 3.1 million yuan and 2.69 million yuan respectively.

Mark some Chinese information in the picture, Matplotlib itself is not very friendly to Chinese support, before drawing, use the following code to set up, you can solve this problem.

1. The vertical bar graph import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.sans-serif'] = [' SimHei'] # is used to display the Chinese label plt.rcParams ['axes.unicode_minus'] = False # and the negative sign member = [u' Xiao Hong', u 'Xiao Wang', u 'Xiao Li', u 'Xiao Zhang'] sales = [260,300,310,269] # drawing plt.bar (range (4), sales, align='center') Color='steelblue', alpha=0.7) # add Y axis label plt.ylabel (u 'annual sales (ten thousand yuan)') # add title plt.title (u 'employee annual sales comparison') # add scale label plt.xticks (range (4), member) # set the scale range of Y axis plt.ylim ([200,350]) # add numeric labels for x, y in enumerate (sales): plt.text (x) above no bar chart Y round 10,'% s'% round (y, 1), ha='center') #% s, which means that an object is formatted as the character # object (x [, n]) # x-numeric expression. # n-A numeric expression that represents the number of places from the decimal point. Plt.show ()

The bar () function here sets the x scale label to be centered horizontally, the bar chart is filled with iron blue, and the transparency is set to 0.7.

2. Horizontal bar chart

To change a vertical bar chart to a horizontal bar chart, you only need to adjust the bar () function to the barh () function and adjust the corresponding label.

Import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.sans-serif'] = [' SimHei'] # to normally display the Chinese label plt.rcParams ['axes.unicode_minus'] = False # to display the negative sign member = [u' Xiao Hong', u 'Xiao Wang', u 'Xiao Li', u 'Xiao Zhang'] sales = [260,300,310,269] # drawing plt.barh (range (4), sales, align='center', color='steelblue' Alpha=0.7) # add Y axis label plt.xlabel (u 'annual sales (ten thousand yuan)') # add title plt.title (u 'employee annual sales comparison') # add scale label plt.yticks (range (4), member) # set the scale range of Y axis plt.xlim ([200,350]) # add numeric labels for x, y in enumerate (sales): plt.text (yearly 10, x) above no bar chart '% s'% y, va='center') #% s, which means to format an object to the character plt.show ()

This is the end of the introduction of "how to achieve vertical bar chart and horizontal bar chart in Python Matplotlib". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report