1

I'm have a plot with rgba points and, when I show the legend for them, I got the markers' legend with alpha values. How can I show them with full opacity?

Simplifying, this is how I plot the points and show the legend:

x = np.arange(10)
y = np.arange(10)

alphas = np.linspace(0.1, 1, 10)
rgba_colors = np.zeros((10,4))
rgba_colors[:,0] = 1.0
rgba_colors[:, 3] = alphas

plt.scatter(x, y, color=rgba_colors, marker='x', facecolors='none', linewidth=2, label='Min Points')

plt.legend(loc='center left', scatterpoints = 1)

And this is what I got:

enter image description here

As can be seen, the legend markers are a little bit transparent. How can I show them in full opacity?

I tried this approach, and this is what I got (no x marker at all):

enter image description here

And when I change the color=='white parameter, I get a line over the marker.

enter image description here

Thank you in advance.

Community
  • 1
  • 1
pceccon
  • 9,379
  • 26
  • 82
  • 158

1 Answers1

2

I could solve it using this piece of code:

marker_min = plt.Line2D((0, 0), (0, 0), markeredgecolor=(0.5, 0.0, 0.0), markerfacecolor='none', linestyle='', marker='x', markeredgewidth=2, markersize=5)
marker_max = plt.Line2D((0, 0), (0, 0), markeredgecolor=(0.0, 0.5, 0.0), markerfacecolor='none', linestyle='', marker='o', markeredgewidth=2, markersize=5)
plt.legend([marker_min, marker_max], ['Min Points', 'Max Points'], numpoints=1, loc='center left', bbox_to_anchor=(1, 0.5))

enter image description here

Just don't know if it is the best solution.

pceccon
  • 9,379
  • 26
  • 82
  • 158
  • 2
    This is not a beautiful solution, but it might still be the simplest and best! One minor change, though: You can supply `Line2D` with empty points, as well: `Line2D([],[],...)`. I tried to figure out a better way, but the better ways seem to lead you deep into the object model and may be rather fragile as the version changes. – DrV Jul 19 '14 at 20:46