Python matplotlib (2D graphics library) 2

plt.hist :( frequency distribution) histogram

Difference histogram bar graph
(1) bar graph (histogram) with an elongated represent each category represents the length of the elongated frequency category indicating the type represents a width.
(2) is a histogram showing the number by the area of the height, the number of rectangular frequency groups or frequency represents the frequency, the width of each group, said group from each group, so its meaning both height and width. When the same width, generally represented by the frequency elongated length.
(3) bar graph (histogram) showing mainly for discrete distribution of data, are mainly used for the distribution of the histogram display data continuity. Intuitively, the histogram is the interface between each elongated together, continuously arranged, showing the mathematical relationship between the transactions; left bar graph (histogram) between the elongate voids arranged separately, to distinguish between different classes.

the difference Frequency distribution histogram Bar chart (histogram)
Data on the horizontal axis Continuous, is a range of Isolated, on behalf of a class
Between the elongated There is no gap There are gaps
Frequency representation Represented generally by an elongated area; when the same width, the length represents Elongated length

函数
plt.hist(x,bins=None,range=None, density=None, bottom=None, histtype=‘bar’, align=‘mid’, log=False, color=None, label=None, stacked=False, normed=None)

key parameter

  • x: data collection, data sets will eventually histogram statistics
  • bins: The more the number painted histogram, the number, the more closely with the bar.
  • range: tuple, the display section
  • density: bool, the default is False, displays the frequency statistics for True frequency statistics, however, shows note here, the frequency statistics = number / (total * interval width) interval, and normed effect is the same, the official recommended density
  • histtype: Optional { 'bar', 'barstacked', 'step', 'stepfilled'} one default named bar, then recommended to use the default configuration, step using a ladder, 'stepfilled' represents a black border bars removed
  • align: Optional { 'left', 'mid', 'right'} one, the default is 'mid', the control level of the histogram distribution, left or right, there will be some blank areas, it is recommended to use the default
  • log: bool, default False, i.e. whether to select the index y axis scale
  • stacked: bool, the default is False, whether stacked like map
  • normed = 1 represents the frequency of appearance were normalized. normed = 0, compared with the frequency; has been substituted density.

return value

  • n: array or list of arrays (box value)
  • bins: array (boundary box)
  • patches : list or list of lists

Example 1 :

import matplotlib.pyplot as plt
import numpy as np
import matplotlib

# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei']   # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False     # 正常显示负号
# 随机生成(10000,)服从正态分布的数据
data = np.random.randn(10000)
#density=False:频数,density=True:频率
plt.hist(data, bins=40, density=True, facecolor="blue", edgecolor="black", alpha=0.7)
# 显示横轴标签
plt.xlabel("区间")
# 显示纵轴标签
plt.ylabel("频数/频率")
# 显示图标题
plt.title("频数/频率分布直方图")
plt.show()

Example 2:
connecting the center of the histogram

import matplotlib.pyplot as plt
import numpy as np

x=np.random.randint(0,100,100)#生成【0-100】之间的100个数据,即 数据集
bins=np.arange(0,101,10)#设置连续的边界值,即直方图的分布区间[0,10],[10,20]...
width=10#柱状图的宽度
#直方图会进行统计各个区间的数值
frequency_each,_,_= plt.hist(x,bins,color='deepskyblue',width=width,alpha=0.7)#alpha设置透明度,0为完全透明
plt.xlabel('scores')
plt.ylabel('count')
plt.xlim(0,100)#设置x轴分布范围
plt.plot(bins[1:]-(width//2),frequency_each,color='palevioletred')#利用返回值来绘制区间中点连线
plt.show()

Example 3:

import  numpy as np
import matplotlib.pyplot as plt
#概率分布直方图
#高斯分布
#均值为0
mean = 0
#标准差为1,反应数据集中还是分散的值
sigma = 1
x=mean+sigma*np.random.randn(10000)
fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6))
#第二个参数是柱子宽一些还是窄一些,越大越窄越密
ax0.hist(x,40,histtype='bar',facecolor='yellowgreen',alpha=0.75)
##pdf概率分布图,一万个数落在某个区间内的数有多少个
ax0.set_title('pdf')
ax1.hist(x,20,histtype='bar',facecolor='pink',alpha=0.75,cumulative=True,rwidth=0.8)
#cdf累计概率函数,cumulative累计。比如需要统计小于5的数的概率
ax1.set_title("cdf")
fig.subplots_adjust(hspace=0.4)
plt.show()

Example 4:

import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt

sampleNo = 1000;
# 一维正态分布
# 下面三种方式是等效的
mu = 3
sigma = 0.1
np.random.seed(0)
s = np.random.normal(mu, sigma, sampleNo )
#s = np.random.rand(1, sampleNo )
plt.subplot(141)
plt.hist(s, 10, density=True)   #####bins=10

np.random.seed(0)
s = sigma * np.random.randn(sampleNo ) + mu
plt.subplot(142)
plt.hist(s, 30, density=True)   #####bins=30

np.random.seed(0)
s = sigma * np.random.standard_normal(sampleNo ) + mu
plt.subplot(143)
plt.hist(s, 30, density=True)   #####bins=30

# 二维正态分布
mu = np.array([[1, 5]])
Sigma = np.array([[1, 0.5], [1.5, 3]])
R = cholesky(Sigma)
s = np.dot(np.random.randn(sampleNo, 2), R) + mu
plt.subplot(144)
# 注意绘制的是散点图,而不是直方图
plt.plot(s[:,0],s[:,1],'+')
plt.show()

plt.bar: bar (statistical) FIG / Histogram

function

matplotlib.pyplot.bar (x, height, width = 0.8, bottom = None, align = 'center', data = None, kwargs *)
generated vertical bar graph
matplotlib.pyplot.barh () generated in the horizontal direction of the article FIG shaped

Important parameters

  • x: Scalar sequence. It indicates the x coordinate of the bar graph.
  • height: scalar or series of scalars. Height of the bar.
  • width: scalar or array, an optional parameter. Strip width between 0 and 1, the default is: 0.8.
  • bottom: scalar or array, an optional parameter. y coordinate of the bottom of the bar, the default value None. When the set value, the lower section is empty
  • color: scalar or array, an optional parameter. Bar foreground color.
  • edgecolor: scalar or array, an optional parameter. Bar border color.
  • linewidth:标量或者数组,可选参数。条形图边界宽度。如果为None,使用默认linewidth;如果为0,不画边界。默认为None。
  • tick_label:字符串或者数组,可选参数。条形图的tick标记,默认为None。
  • xerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。
  • yerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。
  • ecolor:标量或者数组,可选参数。指定errorbars的颜色,默认为None。
  • capsize:标量,可选参数。确定errorbars上限的长度,默认为None,从errorbar.capsize rcParam获取到值。
  • error_kw:字典类型,可选参数。kwags参数被传给errorbar方法。ecolor和capsize可能在这被指定而不是作为一个单独的kwargs。
  • align:{‘center’,‘edge’},可选参数,默认:‘center’。 如果是’edge’,通过左边界(条形图垂直)和底边界(条形图水平)来使条形图对齐。如果是’center’,将left参数解释为条形图中心坐标。通过传递一个给width设置复数,来使条形图以右边界进行对齐。
  • orientation:{‘vertical’,‘horizontal’},可选参数。设置条形图方向。
  • log:布尔类型,可选参数。如果为true,设置轴到log scale。默认为False。

返回值
bars:matplotlib.container.BarContainer。
带有所有bar与errorbar的容器。

示例1

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

X = [0, 1, 2, 3, 4, 5]
Y = [222, 42, 455, 664, 454, 334]
fig = plt.figure()
#plt.bar为我们创建条形图
plt.bar([0,3,5,7,9],[5,2,7,8,2], label="Example one",color='r')  #红色

plt.bar([2,4,6,8,10],[8,6,2,5,6], label="Example two", color='g') #绿色
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')
plt.title('Epic Graph\nAnother Line! Whoa')
plt.show()

plt.stackplot:堆叠图

堆叠图用于显示『部分对整体』随时间的关系。 堆叠图基本上类似于饼图,只是随时间而变化。
函数
plt.stackplot(x,* args,** kwargs )

重要参数

  • “x”:array,维度为n的一维数组
  • “y”:array,2d数组(维度MxN)或1d数组的序列(每维1xN)
stackplot(x, y)    # where y is MxN(2d array of dimension MxN)  
stackplot(x, y1, y2, y3, y4)  # where y1, y2, y3, y4, are all 1xNm  
stackplot(x, y1, y2, y3, y4, labels=[], colors=[]) 
  • “baseline “:[‘zero’ , ‘sym’ ,‘wiggle’ , ‘weighted_wiggle’],计算基线的方法
  • “zero”:function,恒定的零基线,即一个简单的堆积图。
  • “sym”:function,围绕零对称,有时称为“ThemeRiver”
  • “wiggle”:function,最小化平方斜率的总和
  • “weighted_wiggle”:function,是否相同,但权重来说明每个图层的大小。它也被称为“Streamgraph”版式。
  • “labels “:string,要分配给每个数据系列的标签。
  • “colors “,string,颜色的列表或元组。这些将循环并用于对堆叠区域进行着色。

示例1:
考虑一个情况,一天有24小时,看看我们如何花费时间。 将我们的活动分为:睡觉,吃饭,工作和玩耍。假设我们要在5天的时间内跟踪它:

import matplotlib.pyplot as plt
days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]


plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.plot([],[],color='c', label='Eating', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()

plt.pie:饼图

函数
pie(x, explode=None, labels=None……)

重要参数

  • x:为一个存放各部分占比的向量;
  • labels :list,(每一块)饼图外侧显示的说明文字;
  • explode :list,(每一块)离开中心距离;
  • colors:list,设置为各部分染色列表,元素一一对应
  • startangle :起始绘制角度,默认图是从x轴正方向逆时针画起,如设定=90则从y轴正方向画起;
  • shadow :在饼图下面画一个阴影。默认值:False,即不画阴影;
  • labeldistance:label标记的绘制位置,相对于半径的比例,默认值为1.1,如<1则绘制在饼图内侧;
  • autopct :list,设置饼图内百分比显示,可以使用format字符串或者format function '%1.1f’指小数点前后位数(没有用空格补齐);
  • pctdistance:类似于labeldistance,指定autopct的位置刻度,默认值为0.6;
  • radius :控制饼图半径,默认值为1;
  • counterclock:指定指针方向;布尔值,可选参数,默认为:True,即逆时针。将值改为False即可改为顺时针。
  • wedgeprops :字典类型,可选参数,默认值:None。参数字典传递给wedge对象用来画一个饼图。例如:wedgeprops={‘linewidth’:3}设置wedge线宽为3。
  • textprops :设置标签(labels)和比例文字的格式;字典类型,可选参数,默认值为:None。传递给text对象的字典参数。
  • center :浮点类型的列表,可选参数,默认值:(0,0)。图标中心位置。
  • frame :布尔类型,可选参数,默认值:False。如果是true,绘制带有表的轴框架。
  • rotatelabels:布尔类型,可选参数,默认为:False。如果为True,旋转每个label到指定的角度。

注意
(1)pie函数默认绘制形状为椭圆形,需自行设置坐标轴模式(plt.axis(‘equal’))来显示为圆形;
(2)pie()绘制顺序默认从x正半轴开始逆时针方向,当x向量所有元素之和小于1时,画图会正常进行,但饼图会有缺口;

示例1

import matplotlib.pyplot as plt
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']

plt.pie(slices,
        labels=activities,
        colors=cols,
        startangle=90,
        shadow= True,
        explode=(0,0.1,0,0),
        autopct='%1.1f%%')
#0.1表示将eating那一块凸显出来

plt.title('Interesting Graph\nCheck it out')
plt.show()

(1)可以选择指定图形的『起始角度』,在例1中,startangle=90 表示为饼图选择了90度角,这意味着第一个部分是一个竖直线条。
(2)explode表示(每一块)离开中心距离,如果不想拉出任何切片,传入0,0,0,0。explode=(0,0.1,0,0)表示第二块离开中心0.1。
(3)autopct,选择将百分比放置到图表上面,这里使用format字符串,’%1.1f%%'表示小数点后一位。

示例2

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']#将图表内容字体设置为黑体,可以正常显示中文

ratios=[0.1,0.1,0.15,0.15,0.4]#存放比例列表
colors=['peru','coral','salmon','yellow','grey']#存放颜色列表,与比例相匹配
labels=["流行",'classic','pop','纯音乐','blue']#存放各类元素标签
explode=(0,0.1,0,0,0.08)

plt.pie(ratios,explode=explode,colors=colors,labels=labels)#绘制饼图
plt.title('歌单音乐种类百分比')
plt.axis('equal')#将饼图显示为正圆形
plt.show()

Guess you like

Origin blog.csdn.net/yyl424525/article/details/92721739