ZK Zhao
ZK Zhao

Reputation: 21523

Matplotlib & Seaborn: Legend too long for line style

I make plot with different kind of styles in '-', '--', '-.'

x=arange(1,9)
gmm_rsquare = [0.90,0.962,0.954,0.908,0.975,0.941,0.905,0.916,]
al_rsquare=[0.85,0.742,0.819,0.884,0.901,0.868,0.793,0.727]
emp_rsquare = [0.908,0.948,0.937,0.920,0.967,0.948,0.945,]
plot(x, gmm_rsquare, label='GMM')
plot(x, al_rsquare, '--', label='AL')
plot(x[:-1], emp_rsquare, '-.', label='Emp')
plt.axis([1,8,0.7,1])
plt.legend()

enter image description here

The legend for -. is not very good, because there is an addtional dash after -..

The problem is also true for seaborn, and even worse

enter image description here

The -- becomes --., and -. becomes -.., due to the improper length of the legend.

How can I fix this?

Upvotes: 1

Views: 1493

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339250

plt.legend has an argument

handlelength : float or None
The length of the legend handles. Measured in font-size units. Default is None which will take the value from the legend.handlelength rcParam.

Hence you can set

plt.legend(handlelength=1.44)

enter image description here

Chosing a different fontsize may require you to chose a different handlelength as well.

Upvotes: 1

Related Questions