Python matplotlib作图时,设置横纵坐标轴数值以百分比(%)显示

reference: https://blog.csdn.net/u014712482/article/details/80571938

一、当我们用Python matplot时作图时,一些数据需要以百分比显示,以更方便地对比模型的性能提升百分比。

二、借助matplotlib.ticker.FuncFormatter(),将坐标轴格式化。

例子:


 
 
  1. # encoding=utf-8
  2. import matplotlib.pyplot as plt
  3. from matplotlib.ticker import FuncFormatter
  4. plt.rcParams[ ‘font.family’] = [ ‘Times New Roman’]
  5. plt.rcParams.update({ ‘font.size’: 8})
  6. x = range( 11)
  7. y = range( 11)
  8. plt.plot(x, y)
  9. plt.show()

图形显示如下:


现在我们将横纵坐标变成百分比形式即,0%,20%,40%…代码如下:


 
 
  1. # encoding=utf-8
  2. import matplotlib.pyplot as plt 
  3. from matplotlib.ticker import FuncFormatter
  4. plt.rcParams[ ‘font.family’] = [ ‘Times New Roman’]
  5. plt.rcParams.update({ ‘font.size’: 8}) 
  6. x = range( 11)
  7. y = range( 11)
  8. plt.plot(x, y)
  9. def to_percent(temp, position):
  10.     return ‘%1.0f’%( 10 temp) + '%'
  11. plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
  12. plt.gca().xaxis.set_major_formatter(FuncFormatter(to_percent))
  13. plt.show()

即增加了10~13的代码,执行结果如下:


可见已经实现我们的需求。

重要代码

return ‘%1.0f’%(10temp) + ‘%’    #这句话指定了显示的格式。

更多格式化显示,可以查看matplotlib.ticker



参考文献

1. http://sofasofa.io/forum_main_post.php?postid=1000816

2. http://www.zeuux.com/group/scipython/bbs/content/53419/

3. https://blog.csdn.net/pipisorry/article/details/37742423



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值