How Python uses matplotlib to draw a stack-histogram tutorial

Source: Internet
Author: User
This article mainly introduces Python based on Matplotlib to draw a stack histogram method, involving Python using matplotlib for graphic drawing of the relevant operation skills, the need for friends can refer to the following

In this paper, we describe the method of Python-based matplotlib to draw a stack histogram. Share to everyone for your reference, as follows:

Usually we do only a set of data histogram statistics, so we just draw a histogram directly can be.

But sometimes we also draw a histogram of multiple sets of data (for example, when I was a freshman to a university city inner ring time distribution), freshman to senior with different colors of the histogram, displayed in a picture, this will be very intuitive.


#!/usr/bin/env python#-*-coding:utf-8-*-#http://www.jb51.net/article/100363.htm# numpy Array intorduction#http:// Matplotlib.org/examples/statistics/histogram_demo_multihist.htmlimport NumPy as Npimport Pylab as Pimport  Matplotlibd1=np.array ([18.46,19.15,18.13, 18.30, 18.07, 18.24, 18.26, 17.14, 18.44, 18.06, 17.44, 16.57, 16.34, 17.21 ]) d1=d1//1+ (D1-D1//1)/0.6d2=np.array ([19.33, 19.06, 18.10, 17.55, 19.55, 19.13, 18.54, 18.30, 18.36, 19.59, 20.01, 1 9.17, 19.30, 18.54, 18.35, 20.04]) d2=d2//1+ (D2-D2//1)/0.6d3=np.array ([20.52, 20.41, 19.20, 19.04, 19.09, 19.01, 17.49, 19 .20.01, 20.11]) d3=d3//1+ (D3-D3//1)/0.6d4=np.array ([22.02, 21.03,21.06, 20.46, 19.46, 20.15, 19.49, 19.43, 19.5 1, 19.39, 19.33, 19.18, 19.13, 19.22, 18.46, 19.07, 18.57, 18.45, 19.17, 18.41, 18.30]) d4=d4//1+ (D4-D4//1)/0.6x= ([ D1,D2,D3,D4]) p.figure () #normed is False is GOODN, bins, patches = p.hist (x, V, [16.5, 22.5],normed=0, histtype= ' Barstacke d ', color=[' blue ', ' green ', ' red', ' yellow '], label=[', ', ', ', ']) print type (x) p.legend () #legend should is signed after set down The Informationp.show ()

For example, it is clear that the blue histogram (freshman) runs fastest and the yellow (senior) histogram runs the slowest.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.