legend
Adding legend based on existing color series
You can create the legend handles using an empty plot with the color based on the colormap and normalization of the scatter plot. import pandas as pd import numpy as np; np.random.seed(1) import matplotlib.pyplot as plt x = [np.random.normal(5,2, size=20), np.random.normal(10,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,1, size=20)] y = [np.random.normal(5,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,2, size=20), np.random.normal(10,2, … Read more
Is it possible to add a string as a legend item
Alternative solution, kind of dirty but pretty quick. import pylab as plt X = range(50) Y = range(50) plt.plot(X, Y, label=”Very straight line”) # Create empty plot with blank marker containing the extra label plt.plot([], [], ‘ ‘, label=”Extra label on the legend”) plt.legend() plt.show()
Legend overlaps with the pie chart
The short answer is: You may use plt.legend‘s arguments loc, bbox_to_anchor and additionally bbox_transform and mode, to position the legend in an axes or figure. The long version: Step 1: Making sure a legend is needed. In many cases no legend is needed at all and the information can be inferred by the context or … Read more
Scatter plot with legend for each color in c
First, I have a feeling you meant to use apostrophes, not backticks when declaring colours. For a legend you need some shapes as well as the classes. For example, the following creates a list of rectangles called recs for each colour in class_colours. import matplotlib.patches as mpatches classes = [‘A’,’B’,’C’] class_colours = [‘r’,’b’,’g’] recs = … Read more
How to plot a scatter plot with a legend label for each class
Actually both linked questions provide a way how to achieve the desired result. The easiest method is to create as many scatter plots as unique classes exist and give each a single color and legend entry. import matplotlib.pyplot as plt x=[1,2,3,4] y=[5,6,7,8] classes = [2,4,4,2] unique = list(set(classes)) colors = [plt.cm.jet(float(i)/max(unique)) for i in unique] … Read more