Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to create a histogram in Matplotlib

Shulou Source: shulou.com Published: 2022-06-01 18:37:51 10月02日 Update

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.

Tags: Age histogram number of people content article Zhongchuang interviewees charts data files more boxes different good practical above between garbled code location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi MySQL Linux Docker