最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python matplotlib作圖時,設置橫縱坐標軸數(shù)值以百分比(%)顯示

 更新時間:2020年05月16日 10:23:50   作者:Rainpages  
這篇文章主要介紹了使用Python matplotlib作圖時,設置橫縱坐標軸數(shù)值以百分比(%)顯示,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、當我們用Python matplot時作圖時,一些數(shù)據(jù)需要以百分比顯示,以更方便地對比模型的性能提升百分比。

二、借助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()

圖形顯示如下:

現(xiàn)在我們將橫縱坐標變成百分比形式即,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的代碼,執(zhí)行結果如下:

可見已經(jīng)實現(xiàn)我們的需求。

重要代碼

return '%1.0f'%(10*temp) + '%' #這句話指定了顯示的格式。

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

補充知識:matplotlib畫圖系列之設置坐標軸(精度、范圍,標簽,中文字符顯示)

在使用matplotlib模塊時畫坐標圖時,往往需要對坐標軸設置很多參數(shù),這些參數(shù)包括橫縱坐標軸范圍、坐標軸刻度大小、坐標軸名稱等

在matplotlib中包含了很多函數(shù),用來對這些參數(shù)進行設置。

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']
 
#創(chuàng)建數(shù)據(jù)
x = np.linspace(-5, 5, 100)
y1 = np.sin(x)
y2 = np.cos(x)
 
#創(chuàng)建figure窗口
plt.figure(num=3, figsize=(8, 5))
#畫曲線1
plt.plot(x, y1)
#畫曲線2
plt.plot(x, y2, color='blue', linewidth=5.0, linestyle='--')
#設置坐標軸范圍
plt.xlim((-5, 5))
plt.ylim((-2, 2))
#設置坐標軸名稱
plt.xlabel('xxxxxxxxxxx')
plt.ylabel('yyyyyyyyyyy')
#設置坐標軸刻度
my_x_ticks = np.arange(-5, 5, 0.5)
my_y_ticks = np.arange(-2, 2, 0.3)
plt.xticks(my_x_ticks)
plt.yticks(my_y_ticks)
 
#顯示出所有設置
plt.show()

結果

以上這篇使用Python matplotlib作圖時,設置橫縱坐標軸數(shù)值以百分比(%)顯示就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

瑞金市| 宿迁市| 新绛县| 清新县| 东山县| 和田县| 永登县| 静乐县| 武城县| 施秉县| 特克斯县| 三穗县| 定日县| 紫云| 望都县| 延安市| 汝州市| 西青区| 盐边县| 广平县| 无棣县| 新化县| 磴口县| 遂平县| 永福县| 嘉义市| 盘锦市| 烟台市| 寿光市| 麟游县| 嘉义县| 京山县| 衡阳市| 建始县| 尉氏县| 溧水县| 苏尼特左旗| 鲁甸县| 巴里| 云安县| 郑州市|