最近在畫圖時候發現我居然不會把分數轉爲百分比!what?主要是之前也沒有過這個需求,anyway,百度一下就有了,這裏當記錄!
1.百分比轉爲小數,這裏默認百分比是字符串
# -*- coding: utf-8 -*-
s = '20%' # 默認要轉換的百分比是字符串
aa = float(s.strip('%')) # 去掉s 字符串中的 %
bb = aa/100.0
print(bb)
# 輸出結果是 0.2
2.小數轉爲百分比 ----- "%.2f%%" % (a * 100)
#方法一
a = 0.3214323
bb = "%.2f%%" % (a * 100)
print(bb)
# 輸出結果是32.14%
#方法二
a = 0.3214323
b = str(a*100) + '%'
print(b)
# 輸出結果是32.14323%
用在matplotlib畫圖中
plt.figure(figsize=(15,7))
plt.plot(up_move['各月份上漲概率'])
for a,b in zip(up_move.index,up_move['各月份上漲概率']):
print(b)
plt.text(a, b+0.01, '%.3f' %b, ha='center', va= 'bottom',fontsize=20)
#plt.text(a, b+0.01, '%.3f%%' %(b*100), ha='center', va= 'bottom',fontsize=20)
plt.ylim(0.2,0.9)
plt.show()
plt.figure(figsize=(15,7))
plt.plot(up_move['各月份上漲概率'])
for a,b in zip(up_move.index,up_move['各月份上漲概率']):
print(b)
#plt.text(a, b+0.01, '%.3f' %b, ha='center', va= 'bottom',fontsize=20)
plt.text(a, b+0.01, '%.3f%%' %(b*100), ha='center', va= 'bottom',fontsize=20)
plt.ylim(0.2,0.9)
plt.show()