在使用matplotlib模塊時畫坐標(biāo)圖時,往往需要對坐標(biāo)軸設(shè)置很多參數(shù),這些參數(shù)包括橫縱坐標(biāo)軸范圍、坐標(biāo)軸刻度大小、坐標(biāo)軸名稱等
xlim()
:設(shè)置x坐標(biāo)軸范圍ylim()
:設(shè)置y坐標(biāo)軸范圍xlabel()
:設(shè)置x坐標(biāo)軸名稱ylabel()
:設(shè)置y坐標(biāo)軸名稱xticks()
:設(shè)置x軸刻度yticks()
:設(shè)置y軸刻度#創(chuàng)建數(shù)據(jù)x = np.linspace(-5, 5, 100)y1 = np.sin(x)y2 = np.cos(x)#創(chuàng)建figure窗口,figsize設(shè)置窗口的大小plt.figure(num=3, figsize=(8, 5))#畫曲線1plt.plot(x, y1)#畫曲線2plt.plot(x, y2, color='blue', linewidth=5.0, linestyle='--')#設(shè)置坐標(biāo)軸范圍plt.xlim((-5, 5))plt.ylim((-2, 2))#設(shè)置坐標(biāo)軸名稱plt.xlabel('xxxxxxxxxxx')plt.ylabel('yyyyyyyyyyy')#設(shè)置坐標(biāo)軸刻度my_x_ticks = np.arange(-5, 5, 0.5)#對比范圍和名稱的區(qū)別#my_x_ticks = np.arange(-5, 2, 0.5)my_y_ticks = np.arange(-2, 2, 0.3)plt.xticks(my_x_ticks)plt.yticks(my_y_ticks)#顯示出所有設(shè)置plt.show()
import matplotlib.pyplot as pltimport numpy as npx = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))plt.xlabel('I am x')plt.ylabel('I am y')plt.show()
x = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))plt.xlabel('I am x')plt.ylabel('I am y')new_ticks = np.linspace(-1, 2, 5)print(new_ticks)plt.xticks(new_ticks)plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])plt.show()
gca()
:獲取當(dāng)前坐標(biāo)軸信息.spines
:設(shè)置邊框.set_color
:設(shè)置邊框顏色:默認(rèn)白色.spines
:設(shè)置邊框.xaxis.set_ticks_position
:設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置.yaxis.set_ticks_position
:設(shè)置y坐標(biāo)刻度數(shù)字或名稱的位置.set_position
:設(shè)置邊框位置x = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))new_ticks = np.linspace(-1, 2, 5)plt.xticks(new_ticks)plt.yticks([-2, -1.8, -1, 1.22, 3],['$really\ bad$', '$bad$', '$normal$', '$good$', '$really\ good$'])ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')plt.show()
ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')#設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置ax.xaxis.set_ticks_position('bottom')#設(shè)置邊框位置ax.spines['bottom'].set_position(('data', 0))plt.show()
ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')#設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置ax.xaxis.set_ticks_position('bottom')#設(shè)置邊框位置ax.spines['bottom'].set_position(('data', 0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data',0))plt.show()
matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=False, **kwargs)
x, y:表示坐標(biāo);s:字符串文本;fontdict:字典,可選;kw:fontsize=12,horizontalalignment=‘center’、ha=’cener’verticalalignment=’center’、va=’center’
#!/usr/bin/python#coding: utf-8import numpy as npimport matplotlib.pyplot as pltx = np.arange(-10, 11, 1) #形成一個數(shù)組,第三個參數(shù)表示步長,#start,end,stepy = x ** 2plt.plot(x, y)# 第一個參數(shù)是x軸坐標(biāo)# 第二個參數(shù)是y軸坐標(biāo)# 第三個參數(shù)是要顯式的內(nèi)容# alpha 設(shè)置字體的透明度# family 設(shè)置字體# size 設(shè)置字體的大小# style 設(shè)置字體的風(fēng)格# wight 字體的粗細(xì)# bbox 給字體添加框,alpha 設(shè)置框體的透明度, facecolor 設(shè)置框體的顏色plt.text(-3, 20, "function: y = x * x", size = 15, alpha = 0.2)plt.text(-3, 40, "function: y = x * x", size = 15, family = "fantasy", color = "r", style = "italic", weight = "light", bbox = dict(facecolor = "r", alpha = 0.2))plt.show()
# -*- coding: utf-8 -*-import timeimport matplotlib.pyplot as pltdef showResult(xList, yList, title, xLabel, yLabel): plt.plot(xList, yList, 'g*-') plt.title(title) plt.xlabel(xLabel) plt.ylabel(yLabel) for x, y in zip(xList, yList): plt.text(x, y+0.3, str(y), ha='center', va='bottom', fontsize=10.5) plt.savefig('fig'+str(int(time.time()))+'.jpg') plt.show()x_arr = [1, 2, 3, 4, 5, 6]y_arr = [1, 4, 9, 16, 25, 36]showResult(x_arr, y_arr, 'title', 'x', 'y')
其中
for x, y in zip(xList, yList):plt.text(x, y+0.3, '%.0f'%y, ha='center', va='bottom', fontsize=10.5)
逐個獲取需要標(biāo)注的點的橫縱坐標(biāo) x與 y,然后在位置 (x, y+0.3) 處以 10.5 的字體顯示出 y 的值,‘center’ 和 ‘bottom’ 分別指水平和垂直方向上的對齊方式。