绘图(一) 坐标轴设置
1. 字体
plt.rc('font',family='Times New Roman')
2. 刻度设置
- fontsize 字号
- color 颜色
- rotation 倾斜度
plt.xticks(fontsize=10,color='blue',rotation=60)
plt.yticks(fontsize=10)
3. 坐标轴范围xlim/ylim
plt.xlim(0,100)
plt.ylim(10,20)
4. 坐标轴线
- 粗细
ax=plt.gca();
ax.spines['bottom'].set_linewidth(1);
ax.spines['left'].set_linewidth(1);
- 不显示坐标轴上面和右边刻度线
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
或者
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
5. 坐标轴标签
plt.xlabel('X')
plt.ylabel('Y')
6. 坐标轴刻度间隔
xmajorLocator = MultipleLocator(10)
xmajorFormatter = FormatStrFormatter('%1.1f')
xminorLocator = MultipleLocator(1)
ymajorLocator = MultipleLocator(0.05) # 主刻度以0.05为单位
ymajorFormatter = FormatStrFormatter('%1.1f') # 精确到小数点后一位
yminorLocator = MultipleLocator(0.01) # 次刻度0.01为单位
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.xaxis.set_major_formatter(xmajorFormatter)
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_major_locator(y_major_locator)
ax.yaxis.set_major_formatter(ymajorFormatter)
ax.yaxis.set_minor_locator(yminorLocator)
#坐标轴的网格使用主刻度/次刻度设置
ax.xaxis.grid(True, which='major') # x主刻度
ax.yaxis.grid(True, which='minor') # y次刻度