Matplotlib绘图设置---坐标轴刻度和标签设置
每个axes对象都有xaxis和yaxis属性,且xaxis和yaxis的每一个坐标轴都有主要刻度线/标签和次要刻度线/标签组成,标签位置通过一个Locator对象设置,标签格式通过一个Formatter设置。
plt.style.use('seaborn-whitegrid')
#x轴和y轴设置成对数显示尺度
ax = plt.axes(xscale='log', yscale='log')

#主刻度和次刻度标签位置对象Locator
print(ax.xaxis.get_major_locator())
print(ax.xaxis.get_minor_locator())
<matplotlib.ticker.LogLocator object at 0x0000021BD90A2308>
<matplotlib.ticker.LogLocator object at 0x0000021BD90A2508>
#主刻度和次刻度标签格式对象Formatter
print(ax.xaxis.get_major_formatter())
print(ax.xaxis.get_minor_formatter())
<matplotlib.ticker.LogFormatterSciNotation object at 0x0000021BD90A24C8>
<matplotlib.ticker.LogFormatterSciNotation object at 0x0000021BD99E97C8>
常用的定位器类和格式生成器类
| 定位器类 | 描述 |
|---|---|
| NullLocator | 无刻度 |
| FixdeLocator | 刻度位置固定 |
| IndexLocator | 用索引作为定位器(如 x = range(len(y)) |
| LinearLocator | 从min 到max 均匀分布刻度 |
| LogLocator | 从min 到 max 按对数分布刻度 |
| MultipleLocator | 刻度和范围都是基数(base)的倍数 |
| MaxNLocator | 为最大刻度找到最优位置 |
| AutoLocator | (默认)以MaxNLocator进行简单配置 |
| AutoMinorLocator | 次要刻度的定位器 |
| 格式生成器类 | 描述 |
|---|---|
| NullFormatter | 刻度上无标签 |
| IndexFormatter | 将一组标签设置为字符串 |
| FixedFormatter | 手动为刻度设置标签 |
| FuncFormatter | 用自定义函数设置标签 |
| FormatStrFormatter | 为每个刻度值设置字符串格式 |
| ScalarFormatter | (默认)为标量值设置标签 |
| LogFormatter | 对数坐标轴的默认格式生成器 |
隐藏刻度和标签
ax = plt.axes()
ax.plot(np.random.rand(50))
#y轴移除标签和刻度线
ax.yaxis.set_major_locator(plt.NullLocator())
#x轴移除标签,保留刻度线
ax.xaxis.set_major_formatter(plt.NullFormatter())

隐藏刻度和标签后的图像:

例子:
#创建5 * 5 的 (5 * 5)大小的窗格
fig, ax = plt.subplots(5, 5, figsize=(5, 5))
#行列空白设置为0
fig.subplots_adjust(hspace=0, wspace=0)
#从scikit-learn获取一些人脸照片数据
from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces().images
for i in range(5):
for j in range(5):
#隐藏x和y轴刻度和标签
ax[i,j].xaxis.set_major_locator(plt.NullLocator())
ax[i,j].yaxis.set_major_locator(plt.NullLocator())
ax[i,j].imshow(faces[10 * i + j], cmap='bone')

增减刻度数量
fig, ax = plt.subplots(4, 4, sharex=True, sharey=True)
for axi in ax.flat:
#plt.MaxNLocator()设置最多的刻度数量
axi.xaxis.set_major_locator(plt.MaxNLocator(3))
axi.yaxis.set_major_locator(plt.MaxNLocator(3))
设置最多的刻度数量为3:

自定义刻度格式(FuncFormatter)
#画正弦曲线和余弦曲线
fig, ax = plt.subplots()
x = np.linspace(0, 3 * np.pi, 1000)
ax.plot(x, np.sin(x), lw=3, label='Sine')
ax.plot(x, np.cos(x), lw=3, label='Cosine')
#设置网格
ax.grid(True)
#设置图例
ax.legend(frameon=False)
#设置坐标轴等距
ax.axis('equal')
#设置x坐标轴上下限
ax.set_xlim(0, 3 * np.pi)

#自定义坐标标签
#使用美元符号$将LaTex字符串括起来,可以显示数学符号和公式:$\pi$
def format_func(value, tick_number):
# N * np.pi/2 = value , value为np.pi/2的倍数
N = int(np.round(2 * value / np.pi))
# 0点位置
if N == 0:
return "0"
# np.pi/2 的位置
elif N == 1:
return r"$\pi/2$"
# np.pi/2的位置
elif N == 2:
return r"$\pi$"
# np.pi/2 倍数的位置
elif N % 2 > 0:
return r"${0}\pi/2$".format(N)
# np.pi 倍数的位置
else:
return r"${0}\pi$".format(N // 2)
#plt.FuncFormatter()创建自定义的刻度格式对象
my_formatter = plt.FuncFormatter(format_func)
ax.xaxis.set_major_formatter(my_formatter)
fig

Matplotlib绘图设置---坐标轴刻度和标签设置的更多相关文章
- 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...
- Matplotlib绘图双纵坐标轴设置及控制设置时间格式
双y轴坐标轴图 今天利用matplotlib绘图,想要完成一个双坐标格式的图. fig=plt.figure(figsize=(20,15)) ax1=fig.add_subplot(111) ax1 ...
- Python之坐标轴刻度细化、坐标轴设置、标题图例添加
学习python中matplotlib绘图设置坐标轴刻度.文本 http://www.jb51.net/article/134638.htm Python绘图 https://www.cnblogs. ...
- python库之matplotlib学习---关于坐标轴
首先定·定义x, y创建一个figure import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1, 1, 10) y ...
- 【划重点】Python matplotlib绘图设置坐标轴的刻度
一.语法简介 plt.xticks(ticks,labels,rotation=30,fontsize=10,color='red',fontweight='bold',backgroundcolor ...
- matplotlib画图教程,设置坐标轴标签和间距
大家好,欢迎来到周四数据处理专题,我们今天继续matplotlib作图教程. 在上周的文章当中我们介绍了如何通过xlabel和ylabel设置坐标轴的名称,以及这两个函数的花式设置方法,可以设置出各种 ...
- matplotlib绘图教程,设置标签与图例
大家好,欢迎大家阅读周四数据处理专题,我们继续介绍matplotlib作图工具. 在上一篇文章当中我们介绍了matplotlib这个包当中颜色.标记和线条这三种画图的设置,今天我们同样也介绍三种新的设 ...
- Matlab绘图基础——axis设置坐标轴取值范围
peaks; axis tight %Set the axis limits to equal the range of the data axis square axis 'auto x' % ...
- 2018.04.02 matplotlib 图名,图例,轴标签,轴边界,轴刻度,轴刻度标签
import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame(np.random.r ...
- 使用matplotlib绘制常用图表(2)-常用图标设置
一.使用subplots绘制子图 import numpy as np from matplotlib import pyplot as plt %matplotlib inline x = np.a ...
随机推荐
- 问题:AttributeError: module 'lib' has no attribute 'OpenSSL_add_all_algorithms'
分析 在使用支付宝沙箱时,报了这个错误,该问题是没有安装openssl包 解决 pip3 install pyOpenSSL 安装后再次运行如果还是报错,请降低加密库 pip install cryp ...
- 【LeetCode回溯算法#12】二叉树的直径,树形dp的前置内容(使用dfs)
二叉树的直径 给你一棵二叉树的根节点,返回该树的 直径 . 二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 .这条路径可能经过也可能不经过根节点 root . 两节点之间路径的 长度 由它们 ...
- logback中使用MDC自定义日志输出格式
logback-MDC 相当于自定义日志格式输出 写在过滤器中 示例: try { Context context = createContext(request, response); proces ...
- goland快键键防忘
环境: debian下的goland 2018实测: 鼠标button2 == 鼠标中键 光标移动: 跳转光标到刚才的位置: ctrl+win+alt+左/右 按词左右移动光标: ctrl + 左/右 ...
- 玩转Vue3之Composables
前言 Composables 称之为可组合项,熟悉 react 的同学喜欢称之为 hooks ,由于可组合项的存在,Vue3 中的组件之间共享状态比以往任何时候都更容易.这种新范例引入了一种更有组织性 ...
- golang开发:环境篇(三)开发利器Goland安装
这节主要介绍下golang开发的最主要的IDE,Goland.可以有效提高开发效率.用过一段时间 IntelliJ+GO插件,其实功能上跟goland差不多.不过团队的其它开发者基本都是Goland, ...
- LosslessCut 视频 切割合并 - 软件推荐 - 非常好用
LosslessCut 视频切割合并 - 软件推荐 - 非常好用 下载地址 https://n802.com/f/14902046-490311155-557856 参考文章 http://www.r ...
- Android Studio安装插件重启插件消失
问题 安装插件后,已经提示让重启IDE,但是重启后发现插件是安装失败了 解决方法 原因是自己改了配置,如果下载的插件是jar包,则可以安装,如果是zip压缩文件的插件,则是要我们手动解压一下 我上面的 ...
- 记录--开发uniapp nvue App+微信小程序,我踩过的坑( 纯干货 )
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 最近接了个项目,采用uniapp的nvue开发安卓和ios端+小程序端,第一次开发nvue,对于css布局这块,还是踩了很多坑.以及一些u ...
- 记录--用JS轻松实现一个录音、录像、录屏的工具库
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 最近项目遇到一个要在网页上录音的需求,在一波搜索后,发现了 react-media-recorder 这个库.今天就跟大家一起研究一 ...