Python Matplotlib line plot:更改mid中的线条颜色

2024-09-30 22:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把一系列的坐标从一条连续的线变成一条特定的曲线。在

Input arrays: layerdict['Xc'] = [50.6, 69.4, 69.4, 50.6, **50.6**, **50.2**, 69.8, 69.8, 50.2, **50.2**, **69.053**, 69.12, 69.12] layerdict['Yc'] = [50.6, 50.6, 69.4, 69.4, **50.6**, **50.2**, 50.2, 69.8, 69.8, **50.2**, **50.88**, 50.996, 51.796]

**仅用于视觉目的

我想改变线的颜色,从(50.6,50.6)到(50.2,50.2)和(50.2,50.6)到(69.0535088)等等。。。最好的办法是什么?我有一个条件语句,可以检测then条件并插入空值或其他操作

这是我到目前为止的情况。在

layerdict = {'Xc': [], 'Yc': [], 'Xt': [], 'Yt': []}

with open(inputfilepath, 'r') as ifile:

for item in ifile:
    gonematch = gonepattern.match(item)
    gtrmatch = gtrpattern.match(item)

    if gonematch:
        tlist = item.split(' ')
        layerdict['Xc'].append(float(tlist[1][1:]))
        layerdict['Yc'].append(float(tlist[2][1:]))
    elif gtrmatch:
        tlist = item.split(' ')
        layerdict['Xt'].append(float(tlist[1][1:]))
        layerdict['Yt'].append(float(tlist[2][1:]))



plt.plot(layerdict['Xc'], layerdict['Yc'], label='linepath', linewidth=3.5)


plt.xlabel('X')
plt.ylabel('Y')
plt.show(block=True)

示例输入文件如下所示(仅供参考,我从中提取坐标)

^{pr2}$

Tags: matchpltfloatitem条件xtytxc
1条回答
网友
1楼 · 发布于 2024-09-30 22:18:48

我会使用numpy的ma模块中的屏蔽数组。虽然numpy数组在索引和数学方面已经比简单列表好,但是掩码数组只在掩码值为False的地方自动绘制。但是,您仍然可以通过使用data属性来检索整个无掩码数组,因此先绘制整个数组,然后只绘制一个子集,只需使用两个几乎相同的plot命令即可:

import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma

layerdict = dict()
layerdict['Xc'] = [50.6, 69.4, 69.4, 50.6, 50.6, 50.2, 69.8, 69.8, 50.2, 50.2, 69.053, 69.12, 69.12]
layerdict['Yc'] = [50.6, 50.6, 69.4, 69.4, 50.6, 50.2, 50.2, 69.8, 69.8, 50.2, 50.88, 50.996, 51.796]

highlightmask = np.ones(len(layerdict['Xc'])).astype(bool)
highlightmask[4:6] = highlightmask[9:11] = False

layerdict['Xc'] = ma.array(layerdict['Xc'])
layerdict['Yc'] = ma.array(layerdict['Yc'], mask=highlightmask)

plt.plot(layerdict['Xc'], layerdict['Yc'].data, label='linepath', linewidth=3.5)
plt.plot(layerdict['Xc'], layerdict['Yc'], 'r', linewidth=3.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

相关问题 更多 >