0

While using the legends module for matplotlib is fine for labeling my plots distinguishably, I want to label my plots using a color labeling like (without the lines):

enter image description here

If I where to go about this with my plot, I use the text module to put in the labels with the same color. For example, from my plot:

fig6 = plt.figure()
VelCumullog = fig6.add_subplot(111)


VelCumullog.plot(VelCumu[0], VelCumu[1], color = 'slateblue', label = 'Illustris-1')
VelCumullog.plot(VelCumuD[0], VelCumuD[1], color = 'crimson',  label = 'Illustris-1-Dark')
VelCumullog.set_xscale('log')
VelCumullog.set_yscale('log')
VelCumullog.set_xlim(50,500)
VelCumullog.set_ylim(1,5000)
VelCumullog.set_xlabel('$\mathrm{Velocity\ Relative\ to\ Host}\ [\mathrm{km}\ \mathrm{s}^{-1}]$')
VelCumullog.set_ylabel('$N\ (>v_{\mathrm{rel}})$ ', labelpad=-1)
VelCumullog.set_xticks([100, 1000])
VelCumullog.set_yticks([10, 100, 1000])
VelCumullog.get_xaxis().set_major_formatter(tic.ScalarFormatter())
VelCumullog.get_yaxis().set_major_formatter(tic.ScalarFormatter())
#VelCumullog.legend(loc='upper left', frameon=False)
VelCumullog.text(60, 2800, 'Illustris-1', color='slateblue')
VelCumullog.text(60, 1800, 'Illustris-1-Dark', color='crimson')

enter image description here

Where you see me just use text instead of legend.

But as you see, if I where to do this method for other plots, it can get pretty tedious to porperly place the labels on the plot, since I have to define their coordinates. Especially if the spacing between the text are off in comparison to the other texts in plots.

I was wondering if their would be another method in what I am wanting to do, like using the legends module, or something else that makes my life easier.

1 Answer 1

1

If I understand correctly, you want your legends to have no lines, and have the text colored. We can do that by setting handlelength=0 when calling legend and manually changing the text color.

The following works for me:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib.ticker as tic
fig6 = plt.figure()
VelCumullog = fig6.add_subplot(111)

VelCumu = [np.arange(0,1000,1.0)]
VelCumu.append(1000-2.0*VelCumu[0])
VelCumuD = [np.arange(0,1000,1.0)]
VelCumuD.append(1200-2.0*VelCumu[0])

VelCumullog.plot(VelCumu[0], VelCumu[1], color = 'slateblue', label = 'Illustris-1')
VelCumullog.plot(VelCumuD[0], VelCumuD[1], color = 'crimson',  label = 'Illustris-1-Dark')
VelCumullog.set_xscale('log')
VelCumullog.set_yscale('log')
VelCumullog.set_xlim(50,500)
VelCumullog.set_ylim(1,5000)
VelCumullog.set_xlabel('$\mathrm{Velocity\ Relative\ to\ Host}\ [\mathrm{km}\ \mathrm{s}^{-1}]$')
VelCumullog.set_ylabel('$N\ (>v_{\mathrm{rel}})$ ', labelpad=-1)
VelCumullog.set_xticks([100, 1000])
VelCumullog.set_yticks([10, 100, 1000])
VelCumullog.get_xaxis().set_major_formatter(tic.ScalarFormatter())
VelCumullog.get_yaxis().set_major_formatter(tic.ScalarFormatter())
l = VelCumullog.legend(loc='upper left', frameon=False, handlelength=0)
l.get_texts()[0].set_color('slateblue')
l.get_texts()[1].set_color('crimson')

plt.show()

enter image description here

If that does not work, you can try to change the Artists that draw the legend graphic to an invisible box:

empty = Rectangle((0, 0), 0, 0, alpha=0.0)
l = VelCumullog.legend([empty, empty], ['Illustris-1', 'Illustris-1-Dark'], loc='upper left', frameon=False, handlelength=0, handletextpad=0)
l.get_texts()[0].set_color('slateblue')
l.get_texts()[1].set_color('crimson')

Notice I also set handletextpad=0 which might help some alignment issues (removes the space between the invisible Artist and the label).

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for trying to reproduce my plot! Question: What do I do when I set the handle length to zero, i still get a small portion of my handle/line?
I added some information on how to make an invisible artist instead of a line in the legend. Does this help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.