两者存在异同
axes对于下标的更换:
- set_xticks()
set_xticklabels()
set_yticks()
set_yticklabels()
matplotlib.pyplot.xticks()功能
- matplotlib库的pyplot模块中的annotate()函数用于获取和设置x轴的当前刻度位置和标签
axes对于下标的更换:
set_xticks()
set_xticklabels()
set_yticks()
set_yticklabels()
matplotlib.pyplot.xticks()功能
matplotlib库的pyplot模块中的annotate()函数用于获取和设置x轴的当前刻度位置和标签
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签
plt.rcParams['axes.unicode_minus']=False
#在距画布顶部0.2,、左侧0.2的位置上添加一个宽度为0.5、高度为0.5的绘图区域
ax0 = plt.axes((0.2, 0.2, 0.5, 0.5))
x = np.arange(5)
ax0.plot(x, [1044.98, 1045.02, 1044.32, 1041.05, 1042.08], '^', ms=8, color='green')
ax0.set_xticks(x)
ax0.set_xticklabels(['周一', '周二', '周三', '周四', '周五'])
ax0.set_yticks([1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046])
ax0.set_yticklabels([1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046])
plt.show()
#####
ax = plt.axes((0.2, 0.2, 0.5, 0.5))
x = np.arange(5)
ax.plot(x, [1044.98, 1045.02, 1044.32, 1041.05, 1042.08], '^', ms=8, color='green')
ax.set_xticks(x)
ax.set_yticks([1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046])
plt.show()
#####
ax1 = plt.axes((0.4, 0.4, 0.5, 0.5))
x = np.arange(5)
ax1.plot(x, [1044.98, 1045.02, 1044.32, 1041.05, 1042.08], '^', ms=8, color='green')
ax1.set_xticklabels(['周一', '周二', '周三', '周四', '周五'])
ax1.set_yticklabels([1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046])
plt.show()
# xticks 和 xticklabels 一起使用最合适(同理y轴也是)
# 单独使用 xticklabels 时,默认按照原有的轴标进行替换
ax = plt.axes((0.2, 0.2, 0.5, 0.5))
x = np.arange(5)
ax.plot(x, [1044.98, 1045.02, 1044.32, 1041.05, 1042.08], '^', ms=8, color='green')
ax.set_xticks(x, ['周一', '周二', '周三', '周四', '周五'])
ax.set_yticks([1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046])
plt.show()
#plt 的 xticks
ax = plt.axes((0.2, 0.2, 0.5, 0.5))
x = np.arange(5)
ax.plot(x, x, '^', ms=8, color='green')
plt.xticks(x, ['周一', '周二', '周三', '周四', '周五'])
plt.show()