国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python繪制條形圖方法代碼詳解

2020-02-16 11:10:02
字體:
供稿:網(wǎng)友

1.首先要繪制一個簡單的條形圖

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import mlabfrom matplotlib import rcParamsfig1 = plt.figure(2)rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001)plt.title('Pe')plt.show()

1.1上面中rects=plt.bar(left=(0.2,1),height=(1,0.5),width=0.2,align=”center”,yerr=0.000001)這句代碼是最重要的,其中l(wèi)eft表示直方圖的開始的位置(也就是最左邊的地方),height是指直方圖的高度,當(dāng)直方圖太粗時,可以通過width來定義直方圖的寬度,注意多個直方圖要用元組,yerr這個參數(shù)是防止直方圖觸頂。

2.增加直方圖腳注

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import mlabfrom matplotlib import rcParamsfig1 = plt.figure(2)rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001)plt.title('Pe')plt.xticks((0.2,1),('frst','second'))plt.show()

3.條形圖上顯示具體的數(shù)字(自動編號)

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import mlabfrom matplotlib import rcParamsfig1 = plt.figure(2)rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001)plt.title('Pe')def autolabel(rects):  for rect in rects:    height = rect.get_height()    plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))autolabel(rects)plt.xticks((0.2,1),('frst','second'))plt.show()

4.改變顏色

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import mlabfrom matplotlib import rcParamsfig1 = plt.figure(2)rects =plt.bar(left = (0.2,1),height = (1,0.5),color=('r','g'),width = 0.2,align="center",yerr=0.000001)plt.title('Pe')def autolabel(rects):  for rect in rects:    height = rect.get_height()    plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))autolabel(rects)plt.xticks((0.2,1),('frst','second'))plt.show()

5.添加圖注

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import mlabfrom matplotlib import rcParamsfig1 = plt.figure(2)rects1 =plt.bar(left = (0.2),height = (0.5),color=('g'),label=(('no1')),width = 0.2,align="center",yerr=0.000001)rects2 =plt.bar(left = (1),height = (1),color=('r'),label=(('no2')),width = 0.2,align="center",yerr=0.000001)plt.legend()plt.xticks((0.2,1),('frst','second'))plt.title('Pe')def autolabel(rects):  for rect in rects:    height = rect.get_height()    plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))autolabel(rects1)autolabel(rects2)plt.show()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 西贡区| 泰和县| 沙坪坝区| 图片| 铜梁县| 项城市| 确山县| 蕲春县| 靖远县| 平南县| 泗阳县| 银川市| 涪陵区| 中西区| 镇远县| 青浦区| 西华县| 日土县| 随州市| 田阳县| 大关县| 庆阳市| 江达县| 天门市| 黔江区| 巨野县| 安平县| 墨江| 武汉市| 汽车| 阳朔县| 武陟县| 行唐县| 西乡县| 邯郸市| 林芝县| 青神县| 开远市| 沙坪坝区| 上饶县| 祁阳县|