Wild Feather
Wild Feather

Reputation: 229

How to change thickness of line in a Python plot legend, but not in the plot itself?

I have the following code, which produces a graph with six different lines of different colors:

#!/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()

It is very difficult to distinguish some of the colors of the lines in the legend, so I would like to change their thickness, without affecting the thickness of the lines in the actual plot. Is it possible to do this? None of the solutions offered in similar questions I found on this site have worked, I can compile the examples but the thickness does not change.

Upvotes: 1

Views: 4634

Answers (1)

DavidG
DavidG

Reputation: 25380

The following minimal example from your code works for me. It also works when running the full code you posted. I am using 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()

Giving the following figure:

enter image description here

Upvotes: 2

Related Questions