如何在Python图例中更改线条的粗细,但不在图块本身中?

时间:2017-10-26 18:07:58

标签: python matplotlib

我有以下代码,它生成一个包含六种不同颜色的不同颜色的图形:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import math
import numpy
import pylab as pl

int_amp=[0.00,0.40,0.80,1.20,1.60,2.00,2.40,2.80,3.20,3.60,4.00,4.40,4.80]
masa_gr_circ1=[0.00,0.02,0.04,0.06,0.07,0.10,0.11,0.13,0.15,0.17,0.19,0.21,0.24]
masa_gr_circ2=[0.00,0.02,0.06,0.10,0.14,0.18,0.21,0.25,0.29,0.33,0.36,0.40,0.44]
masa_gr_circ3=[0.00,0.07,0.14,0.21,0.28,0.36,0.42,0.49,0.57,0.64,0.71,0.78,0.85]
masa_gr_circ4=[0.00,0.01,0.02,0.02,0.03,0.04,0.05,0.06,0.06,0.08,0.09,0.10,0.11]
masa_gr_circ5=[0.00,0.02,0.05,0.07,0.09,0.12,0.15,0.17,0.20,0.23,0.25,0.27,0.30]
masa_gr_circ6=[0.00,0.05,0.10,0.15,0.19,0.24,0.29,0.34,0.38,0.43,0.48,0.53,0.58]

fit1=numpy.polyfit(int_amp,masa_gr_circ1,1)
fit2=numpy.polyfit(int_amp,masa_gr_circ2,1)
fit3=numpy.polyfit(int_amp,masa_gr_circ3,1)
fit4=numpy.polyfit(int_amp,masa_gr_circ4,1)
fit5=numpy.polyfit(int_amp,masa_gr_circ5,1)
fit6=numpy.polyfit(int_amp,masa_gr_circ6,1)
fitpol1=numpy.poly1d(fit1)
fitpol2=numpy.poly1d(fit2)
fitpol3=numpy.poly1d(fit3)
fitpol4=numpy.poly1d(fit4)
fitpol5=numpy.poly1d(fit5)
fitpol6=numpy.poly1d(fit6)
xfit=numpy.linspace(0.0,4.90,num=1000)
yfit1=fitpol1(xfit)
yfit2=fitpol2(xfit)
yfit3=fitpol3(xfit)
yfit4=fitpol4(xfit)
yfit5=fitpol5(xfit)
yfit6=fitpol6(xfit)

pl.plot(int_amp, masa_gr_circ1, 'ro', markersize=5)
pl.plot(int_amp, masa_gr_circ2, 'ro', markersize=5)
pl.plot(int_amp, masa_gr_circ3, 'ro', markersize=5)
pl.plot(int_amp, masa_gr_circ4, 'ro', markersize=5)
pl.plot(int_amp, masa_gr_circ5, 'ro', markersize=5)
pl.plot(int_amp, masa_gr_circ6, 'ro', markersize=5)
pl.plot(xfit, yfit1, 'b', linewidth=1, label='Circuito 1')
pl.plot(xfit, yfit2, 'crimson', linewidth=1, label='Circuito 2')
pl.plot(xfit, yfit3, 'magenta', linewidth=1, label='Circuito 3')
pl.plot(xfit, yfit4, 'g', linewidth=1, label='Circuito 4')
pl.plot(xfit, yfit5, 'teal', linewidth=1, label='Circuito 5')
pl.plot(xfit, yfit6, 'sienna', linewidth=1, label='Circuito 6')

pl.legend()
pl.show()

很难区分图例中线条的某些颜色,所以我想改变它们的厚度,而不会影响实际图形中线条的粗细。是否有可能做到这一点?我在本网站上发现的类似问题中没有提供的解决方案都有效,我可以编译示例,但厚度不会改变。

1 个答案:

答案 0 :(得分:0)

您的代码中的以下最小示例适合我。它在运行您发布的完整代码时也有效。我使用的是Python 2.7.13

import pylab as pl
import math
import numpy

int_amp=[0.00,0.40,0.80,1.20,1.60,2.00,2.40,2.80,3.20,3.60,4.00,4.40,4.80]
masa_gr_circ1=[0.00,0.02,0.04,0.06,0.07,0.10,0.11,0.13,0.15,0.17,0.19,0.21,0.24]
masa_gr_circ2=[0.00,0.02,0.06,0.10,0.14,0.18,0.21,0.25,0.29,0.33,0.36,0.40,0.44]

fit1=numpy.polyfit(int_amp,masa_gr_circ1,1)
fit2=numpy.polyfit(int_amp,masa_gr_circ2,1)
fitpol1=numpy.poly1d(fit1)
fitpol2=numpy.poly1d(fit2)

xfit=numpy.linspace(0.0,4.90,num=1000)
yfit1=fitpol1(xfit)
yfit2=fitpol2(xfit)

pl.plot(int_amp, masa_gr_circ1, 'ro', markersize=5)
pl.plot(xfit, yfit1, 'b', linewidth=1, label='Circuito 1')
pl.plot(int_amp, masa_gr_circ2, 'ro', markersize=5)
pl.plot(xfit, yfit2, 'crimson', linewidth=1, label='Circuito 2')

leg = pl.legend()

for i in leg.legendHandles:
    i.set_linewidth(5)

pl.show()

给出以下数字:

enter image description here