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 plt

X = 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()

在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值