Matplotlib 中的线段 [英] Line Segments in Matplotlib

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

问题描述

给定 [1,5,7,3,5,10,3,6,8] 的坐标,用于 matplotlib.pyplot,我如何突出显示或着色线的不同部分.例如,列表中的坐标1-3([1,5,7,3])代表属性a.如何为该行着色并在图例中进行标记?

Given coordinates of [1,5,7,3,5,10,3,6,8] for matplotlib.pyplot, how do I highlight or colour different segments of the line. For instance, the coordinates 1-3 ([1,5,7,3]) in the list represent attribute a. How do I colour this bit of the line and mark it in the legend?

有问题的列表包含数以万计的元素.我正在尝试突出显示列表的特定部分.从目前的答案来看,假设我必须逐个绘制每个段是否正确?没有办法说选择从 x1 坐标到 x2 坐标的线段,改变线的颜色"

The list in question contains tens of thousands of elements. I'm trying to highlights specific sections of the list. From the answers so far, is it right to assume I must draw each segment one by one? There isn't a way to say "select line segment from x1 coord to x2 coord, change colour of line"

推荐答案

是的,您需要重新绘制线条,但是可以裁剪线条,以便仅显示您感兴趣的部分.为此,我创建了一个覆盖代表道具(a)的区域的矩形,然后使用它创建了一个 clip_path .

Yes, you need to redraw the line, but you can clip the line so that only the part you are interested in is visible. To do this I create a rectangle covering the area that represents prop (a), then I use this to create a clip_path.

import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox

data = [1,5,7,3,5,10,3,6,8]
X0 = 1
X1 = 3

plt.plot(data, label='full results')
# make a rectangle that will be used to crop out everything not prop (a)
# make sure to use data 'units', so set the transform to transData
propArect = plt.Rectangle((X0, min(data)), X1, max(data), 
                          transform=plt.gca().transData)
# save the line so when can set the clip
line, = plt.plot(data,
         color='yellow',
         linewidth=8,
         alpha=0.5,
         label='Prop (a)',
         )
line.set_clip_path(propArect)

handles, labels = plt.gca().get_legend_handles_labels()
plt.legend(handles, labels)
plt.savefig('highlight.png')
plt.show()

这导致:

绘制线段时,我使用 alpha 关键字调整了透明度,该关键字的范围从 0-1 或透明到实心.我还做了一条更粗的线,以超出原始结果.

When I plotted the line segment, I adjusted the transparent-ness using the alpha keyword, which ranges from 0-1 or transparent to solid. I also made it a thicker line to extend beyond the original results.

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

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