matplotlib scale 刻度

1.5. 对数或者其他非线性坐标轴
使用plt.xscal()来改变坐标轴的刻度

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter # useful for `logit` scale
# Fixing random state for reproducibility
np.random.seed(19680801)
# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
# plot with various axes scales
plt.figure(1)
# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
plt.show()

这里写图片描述

### 如何在 Matplotlib 中设置指数刻度 为了实现坐标轴的指数刻度,在 Matplotlib 中可以通过 `LogLocator` 和 `LogFormatter` 来完成这一功能。对于更简单的场景,可以直接利用 `plt.xscale()` 或者 `plt.yscale()` 函数来快速应用对数尺度到对应的轴上。 下面是一个具体的实例展示如何将 Y 轴转换为指数形式: ```python import matplotlib.pyplot as plt import numpy as np # 创建一些数据用于绘制图形 x = np.linspace(0.01, 100, 400) y = x ** 2 fig, ax = plt.subplots() ax.plot(x, y) # 将Y轴设为对数(即指数)刻度 ax.set_yscale('log') # 可选配置:自定义主次网格线样式 ax.grid(True, which="both", ls="--") plt.title("Example of Logarithmic Scale on Y-axis") plt.xlabel("Linear scale") plt.ylabel("Logarithmic scale") plt.show() ``` 这段代码会生成一张图表,其中 X 轴保持线性比例而 Y 轴则采用了对数值的比例关系[^1]。 当需要更加精细控制时,则可能需要用到 `ticker.LogLocator` 类来自定义主要和次要刻度的位置以及通过 `ticker.LogFormatterSciNotation` 对这些刻度进行科学记号格式化处理[^2]。 #### 自定义指数刻度的例子 如果想要进一步定制刻度位置或者改变其表现方式,可以这样做: ```python from matplotlib.ticker import ScalarFormatter, LogFormatterSciNotation, LogLocator import matplotlib.pyplot as plt import numpy as np x = np.logspace(np.log10(1e-2), np.log10(1e2), num=100) y = x**(3/2.) fig, ax = plt.subplots(figsize=(8,6)) ax.semilogy(x, y,'-', lw=2) # 使用LogLocator设定大刻度位置;使用LogFormatterSciNotation来进行科学计数法标注 major_locator = LogLocator(base=10,numticks=12) minor_locator = LogLocator(subs='all') formatter = LogFormatterSciNotation(labelOnlyBase=False) ax.yaxis.set_major_locator(major_locator) ax.yaxis.set_minor_locator(minor_locator) ax.yaxis.set_major_formatter(formatter) plt.title('Customized Exponential Tick Marks') plt.xlabel('X Axis Label (linear)') plt.ylabel('Y Axis Label ($\log_{10}$)') plt.tight_layout() plt.show() ``` 此段脚本不仅设置了基本的大刻度间隔还增加了更多细节的小刻度,并且所有刻度都被赋予了恰当的科学记数法标签[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值