国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 十堰市| 昂仁县| 马山县| 彭泽县| 清水河县| 大庆市| 洪泽县| 五大连池市| 大关县| 咸阳市| 台山市| 玉林市| 龙陵县| 宁夏| 呼伦贝尔市| 台北市| 光泽县| 嘉黎县| 奎屯市| 五华县| 四会市| 三门峡市| 菏泽市| 张家港市| 绍兴县| 唐河县| 靖西县| 稷山县| 紫阳县| 昌黎县| 米林县| 安龙县| 桐庐县| 东海县| 溧水县| 阜城县| 萨迦县| 吕梁市| 怀安县| 临朐县| 望都县|