matplotlib.pyplot.xlim()、ylim()、axis()结构及用法||参数详解

matplotlib.pyplot.xlim(*args, **kwargs)

获取或者是设定x座标轴的范围,当前axes上的座标轴。

有两种参数输入方式

  1. plt.xlim(num1, num2)
    

在这里插入图片描述

  1. plt.xlim(xmin=num1,xmax=num2)
    

在这里插入图片描述

内容与x一样

matplotlib.pyplot.axis(*v, **kwargs)

参数详解:

  1. xmin, xmax, ymin, ymax : float, optional

  2. option : str

    ValueDescription
    ‘on’Turn on axis lines and labels.
    ‘off’Turn off axis lines and labels.
    ‘equal’Set equal scaling (i.e., make circles circular) by changing axis limits.
    ‘scaled’Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box.
    ‘tight’Set limits just large enough to show all data.
    ‘auto’Automatic scaling (fill plot box with data).
    ‘normal’Same as ‘auto’; deprecated.
    ‘image’‘scaled’ with axis limits equal to data limits.
    ‘square’Square plot; similar to ‘scaled’, but initially forcing xmax-xmin = ymax-ymin.
  3. emit : bool, optional, default True

在这里插入图片描述

### 使用 Matplotlib 添加注释 Matplotlib 提供了多种方法来向图表中添加注释,这有助于解释数据点或突出显示特定区域。以下是几种常见的添加注释的方式。 #### 方法一:使用 `plt.annotate` `annotate` 函数允许指定要标注的位置以及放置文本的地方,并可以通过箭头连接两者: ```python import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) ax.plot(t, s) ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() ``` 这段代码创建了一个简单的余弦波形图,在 t=2 的位置标记出了局部最大值并附带说明文字[^1]。 #### 方法二:利用 `ax.text` 或者 `plt.text` 如果只需要简单地在图形上某个固定坐标处添加一些描述性的文字,则可以直接使用 `text()` 方法: ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlim([0, 10]) ax.set_ylim([0, 10]) # Add text in string 'Text' to axis at location (0, 0) ax.text(0, 0, "Origin", fontsize=12) plt.show() ``` 此示例展示了如何在一个空白画布上的原点附近添加一段名为 “Origin”的文本标签[^2]。 #### 方法三:结合 `arrowstyle` 参数自定义样式 对于更复杂的场景,比如想要改变箭头形状或其他属性时,可以在 `annotate` 中设置更多选项: ```python from matplotlib.patches import FancyArrowPatch class MyFancyArrow(FancyArrowPatch): def __init__(self, posA=None, posB=None, *args, **kwargs): super().__init__(posA, posB, *args, **kwargs) fig, ax = plt.subplots(figsize=(6, 4)) x = range(7) y = [i**2 for i in x] line, = ax.plot(x, y) for xi, yi in zip(x, y)[::2]: ax.add_patch(MyFancyArrow((xi-.1, yi+.8), (xi+.1, yi-.8), mutation_scale=20)) plt.xlim(-1, 7); plt.ylim(-1, 49); plt.show() ``` 这里通过继承 `FancyArrowPatch` 类来自定义箭头外观,并将其应用于多个数据点之间[^3]。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值