Home  >  Article  >  Backend Development  >  Detailed tutorial on how to use matplotlib to draw stacked histograms in Python

Detailed tutorial on how to use matplotlib to draw stacked histograms in Python

巴扎黑
巴扎黑Original
2017-08-15 15:06:511984browse

This article mainly introduces the method of drawing stacked histograms in Python based on matplotlib, and involves the related operating skills of Python using matplotlib for graphics drawing. Friends in need can refer to it

The example of this article tells the Python based Matplotlib's method for drawing stacked histograms. Share it with everyone for your reference, the details are as follows:

Usually we only do histogram statistics on a set of data, so we only need to draw the histogram directly.

But sometimes we draw histograms of multiple sets of data at the same time (for example, the distribution of the time it took me to run the inner ring of the university town from my freshman to senior years), and use histograms of different colors from my freshman to senior years. Displayed on a picture, it 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.html
import numpy as np
import pylab as P
import matplotlib
d1=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.6
d2=np.array([19.33 ,19.06 ,18.10 ,17.55 ,19.55 ,19.13 ,18.54 ,
     18.30 ,18.36 ,19.59 ,20.01 ,19.17 ,19.30 ,18.54 ,18.35 ,20.04 ])
d2=d2//1+(d2-d2//1)/0.6
d3=np.array([20.52 ,20.41 ,19.20 ,19.04 ,19.09 ,19.01 ,17.49 ,19.18 ,20.01 ,20.11 ])
d3=d3//1+(d3-d3//1)/0.6
d4=np.array([22.02 ,21.03,21.06 ,20.46 ,19.46 ,20.15 ,19.49 ,19.43 ,
       19.51 ,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.6
x=([d1,d2,d3,d4])
P.figure()
#normed is False is good
n, bins, patches = P.hist(x, 12, [16.5, 22.5],normed=0, histtype='barstacked',
              color=['blue', 'green', 'red','yellow'],
             label=['   ', '   ', '   ','   '])
print type(x)
P.legend()#legend should be signed after set down the information
P.show()

Take the above picture as an example. It is obvious that the blue histogram (big one) runs the fastest, and the yellow (big one) 4) Histogram runs the slowest.

The above is the detailed content of Detailed tutorial on how to use matplotlib to draw stacked histograms in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn