1.首先要繪制一個簡單的條形圖
1
2
3
4
5
6
7
8
|
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = 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)這句代碼是最重要的,其中left表示直方圖的開始的位置(也就是最左邊的地方),height是指直方圖的高度,當直方圖太粗時,可以通過width來定義直方圖的寬度,注意多個直方圖要用元組,yerr這個參數是防止直方圖觸頂。
2.增加直方圖腳注
1
2
3
4
5
6
7
8
9
|
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = 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.條形圖上顯示具體的數字(自動編號)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = 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.改變顏色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = 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.添加圖注
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = 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() |
6大家根據自己的需要自己來繪制自己的條形圖
下面回答網友提問,如何畫在條形圖上垂直顯示數據:
下面這個函數是用來垂直顯示的,其中設置角度就可以以任意方式來顯示。
1
2
3
4
|
def autolabel(rects,Num = 1.12 ,rotation1 = 90 ,NN = 1 ): for rect in rects: height = rect.get_height() plt.text(rect.get_x() - 0.04 + rect.get_width() / 2. , Num * height, '%s' % float (height * NN),rotation = rotation1) |
調用方式如下
1
2
|
rects1 = plt.bar(left = ( 0.05 ),height = (Pe_FH),color = ( 'b' ),label = ( 'FHMM' ),width = 0.1 ,align = "center" ,yerr = 0.000001 ); autolabel(rects1, 1.09 ); |
下面是效果圖
總結
以上就是本文關于python繪制條形圖方法代碼詳解的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/yywan1314520/article/details/50818471