07_设置坐标轴刻度、设置刻度标签(Setting Tick Labels)、调整刻度标签 (Adjusting the ticklabels)
7.設置刻度
7.1.設置坐標軸刻度
7.2.設置刻度標簽(Setting Tick Labels)
7.3.調整刻度標簽 (Adjusting the ticklabels)
7.設置刻度
7.1.設置坐標軸刻度
到目前為止,在所有例子中Matplotlib都自動負責了確定軸上間距點的任務。
例如,可以看到前面一個例子中的X軸編號為 -6. -4, -2, 0, 2, 4, 6, 而Y軸編號 -1.0,0, 1.0, 2.0, 3.0
使用xticks或yticks可獲取或設置當前的刻度位置和標簽。
import numpy as np import matplotlib.pyplot as plt # get the current axes, creating them if necessary: ax = plt.gca() locs, labels = plt.xticks() print(locs, labels) locs, labels = plt.xticks() print(locs, labels) locs, labels = plt.yticks() print(locs, labels)輸出結果:
[0. 0.2 0.4 0.6 0.8 1. ] [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')] [0. 0.2 0.4 0.6 0.8 1. ] [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')] plt.xticks(np.arange(10)) locs, labels = plt.xticks() print(locs, labels) ''' 輸出結果: [0 1 2 3 4 5 6 7 8 9] [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')] '''現在,設置xticks的位置和標簽:
# set the locations and labels of the xticks plt.xticks(np.arange(4),('Berlin', 'London', 'Paris', 'Toronto')) plt.show()輸出:
([<matplotlib.axis.XTick at 0x17822581a48>,<matplotlib.axis.XTick at 0x1782257dec8>,<matplotlib.axis.XTick at 0x1782257da88>,<matplotlib.axis.XTick at 0x178225a8248>],<a list of 4 Text xticklabel objects>)回到之前的三角函數示例。下面使用Pi的因子而非整數值作為X軸的刻度:
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-2 * np.pi, 2 * np.pi, 70, endpoint=True) F1 = np.sin(X**2) F2 = X * np.sin(X) # get the current axes, creating them if necessary: ax = plt.gca() # making the top and right spine invisible: ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') # moving bottom spine up to y=0 position: ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) # moving left spine to the right to position x == 0: ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0)) plt.xticks( [-6.28, -3.14, 3.14, 6.28]) plt.yticks([-3, -1, 0, +1, 3]) plt.plot(X, F1) plt.plot(X, F2) plt.show()7.2.設置刻度標簽(Setting Tick Labels)
現在用自定義標記重新定義xticks。 我們將再次使用xticks方法來實現此目的。
但是這次將用兩個參數調用xticks:
第一個是之前使用的相同列表,即x軸上的位置,來得到刻度。
第二個參數是具有與之相應LaTex刻度標記的相同大小的列表,即想要用文本而不是值。LaTex表示法必須是一個原始字符串(raw string)來抑制Python的轉義機制,因為LaTex表示法大量使用并依賴于反斜杠。
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-2 * np.pi, 2 * np.pi, 70, endpoint=True) F1 = X * np.sin(X) ax = plt.gca() ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0)) # labelling the X ticks: plt.xticks( [-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$']) plt.yticks([-3, -1, 0, +1, 3]) plt.plot(X, F1) plt.show()7.3.調整刻度標簽 (Adjusting the ticklabels)
我們希望增加ticklabels的易讀性。 我們將增加字體大小,并將在半透明背景上渲染它們。
print(ax.get_xticklabels()) ''' 輸入結果: [Text(-6.28, 0, '$-2\\pi$'), Text(-3.14, 0, '$-\\pi$'), Text(3.14, 0, '$+\\pi$'), Text(6.28, 0, '$+2\\pi$')] '''for xtick in ax.get_xticklabels():print(xtick) ''' 輸出結果: Text(-6.28, 0, '$-2\\pi$') Text(-3.14, 0, '$-\\pi$') Text(3.14, 0, '$+\\pi$') Text(6.28, 0, '$+2\\pi$') '''labels = [xtick.get_text() for xtick in ax.get_xticklabels()] print(labels) ''' 輸出結果: ['$-2\\pi$', '$-\\pi$', '$+\\pi$', '$+2\\pi$'] '''讓我們增加字體大小并使字體半透明:
import numpy as np import matplotlib.pyplot as pltX = np.linspace(-2 * np.pi, 2 * np.pi, 170, endpoint=True) F1 = np.sin(X ** 3 / 2) ax = plt.gca() ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data', 0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data', 0)) plt.xticks([-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$']) plt.yticks([-3, -1, 0, +1, 3]) for xtick in ax.get_xticklabels():# 設置x軸上字體大小xtick.set_fontsize(18)# 設置x軸標簽的顏色xtick.set_bbox(dict(facecolor='red', edgecolor='None', alpha=0.7)) for ytick in ax.get_yticklabels():# 設置y軸上字體大小ytick.set_fontsize(14)# 設置y軸上標簽的顏色ytick.set_bbox(dict(facecolor='green', edgecolor='red', alpha=0.7))plt.plot(X, F1, label="$sin(x)$") plt.legend(loc='lower left') plt.show()總結
以上是生活随笔為你收集整理的07_设置坐标轴刻度、设置刻度标签(Setting Tick Labels)、调整刻度标签 (Adjusting the ticklabels)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive中存Array类型的数据的案例,
- 下一篇: 08_添加图例、添加标题(Adding