更改 matplotlib 中图的线条样式

发布于 2025-01-06 04:31:51 字数 272 浏览 0 评论 0原文

我正在绘制一些数据(两条线),并且我想更改线条之间差异具有统计显着性的部分的线条样式。因此,在下图中(现在链接 b/c 反垃圾邮件政策不允许我发布图像),我希望线条看起来有所不同(即可能是虚线),直到它们开始在 35 左右收敛x 轴。

线图

有没有办法轻松做到这一点?我的 x 轴值差异很大,我只是不清楚如何更改某些 x 轴位置的线条样式。

I'm graphing some data (two lines) and I'd like to change the line style for the portions of the lines where the difference between them is statistically significant. So, in the below image (now a link b/c anti-spam policies don't allow me to post an image) I'd like the lines to look different (i.e. dashed perhaps) up until they start converging at about 35 on the x axis.

line plot

Is there a way to do this easily? I have the values for the x axis where the differences are significant, I'm just not clear how to change line styles at certain x-axis locations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2025-01-13 04:31:51

编辑:我已经打开并离开了,所以我没有注意到@Ricardo的答案。因为 matplotlib 无论如何都会将东西转换为 numpy 数组,所以有更有效的方法来做到这一点。

举个例子:

只需绘制两条不同的线,一条具有虚线样式,另一条具有实线样式。

例如

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = 2 * x
y2 = 3 * x

xthresh = 4.5
diff = np.abs(y1 - y2)
below = diff < xthresh
above = diff >= xthresh

# Plot lines below threshold as dotted...
plt.plot(x[below], y1[below], 'b--')
plt.plot(x[below], y2[below], 'g--')

# Plot lines above threshold as solid...
plt.plot(x[above], y1[above], 'b-')
plt.plot(x[above], y2[above], 'g-')

plt.show()

在此处输入图像描述

对于循环的情况,请使用屏蔽数组:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = 2 * np.cos(x)
y2 = 3 * np.sin(x)

xthresh = 2.0
diff = np.abs(y1 - y2)
below = diff < xthresh
above = diff >= xthresh

# Plot lines below threshold as dotted...
plt.plot(np.ma.masked_where(below, x), np.ma.masked_where(below, y1), 'b--')
plt.plot(np.ma.masked_where(below, x), np.ma.masked_where(below, y2), 'g--')

# Plot lines above threshold as solid...
plt.plot(np.ma.masked_where(above, x), np.ma.masked_where(above, y1), 'b-')
plt.plot(np.ma.masked_where(above, x), np.ma.masked_where(above, y2), 'g-')

plt.show()

在此处输入图像描述

Edit: I'd had this open and left, so I didn't notice @Ricardo's answer. Because matplotlib will convert things to numpy arrays regardless, there are more efficient ways to do it.

As an example:

Just plot two different lines, one with a dashed linestyle and another with a solid linestyle.

E.g.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = 2 * x
y2 = 3 * x

xthresh = 4.5
diff = np.abs(y1 - y2)
below = diff < xthresh
above = diff >= xthresh

# Plot lines below threshold as dotted...
plt.plot(x[below], y1[below], 'b--')
plt.plot(x[below], y2[below], 'g--')

# Plot lines above threshold as solid...
plt.plot(x[above], y1[above], 'b-')
plt.plot(x[above], y2[above], 'g-')

plt.show()

enter image description here

For the case where they're cyclic, use masked arrays:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = 2 * np.cos(x)
y2 = 3 * np.sin(x)

xthresh = 2.0
diff = np.abs(y1 - y2)
below = diff < xthresh
above = diff >= xthresh

# Plot lines below threshold as dotted...
plt.plot(np.ma.masked_where(below, x), np.ma.masked_where(below, y1), 'b--')
plt.plot(np.ma.masked_where(below, x), np.ma.masked_where(below, y2), 'g--')

# Plot lines above threshold as solid...
plt.plot(np.ma.masked_where(above, x), np.ma.masked_where(above, y1), 'b-')
plt.plot(np.ma.masked_where(above, x), np.ma.masked_where(above, y2), 'g-')

plt.show()

enter image description here

软的没边 2025-01-13 04:31:51

假设您的数据位于 NumPy 数组 dataset1dataset2 上,并且您已将 threshold 定义为您的重要性

def group(data):
    """Assumes that len(data) > 0"""
    prev = 0
    index = 1
    value = data[0]

    while (index < len(data)):
        if data[index] != value:
            yield (value, prev, index)

            value = not value
            prev = index
        index += 1

    yield (value, prev, index)

diff = np.abs(dataset1 - dataset2)
for significant, start, end in group(diff < threshold):
   # Plot data from dataset1[start:end] and dataset2[start:end]
   # Use the value in "significant" (True/False) to figure out
   # The style

Let's say that your data is on NumPy arrays dataset1 and dataset2 and you've defined threshold as your significance

def group(data):
    """Assumes that len(data) > 0"""
    prev = 0
    index = 1
    value = data[0]

    while (index < len(data)):
        if data[index] != value:
            yield (value, prev, index)

            value = not value
            prev = index
        index += 1

    yield (value, prev, index)

diff = np.abs(dataset1 - dataset2)
for significant, start, end in group(diff < threshold):
   # Plot data from dataset1[start:end] and dataset2[start:end]
   # Use the value in "significant" (True/False) to figure out
   # The style
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文