gpt4 book ai didi

python - Matplotlib - 在图表中显示 Axis 中断//

转载 作者:行者123 更新时间:2023-12-04 01:04:11 24 4
gpt4 key购买 nike

我正在尝试重新创建以下情节的外观:

enter image description here

Axis 本身的中断非常简单,如下所述:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html

但我不知道如何在图表本身中包含//中断。有什么想法吗?

这是我的数据目前的样子。

Here

最佳答案

包裹brokenaxes如您所知,非常适合在 Axis 上打断,但在已发布的包中似乎没有这方面的功能。不过,我已经想出如何实现您的目标。

首先在终端中从您的环境目录安装包:

>>> pip install brokenaxes

>>> conda install -c conda-forge brokenaxes

解决方法:

>>> from brokenaxes import brokenaxes
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> x_min = 0
>>> x_break = 3
>>> gap = .1
>>> x_max = 6
>>>
>>> def f(x):
... return np.cos(x)
...
>>>
>>> fig = plt.figure(figsize=(10,4))
>>> brax = brokenaxes(xlims=((x_min, x_break), (x_break+gap, x_max)), hspace=gap)
>>> xs = np.linspace(x_min, x_max, 150)
>>>
>>> brax.plot(xs, f(xs))
>>> brax.annotate(u'\u2014', (x_break-0.001, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')
>>> brax.annotate(u'\u2014', (x_break+gap, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')
>>>
>>> plt.show()

如果您从现有数组中绘图,函数 f 可以从数组中检索这些值索引。

关于python - Matplotlib - 在图表中显示 Axis 中断//,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67066349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com