在matplotlib中为起点和终点提供不同的颜色

时间:2017-03-11 04:15:58

标签: python python-3.x matplotlib

我有一堆点(在这个例子中为25),它创建随机并改变每个开始,我的代码计算最短路并以这种方式绘制。我想以不同的颜色显示起点和终点。有可能吗?

这是我的图和我的代码,它使用matplot进行绘图:

Shortest way after calculation

data = mutant[0]

x_val = [x[0] for x in data]
y_val = [x[1] for x in data]

print
x_val
plt.plot(x_val, y_val)
plt.plot(x_val, y_val, 'or')
plt.show()

注意:我最好的方法是列表:

mutant =  [[[-8.9100652418836788, -4.5399049973954666], [-7.4314482547739411, 6.6913060635885833], [9.5105651629515346, 3.0901699437494741], [-8.4804809615642611, -5.2991926423320477], [2.2495105434386491, -9.7437006478523518], [-4.5399049973954693, -8.9100652418836788], [9.9254615164132201, 1.2186934340514748], [9.5630475596303537, -2.9237170472273717], [4.5399049973954666, -8.9100652418836805], [1.908089953765451, -9.8162718344766393], [-2.2495105434386504, 9.7437006478523518], [9.7029572627599645, 2.4192189559966772], [-4.5399049973954693, -8.9100652418836788], [9.2718385456678742, 3.74606593415912], [9.8480775301220795, 1.7364817766693033], [-0.69756473744125325, 9.9756405025982424], [-2.4192189559966777, -9.7029572627599645], [-5.4463903501502706, 8.3867056794542414], [3.9073112848927369, 9.2050485345244031], [9.993908270190957, -0.34899496702500821], [9.4551857559931687, -3.255681544571567], [7.193398003386509, -6.9465837045899761], [2.588190451025203, -9.6592582628906847], [4.383711467890774, -8.9879404629916699], [-8.8294759285892699, 4.6947156278589066]]]

注意:我使用的是python 3.6

1 个答案:

答案 0 :(得分:2)

在plt.show()

之前添加两行
plt.plot(x_val[0], y_val[0], 'og')
plt.plot(x_val[-1], y_val[-1], 'ob')

他们用绿点覆盖第一个元素,用蓝点覆盖最后一个元素。