Questions tagged [legend]

A graph legend is commonly used when plotting several sets of data within the same diagram. The legend associates each plot with its corresponding data.

A legend attached to a graph (or other visualization of data) is used to explain the graphical language (colors, symbols, lines, etc) of the specific visualization.

In scientific software for statistical computing and graphics and also , function legend adds a legend box to an existing plot.

4990 questions
1574
votes
18 answers

How to put the legend outside the plot

I have a series of 20 plots (not subplots) to be made in a single figure. I want the legend to be outside of the box. At the same time, I do not want to change the axes, as the size of the figure gets reduced. I want to keep the legend box outside…
pottigopi
  • 15,759
  • 3
  • 15
  • 4
501
votes
9 answers

How to change legend fontsize with matplotlib.pyplot

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this: plot.figure() plot.scatter(k, sum_cf, color='black', label='Sum of…
mike
  • 22,931
  • 31
  • 77
  • 100
478
votes
6 answers

Adding a matplotlib legend

How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables? Please consider the graphing script below: if __name__ == '__main__': PyPlot.plot(total_lengths, sort_times_bubble, 'b-', …
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
460
votes
11 answers

Secondary axis with twinx(): how to add to legend

I have a plot with two y-axes, using twinx(). I also give labels to the lines, and want to show them with legend(), but I only succeed to get the labels of one axis in the legend: import numpy as np import matplotlib.pyplot as plt from matplotlib…
joris
  • 133,120
  • 36
  • 247
  • 202
351
votes
4 answers

Remove legend ggplot 2.2

I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE) and geom_point(aes(color = vs), show.legend = FALSE). Edit: As this question and its…
Guy
  • 3,535
  • 2
  • 12
  • 9
310
votes
11 answers

Remove the legend on a matplotlib figure

To add a legend to a matplotlib plot, one simply runs legend(). How to remove a legend from a plot? (The closest I came to this is to run legend([]) in order to empty the legend from data. But that leaves an ugly white rectangle in the upper right…
Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
285
votes
5 answers

Moving matplotlib legend outside of the axis makes it cutoff by the figure box

I'm familiar with the following questions: Matplotlib savefig with a legend outside the plot How to put the legend out of the plot It seems that the answers in these questions have the luxury of being able to fiddle with the exact shrinking of the…
jonathanbsyd
  • 8,031
  • 6
  • 24
  • 26
216
votes
10 answers

Plot a legend outside of the plotting area in base graphics?

As the title says: How can I plot a legend outside the plotting area when using base graphics? I thought about fiddling around with layout and produce an empty plot to only contain the legend, but I would be interested in a way using just the base…
Henrik
  • 14,202
  • 10
  • 68
  • 91
204
votes
9 answers

Add a common Legend for combined ggplots

I have two ggplots which I align horizontally with grid.arrange. I have looked through a lot of forum posts, but everything I try seem to be commands that are now updated and named something else. My data looks like this; # Data plot 1 …
jO.
  • 3,384
  • 7
  • 28
  • 38
191
votes
7 answers

Remove 'a' from legend when using aesthetics and geom_text

How can I can remove the letter 'a' from the legend generated by this code? If I remove the geom_text, then the 'a' letter will not show in the legend. I want to keep geom_text, though. ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, …
user2700207
  • 1,953
  • 2
  • 12
  • 4
178
votes
4 answers

Add legend to ggplot2 line plot

I have a question about legends in ggplot2. I managed to plot three lines in the same graph and want to add a legend with the three colors used. This is the code used library(ggplot2) ## edit from original post - removed lines that downloaded data…
pacomet
  • 5,011
  • 12
  • 59
  • 111
172
votes
8 answers

Is there a way to change the spacing between legend items in ggplot2?

Is there a way to change the spacing between legend items in ggplot2? I currently have legend.position ="top" which automatically produces a horizontal legend. However, the spacing of the items is very close together and I am wondering how to…
overflowname
  • 1,993
  • 3
  • 13
  • 12
167
votes
4 answers

Matplotlib - global legend and title aside subplots

I've started with matplot and managed some basic plots, but now I find it hard to discover how to do some stuff I need now :( My actual question is how to place a global title and global legend on a figure with subplots. I'm doing 2x3 subplots where…
Gere
  • 12,075
  • 18
  • 62
  • 94
158
votes
3 answers

matplotlib: colorbars and its text labels

I'd like to create a colorbar legend for a heatmap, such that the labels are in the center of each discrete color. Example borrowed from here: import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap #discrete…
dimka
  • 4,301
  • 11
  • 31
  • 36
155
votes
5 answers

How to manually create a legend

I am using matplotlib and I would like to manually add items to the legend that are a color and a label. I am adding data to to the plot to specifying there would lead to a lot of duplicates. My thought was to do: …
Brady forcier
  • 1,597
  • 2
  • 11
  • 10
1
2 3
99 100