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 create a histogram in Matplotlib

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

Share

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

This article will explain in detail how to create a histogram in Matplotlib. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Getting started example

Let's take a look at an example: read a data.csv file to count the information distribution of all interviewees in different age groups, and specify a location with a middle-aged age of 29 to display it. The csv file contains a total of 79211 pieces of data:

The above figure is mismatched. The specific code is as follows:

Import pandas as pdfrom matplotlib import pyplot as pltfrom matplotlib import rcParams# sets chart font Prevent Chinese garbled rcParams ['font.family'] =' Microsoft YaHei'rcParams ['font.sans-serif'] =' Microsoft YaHei'# Chart style plt.style.use ('fivethirtyeight') # read data data = pd.read_csv (' data.csv') ids = data ['Responder_id'] ages = data [' Age'] # define box segment list bins = [10, 20, 30, 40, 50, 60, 70, 80, 90,100]

# Construction histogram # Color of edges connected between each box # y axis number shows logplt.hist (ages,bins,edgecolor='white',log=True) # define middle age median_age = 29color ='# fc4f30'#axvline () add vertical lines to the axis. Plt.axvline (median_age, color=color, label=' middle age', linewidth=2)

Plt.legend () plt.title ('age distribution') plt.xlabel ('age') plt.ylabel ('total number of respondents')

Plt.tight_layout ()

Plt.show ()

Running result:

This is the end of the article on "how to create a histogram in 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, please 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.

Share To

Internet Technology

Wechat

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

12
Report