久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:132日期:2022-07-25 13:29:51

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

二、借助matplotlib.ticker.FuncFormatter(),將坐標(biāo)軸格式化。

例子:

# encoding=utf-8import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatterplt.rcParams[’font.family’] = [’Times New Roman’]plt.rcParams.update({’font.size’: 8}) x = range(11)y = range(11)plt.plot(x, y)plt.show()

圖形顯示如下:

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

現(xiàn)在我們將橫縱坐標(biāo)變成百分比形式即,0%,20%,40%....代碼如下:

# encoding=utf-8import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatterplt.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í)行結(jié)果如下:

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

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

重要代碼

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

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

補(bǔ)充知識(shí):matplotlib畫(huà)圖系列之設(shè)置坐標(biāo)軸(精度、范圍,標(biāo)簽,中文字符顯示)

在使用matplotlib模塊時(shí)畫(huà)坐標(biāo)圖時(shí),往往需要對(duì)坐標(biāo)軸設(shè)置很多參數(shù),這些參數(shù)包括橫縱坐標(biāo)軸范圍、坐標(biāo)軸刻度大小、坐標(biāo)軸名稱(chēng)等

在matplotlib中包含了很多函數(shù),用來(lái)對(duì)這些參數(shù)進(jìn)行設(shè)置。

plt.xlim、plt.ylim 設(shè)置橫縱坐標(biāo)軸范圍 plt.xlabel、plt.ylabel 設(shè)置坐標(biāo)軸名稱(chēng) plt.xticks、plt.yticks設(shè)置坐標(biāo)軸刻度

以上plt表示matplotlib.pyplot

例子

#導(dǎo)入包import matplotlib.pyplot as pltimport 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))#畫(huà)曲線1plt.plot(x, y1)#畫(huà)曲線2plt.plot(x, y2, color=’blue’, linewidth=5.0, linestyle=’--’)#設(shè)置坐標(biāo)軸范圍plt.xlim((-5, 5))plt.ylim((-2, 2))#設(shè)置坐標(biāo)軸名稱(chēng)plt.xlabel(’xxxxxxxxxxx’)plt.ylabel(’yyyyyyyyyyy’)#設(shè)置坐標(biāo)軸刻度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) #顯示出所有設(shè)置plt.show()

結(jié)果

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

以上這篇使用Python matplotlib作圖時(shí),設(shè)置橫縱坐標(biāo)軸數(shù)值以百分比(%)顯示就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
主站蜘蛛池模板: 清涧县| 大姚县| 灌阳县| 易门县| 平阳县| 中山市| 松原市| 龙海市| 和顺县| 珠海市| 宣城市| 金昌市| 潢川县| 清河县| 郑州市| 疏附县| 新丰县| 桑植县| 怀仁县| 合肥市| 福泉市| 河东区| 钟祥市| 嘉鱼县| 招远市| 荔浦县| 镇平县| 田东县| 齐河县| 渝中区| 潼南县| 南漳县| 固镇县| 塔河县| 阜平县| 中江县| 高安市| 湟中县| 万山特区| 南平市| 波密县|