我正在尝试从一系列坐标绘制一条连续线,但我想在特定的接合点更改线的颜色。
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.053,5088),依此类推...什么是最好的方法做这个 ?我有一个条件语句,可以检测条件然后插入空白值或其他操作
这是我到目前为止所拥有的。
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)
示例输入文件将如下所示(仅供参考,我将从中提取坐标)
X10 Y10 A10 B10
X11 Y11 A10
X12.4 Y23.5 A5 ...
答案 0 :(得分:1)
我会使用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()