更改matplotlib线型中线图 [英] Change matplotlib line style mid-graph

查看:187
本文介绍了更改matplotlib线型中线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一些数据(两行)的图形,我想更改行之间的差异具有统计意义的部分的线条样式.因此,在下面的图片中(现在链接b/c反垃圾邮件策略不允许我发布图片),我希望这些线条看起来有所不同(例如,可能是虚线),直到它们开始收敛到大约35 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.

线条图

有没有一种方法可以轻松地做到这一点?我有x轴的值,其中差异很大,我只是不清楚如何在某些x轴位置更改线型.

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.

推荐答案

我已经打开并离开了,所以我没有注意到@Ricardo的回答.由于matplotlib不管如何都会将其转换为numpy数组,因此有更有效的方法.

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.

例如:

只需绘制两条不同的线,一条线为虚线,另一条线为实线.

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

例如

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()

对于循环的情况,请使用掩码数组:

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()

这篇关于更改matplotlib线型中线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆