更改matplotlib线型中图

Sce*_*zer 10 python graphing styles matplotlib line

我正在绘制一些数据(两行),我想改变行的部分的线条样式,它们之间的差异在统计上是显着的.所以,在下图中(现在链接b/c反垃圾邮件策略不允许我发布图像)我希望线条看起来不同(也许是虚线),直到它们开始收敛于35左右x轴.

线图

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

Joe*_*ton 15

编辑:我已经打开并离开了,所以我没有注意到@里卡多的回答.因为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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于它们是循环的情况,使用蒙版数组:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述