【Matplotlib-6】坐标轴刻度

语句“ax.tick_params()”是通过调用实例ax的实例方法进行刻度样式设置的。同时,通过调用模块pyplot中的函数也可以实现刻度样式的设置工作。具体而言,模块pyplot中的刻度样式的设置是通过函数tick_params()实现的,即可以执行语句“plt.tick_params()”来进行刻度样式的设置。前者是matplotlib的面向对象的操作方法,后者是调用模块pyplot的API的操作方法,这是两种不同思想的操作模式,虽然使用pyplot模块绘制图表非常方便,但是要想使图表有更多的调整和定制化展示,还是应该使用matplotlib的面向对象的操作方法。

使用Matplotlib的面向对象方法

figure——画布实例

axes——一个图表实例

xaxis——x轴实例,yaxis——y轴实例

add_axes()——添加子axes,参数是一个坐标轴位置和大小的四元列表

fig.add_subplot(111)——分割画布,添加子axes

MultipleLocator(val)——一格为一个val(主副刻度都可用)

AutoMinorLocator(n)——设置副刻度,按照将主刻度一格分为几格副刻度划分

ax.set_xlim——X坐标轴范围

ax.set_title( 'title test',fontsize=12,color='r')——设置标题

set_xlabel——X坐标轴标签

ax.xaxis.set_major_locator——主刻度位置

ax.xaxis.set_minor_locator——副刻度位置

ax.xaxis.set_ticks_position——设置刻度相对于坐标轴的位置

ax.xaxis.set_minor_formatter——副刻度格式

ax.tick_params——刻度样式

ax.set_xticklabels——刻度是文字

from matplotlib.ticker import MultipleLocator, AutoMinorLocator, FormatStrFormatter
## 画布未分割
plt.figure(figsize = (7,6),dpi = 100)
ax = plt.gca() ## 获取坐标轴
## 画布分割
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
## 设置坐标轴范围
ax.set_xlim(0, 10)
ax.set_ylim(0, 100)
## 坐标轴标签
## boxstyle用于给xlabel和ylabel加文本框
ax.set_xlabel("我是X轴",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
ax.set_ylabel("我是Y轴",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
## 设置主刻度位置
ax.xaxis.set_major_locator(MultipleLocator(1.0)) ## MultipleLocator(1.0)表示刻度间隔1.0
ax.yaxis.set_major_locator(MultipleLocator(1.0))
## 设置副刻度位置
ax.xaxis.set_minor_locator(MultipleLocator(0.25)) ## 副刻度一格0.25
ax.yaxis.set_minor_locator(AutoMinorLocator(4)) ## 自动设置副刻度,一格分为4小格
## 副刻度刻度格式
## 主刻度自动有数值标注,但副刻度需要手动设置数值格式
ax.xaxis.set_minor_formatter(FormatStrFormatter('%0.2f')) ## 小数点后两位float
## 刻度线样式:线宽、线长、颜色
## 主刻度
ax.tick_params("x",which='major',length=15,width=2.0,colors="r",direction = 'in') ## direction指定刻度线朝向内还是外,in; out; inout
ax.tick_params("y",which='major',length=5,width=1.0,colors="r",direction = 'in')
ax.tick_params(which='major',length=15,width=2.0,colors="r",direction = 'in') ## 省略指定具体的x/y轴,则同时设置2个坐标轴
## 副刻度
ax.tick_params(which='minor',length=5,width=1.0,labelsize=6, labelcolor='0.25',direction = 'inout') ## 同时设置x/y轴的副刻度
## 设定刻度标签是文字
ax.set_xticklabels(['A','B','C','D','E','F','G'])
ax.set_yticklabels(['鉴','图','化','视','可','注','关'],family = 'SimHei',fontsize = 14)
## 刻度标签样式:颜色、字体、旋转
for ticklabel in ax.xaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30)
for ticklabel in ax.yaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30)
## 刻度标签相对于坐标轴的位置
ax.xaxis.set_ticks_position("bottom") ## x轴刻度在x轴下方
ax.yaxis.set_ticks_position("left") ## y轴刻度在y轴左侧
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, AutoMinorLocator
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.set_xlim(0, 10)
ax.set_ylim(0, 100)
ax.set_xlabel("X",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
ax.set_ylabel("Y",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
ax.xaxis.set_major_locator(MultipleLocator(1.0)) ## MultipleLocator(1.0)表示刻度间隔1.0
ax.yaxis.set_major_locator(MultipleLocator(5.0))
ax.xaxis.set_minor_locator(MultipleLocator(0.25)) ## 副刻度一格0.25
ax.xaxis.set_minor_formatter(FormatStrFormatter('%0.2f')) ## 小数点后两位float
ax.tick_params("x",which='major',length=5,width=1.0,colors="lightgreen",direction = 'in')
ax.tick_params("y",which='major',length=5,width=1.0,colors="r",direction = 'in')
ax.tick_params(which='minor',length=5,width=1.0,labelsize=5, labelcolor='0.25',direction = 'inout') ## 同时设置x/y轴的副刻度
for ticklabel in ax.xaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(12)
ticklabel.set_rotation(30)
for ticklabel in ax.yaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(12)
plt.show()

使用pyplot函数

坐标轴刻度范围

plt.xlim(xstart, xend)
plt.ylim(ystart, yend)

xstart——刻度的起点

xend——刻度的终点

起点、终点不具有大小关系的限制,xstart > xend也可,坐标轴刻度将由大到小

坐标轴刻度位置、标签

plt.xticks(ticks, labels, **kwargs)

ticks——刻度位置。传递一个空列表会删除所有xticks。

labels——刻度标签

kwargs——刻度标签外观

rotation = 20 ##旋转20度
rotation = 'vertical' ## 标签竖直
>>> locs, labels = xticks() # Get the current locations and labels.
>>> xticks(np.arange(0, 1, step=0.2)) # Set label locations.
>>> xticks(np.arange(3), ['Tom', 'Dick', 'Sue']) # Set text labels.
>>> xticks([0, 1, 2], ['January', 'February', 'March'],rotation=20) # Set text labels and properties.
>>> xticks([0, 1, 2], ['January', 'February', 'March'],rotation='vertical') # Set text labels and properties.
>>> xticks([]) # Disable xticks.
posted @   徘徊彼岸花  阅读(518)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示