没有合适的资源?快使用搜索试试~ 我知道了~
首页使用Python matplotlib作图时,设置横纵坐标轴数值以百分比(%)显示
一、当我们用Python matplot时作图时,一些数据需要以百分比显示,以更方便地对比模型的性能提升百分比。 二、借助matplotlib.ticker.FuncFormatter(),将坐标轴格式化。 例子: # encoding=utf-8 import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter plt.rcParams['font.family'] = ['Times New Roman'] plt.rcParams.update({'font.size': 8}) x = range(
资源详情
资源评论
资源推荐

使用使用Python matplotlib作图时作图时,设置横纵坐标轴数值以百分比设置横纵坐标轴数值以百分比
(%)显示显示
一、当我们用Python matplot时作图时,一些数据需要以百分比显示,以更方便地对比模型的性能提升百分比。
二、借助matplotlib.ticker.FuncFormatter(),将坐标轴格式化。
例子:
# encoding=utf-8
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.rcParams['font.family'] = ['Times New Roman'] plt.rcParams.update({'font.size': 8})
x = range(11)
y = range(11)
plt.plot(x, y)
plt.show()
图形显示如下:
现在我们将横纵坐标变成百分比形式即,0%,20%,40%….代码如下:
# encoding=utf-8
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.rcParams['font.family'] = ['Times New Roman'] plt.rcParams.update({'font.size': 8})
x = range(11)
y = range(11)
plt.plot(x, y)
def to_percent(temp, position):
return '%1.0f'%(10*temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
plt.gca().xaxis.set_major_formatter(FuncFormatter(to_percent))
plt.show()
即增加了10~13的代码,执行结果如下:
可见已经实现我们的需求。
重要代码
return ‘%1.0f’%(10*temp) + ‘%’ #这句话指定了显示的格式。
更多格式化显示,可以查看matplotlib.ticker。
补充知识:补充知识:matplotlib画图系列之设置坐标轴(精度、范围,标签,中文字符显示)画图系列之设置坐标轴(精度、范围,标签,中文字符显示)
在使用matplotlib模块时画坐标图时,往往需要对坐标轴设置很多参数,这些参数包括横纵坐标轴范围、坐标轴刻度大小、坐
标轴名称等
在matplotlib中包含了很多函数,用来对这些参数进行设置。
plt.xlim、plt.ylim 设置横纵坐标轴范围
plt.xlabel、plt.ylabel 设置坐标轴名称
plt.xticks、plt.yticks设置坐标轴刻度
以上plt表示matplotlib.pyplot
例子
#导入包
import matplotlib.pyplot as plt
import numpy as np
#支持中文显示
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']




















weixin_38500047
- 粉丝: 10
- 资源: 979
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
最新资源
- ARM Cortex-A(armV7)编程手册V4.0.pdf
- ABB机器人保养总结解析.ppt
- 【超详细图解】菜鸡如何理解双向链表的python代码实现
- 常用网络命令的使用 ipconfig ping ARP FTP Netstat Route Tftp Tracert Telnet nslookup
- 基于单片机控制的DC-DC变换电路
- RS-232接口电路的ESD保护.pdf
- linux下用time(NULL)函数和localtime()获取当前时间的方法
- Openstack用户使用手册.docx
- KUKA KR 30 hA,KR 60 hA机器人产品手册.pdf
- Java programming with JNI
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论0